Executor 프레임 워크는 눈에 띄는 것이지만 스레드 인 자체 풀을 유지합니다. 그렇다면 왜 우리는 Thread/Runnable을 매개 변수로 전달해야합니다. 왜 간단한 Task 인터페이스가 없습니까? ThreadPoolExecutor입니다 내부 실행에 전달 스레드의 방법을 사용하기 때문에 Executor Framework의 작업이 스레드 인 이유
ExecutorService executorService = Executors.newFixedThreadPool(3); // 3 worker thread
executorService.execute(new Thread()); // why thread? Nobody is going to use this as thread?
나는이 요청하고있다.
있는 ThreadPoolExecutor의 코드 발췌 아래를 참조하십시오 :
final void runWorker(Worker w) {
Runnable task = w.firstTask;
w.firstTask = null;
boolean completedAbruptly = true;
try {
while (task != null || (task = getTask()) != null) {
w.lock();
clearInterruptsForTaskRun();
try {
beforeExecute(w.thread, task);
Throwable thrown = null;
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
} finally {
task = null;
w.completedTasks++;
w.unlock();
}
}
completedAbruptly = false;
} finally {
processWorkerExit(w, completedAbruptly);
}
}
내가 여기 중요한 것을 놓치고 있으면 알려 주시기 바랍니다.
W Gandaliter는 Executor가 항상 호출자 스레드로 작업을 처리 할 필요는 없습니다. 오라클 워드 프로세서에서 스 니펫을 확인하십시오 ------------------------ "보다 일반적으로, 태스크는 호출자의 스레드가 아닌 다른 스레드에서 실행됩니다. 각 작업에 대한 새로운 스레드. " 클래스 ThreadPerTaskExecutor는 Executor를 구현합니다. { public void execute (Runnable r) { 새 스레드 (r) .start(); } } – user001
예, 이러한 인터페이스의 요점은 스레딩을 다루는 것이므로 스레딩에 대해 전혀 모르는 간단한 Runnable을 사용할 수 있습니다. 물론 Executor는 다른 스레드에서 실행됩니다 (일반적으로). 이것이 명확하지 않으면 죄송합니다. – gandaliter