Swingworker instances not running concurrently

后端 未结 2 418
南笙
南笙 2020-12-31 12:23

My computer has 4 cores and I am running a Java swing gui program. When I run my application it only uses two cores and about 30% CPU utilization. I have a large number of f

2条回答
  •  爱一瞬间的悲伤
    2020-12-31 12:41

    Maybe Swing is controlling the scheduling of your threads? Or maybe SwingWorker has a thread pool size of 1 (there is no description in SwingWorker javadoc about running multiple SwingWorker threads and I guess multiple threads could cause some difficult concurrency problems in Swing).

    If all you want to do is run some processing in parallel, can you solve this the simple way by extending the Java Thread class, implementing run, calling SwingUtilities.invokeLater() at the end to update the GUI with any changes:

        class PrepareTask extends Thread
        {
          PrepareTask (int start, int end) { ... }
          public void run() { ... ; SwingUtilities.invokeLater(new Runnable() { public void run() { do any GUI updates here } )}
    

    start the threads with:

        prepareTask.start();
    

    Or maybe something in your data model is single-threaded and blocking the second thread? In that case, you have to solve that problem with the data model.

提交回复
热议问题