I'm writing a GUI with a set of SwingWorkers that perform different kinds of tests on a USB connected device. On the frontpage (tabbed layout) of my application the user has an option to run all the tests. The tests are run sequentially with a semaphore (therefore not in a predetermined order), however when the user want's to run all the tests through the "main" SwingWorker that starts all the other "sub"-SwingWorkers, the done()
task of the "main" SwingWorker is reached long before any of the "sub"-SwingWorkers actually finish their tests.
The reason I wan't to know when they're all done is because I want to read the results to a text file and I "save" the results of the tests in some variables of the test type.
main-method
SwingWorker main = new SwingWorker<Void, Void>();
main.execute();
main swingworker do-in-background method
if(!this.isCancelled()){
for(SwingWorker worker : workers){ //Workers is an array of the sub-type SwingWorkers
worker.execute();
}
}
main swingworker done method
//get result variables <-- Here I get a nullpointer exception because sub-tests have not yet finished.
//write report using variables
sub swingworker do-in-background method
if(!this.isCancelled(){
//acquire semaphore
//perform test <-- Takes some time to finish.
}
sub swingworker done method
//save results to variable
//release sempaphore
I tried creating a global boolean variable and then wait for that within the report writing class with a while(true)
and thread.sleep(1000)
but that seems to hang the program, which makes sense.
An important part is that all the sub-swingworkers already work as standalone solutions through their respective tabs in the tabbed layout.
Does anyone know of a good way to find out when all the "sub"-SwingWorkers are done?