Skip to content
Advertisement

Waiting for a task inside a thread pool using SwingWorker java

I’m simulating multiple Tasks inside a thread – when all of them should report to a GUI. So I have a unique form that has 4 Panels inside which should reply and do different Tasks.. they should each keep pooling a database and reporting stuff to the GUI. In this example, I just made it to write numbers to a textArea.

JavaScript

This is the main GUI after all:

Main Gui

and this is the execution:

Running without sleep

I just need to wait for a time on each WorkerDoSomething‘s task, adding that line (previously comment out): Thread.sleep(randomInt(1000, 5000));

but when I do.. the whole execution freezes, of course..because it uses a single thread to run all the tasks – I suppose.

Running with sleep

Is there a solution for this?

Oh…I have to use java 1.8 in the business:

JavaScript

The whole project it’s on my personnal git

— first edit with debug perspective

debug

Advertisement

Answer

The execution is not freezing since Swing uses a thread pool of 10 threads to run the workers.
You can see it work properly if you comment out this part:

JavaScript

PS – Why do you create a new SwingWorker in the actionPerformed method?
Why not simply write like this:

JavaScript
Advertisement