0

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?

Zeliax
  • 4,987
  • 10
  • 51
  • 79
  • Maybe you should switch to a ThreadPool (Executor) and Futures instead of "Sub-Swingworkers". Then you can just `get` the results inside the main-swingworker's `doInBackground`. With a SingleThreadExecutor you can even ditch the semaphore. – Fildor May 03 '16 at 11:23
  • If you really really need to stick to the subworkers: Maybe a [Countdownlatch](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html) can help you. – Fildor May 03 '16 at 11:25
  • Dunno if this is the best solution as the sub-swingworkers should all be standalone and run through their respective tab in the tabbed layout. I guess I could have explained that better in my question. I will update it. – Zeliax May 03 '16 at 11:25
  • "should all be standalone and run through their respective tab in the tabbed layout" - I think you are mixing GUI and business logic. If the tests shall update a GUI Element - ok. You can still do that with Futures. But most simply you could go for the CountDownLatch as a first step. – Fildor May 03 '16 at 11:29
  • I will check the CountDownLatch out. – Zeliax May 03 '16 at 11:31
  • After reading through the CountDownLatch documentation I decided that the CyclicBarrier mentioned within the documentation is what I will need. At least to avoid rewriting my entire program. However, I am unsure if I can make this work so that all the swingworkers can run separately as the Cyclic Barrier will have to be implemented within each of the SwingWorkers (10 in total) and at the point of reaching the barrier the SwingWorker will hang until others reach the same "await" method. – Zeliax May 03 '16 at 11:50
  • This was the case until I found that the barrier require the "threads"/workers to run side by side (at least that is how I understood it), and the reason I can't do that is due to the fact that the device I'm performing my tests on can only perform one task at a time. Which is also the reason for using a semaphore. I will try to implement a countdownlatch. – Zeliax May 03 '16 at 12:00
  • For [example](http://stackoverflow.com/q/11366330/230513). – trashgod May 03 '16 at 14:20
  • Yeah. I ended up doing something like that. I will add it to the answer once I get time :P – Zeliax May 04 '16 at 07:41

0 Answers0