Swing Worker Threads Not Concurrent

后端 未结 3 586
我在风中等你
我在风中等你 2020-12-07 02:16

It seems that when I instantiate 12 Swing Worker threads, the first six starts to complete its task, it finishes AND then the last six starts and finishes. The behavior I\'

相关标签:
3条回答
  • 2020-12-07 02:52

    The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time.

    A CountDownLatch is designed for this very purpose. Here's a good example using a single SwingWorker and a number of subsidiary threads controlled by the latch.

    0 讨论(0)
  • 2020-12-07 02:52

    Your question says:

    The behavior I'm looking for is all 12 threads start working at the same time & finish at the same time.

    But you can't guarantee for all Swing workers threads starting concurrently and ending at same time.

    0 讨论(0)
  • 2020-12-07 03:08

    SwingWorker class has a static ExecutorService field with MAX_WORKER_THREADS = 10. I'm not certain why you see 6 and not 10. But you cannot go over 10.

        /**
         * number of worker threads.
         */
        private static final int MAX_WORKER_THREADS = 10;
    

    ...

        executorService =
                    new ThreadPoolExecutor(1, MAX_WORKER_THREADS,
                                           10L, TimeUnit.MINUTES,
                                           new LinkedBlockingQueue<Runnable>(),
                                           threadFactory);
    
    0 讨论(0)
提交回复
热议问题