당신이 SwingWorker
코드로 탐구하면 정의는 다음 상수 볼 수 있습니다 :
/**
* number of worker threads.
*/
private static final int MAX_WORKER_THREADS = 10;
는 따라서 배경 스레드의 수는 당신이 실제로 만들 SwingWorker
의 수에 관계없이이 값을 초과하지 않을 수 있습니다 . 배경 스레딩 모델을 변화하는
한 가지 방법은 연결하는 것입니다 귀하의 SwingWorker
클래스와 연관된 AppContext
에 자신의 ExecutorService
. 그러나 이것은 AppContext
이 sun.awt
에 속하고 따라서 공식 JDK API의 일부가 아니므로 다소 사기성입니다.
// Create single thread executor to force all background tasks to run on the same thread.
ExecutorService execService = Executors.newSingleThreadExecutor();
// Retrieve the AppContext. *CAUTION*: This is part of the sun.awt package.
AppContext ctxt = AppContext.getAppContext();
// Verify that nothing is already associated with SwingWorker.class within the context.
Object obj = ctxt.get(SwingWorker.class);
if (obj != null) {
throw new IllegalStateException("Object already associated with SwingWorker: " + obj);
}
// Install ExecutorService. Will be retrieved by the SwingWorker when execute() is called.
ctxt.put(SwingWorker.class, ctxt);
Aha. 그거 알아두면 좋을거야. 나는 그것을 알아 차리지 못했다. – RCC
SwingWorker가 Runnable이기 때문에 SwingWorker가 스레딩을보다 잘 제어해야한다면 SwingWorkers를 자신의 ExecutorService를 통해 실행할 수 있습니다. – Sbodd