Reputation: 141
I have a start and stop button. I want to be able to start and stop a task as many times as the user wants to. I was able to get this working properly with a regular thread by doing a wait() when the stop button was pushed and then a notify() when the start button was pushed to start the thread again. This worked great. However... I extended thread. My boss told me to never extend thread and that I should use a SwingWorker. But I noticed that a SwingWorker can only be executed once. Or can it be executed more than once in the same session..?? Can somebody help me in the right direction here?
Upvotes: 4
Views: 759
Reputation: 17359
You don't need a SwingWorker for what you doing. SwingWorker is used for cases when you have to run something in background thread, update your GUI (like progress) without locking i down.
What you did already is fine.
Upvotes: 0
Reputation: 147154
You very rarely need to extend Thread
. What you should do is pass a Runnable
to a Thread
constructor.
For my tastes SwingWorker
adds too much coupling to code, and should be left to demos where it works very well.
Upvotes: 3
Reputation: 81074
You can just create a new instance of your SwingWorker each time you want to run the logic. Personally, I don't see much benefit to SwingWorker for your problem as you described it. Not to say it won't do fine...
Upvotes: 1