1

I have problem with SwingWorker and it's done() method. I have an application that supports plugins through SPI, so I basically can't change the behavior of the plugins. The interface these plugins have to implement contains method List<T> getContracts(). This is the method I am calling from SwingWorker's doInBackground() method. But some of the plugins use multiple threads in this getContracts() method. The problem is, that the done() method is then called before these threads finish. Is there a way to make SwingWorker thread not to finish (not to call it's done() method) until all the threads called from this SwingWorker stop?

Thanks for your help.

tom
  • 311
  • 3
  • 4
  • 13
  • 1
    How can `getContracts` return the list when the threads are still running? Usually `doInBackground` would wait until all `getContracts` are done before exiting and calling `done`. – toto2 Jun 20 '11 at 19:14
  • That's what I was thinking, but it obviously doesn't wait. Individual threads add items to the list that is returned after all threads finished, so I have no idea how `SwingWorker` can finish before these threads – tom Jun 20 '11 at 19:48
  • If you can get a handle on the worker threads you could Thread.join() them. – extraneon Jun 20 '11 at 19:50
  • I hope that list is synchronized. What's SPI? Google gives many different SPI's. You should look at the api: as @extraneon said, if you could get the threads, you could join. There might be other threading methods too. – toto2 Jun 20 '11 at 20:09
  • SPI is servis provider interface, thanxs for you help guys, I'll try – tom Jun 20 '11 at 20:48

1 Answers1

1

It seems you should just be wrapping this question in your SwingWorker done() method.

Community
  • 1
  • 1
Jim
  • 3,476
  • 4
  • 23
  • 33
  • In fact, there are several interesting answers there. I like the accepted one because it is straightforward, and the one by sjlee, where you get a list of Futures because you could SwingWorker.publish() the Futures as they come back, updating your GUI incrementally if you wanted. – Jim Jun 20 '11 at 22:16
  • Actually I should be wrapping that in the plugin whose `getContracts` method I use (and since I am the creator of that plugin, I did it :) ) thank you – tom Jun 20 '11 at 22:47