0

I have many threads (SwingWorker, Timer, Thread..) in different parts of the application. I need to call one method which pause all these threads. What is the best approach to do this?

Thanks

Additional Info

I have a notepad in the app and it can paste images, draw arrow, lines, type text, you can write with stylus etc.. At the same time in the background this app does different things: check login time, check messages, updates something, works with database.

So when you try to write with stylus and app does all these things in the background, writing with stylus becomes a little bit slow. So I decided to pause all these threads in the background. I think this should help and I wonder what is the best way to pause these threads. They should continue to work after I finish to write with stylus.

  • 3
    add some code so we can help you out – rush2sk8 Jul 22 '14 at 15:00
  • 1
    Why do you think you need to do this? – Kevin Workman Jul 22 '14 at 15:00
  • 2
    "*I need to call one method which stops all these threads*" `System.exit` perhaps? – Pshemo Jul 22 '14 at 15:01
  • This is a very good question (though really a duplicate). I don't know why it got downvoted. Anyone who has really tried to implement this knows that it's problematic to code it in practice. The theory about gracefulness and all that is OK but it's a theory IMHO. At least I am not a aware of a good way to "kill" N threads, just "to kill them", period. So pardon my ignorance; I would be glad if someone posts here a few good authoritative and recent references. – peter.petrov Jul 22 '14 at 15:20
  • It sounds like you want to PAUSE the threads. If so, you should rewrite your title and question. – JasonMArcher Jul 22 '14 at 17:49

2 Answers2

0

If you want to stop them immediately (they might be blocking or something similar), the only approach I know is having references to them and calling interrupt on them. And if you know they are not blocked, then you can just set some boolean flag into them called e.g. enabled. And then let them stop themselves by seeing the new value of this flag as false and exiting immediately in such cases.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • Alternate approach: have them all share an `AtomicBoolean` that they check periodically, and which you set to `false` from the one place. – yshavit Jul 22 '14 at 15:04
  • @yshavit Right... Good idea. But this also doesn't work if some of these threads are blocked (say waiting on IO input or something similar). – peter.petrov Jul 22 '14 at 15:11
0

Try having only one class in aplication with function that "generates threads" and matches them with "workers" (other classes that implement same interface and have different functionality). It can have all their references contained in some variable and call function to stop them (stop() or interrupt()).

mr.engineer
  • 187
  • 12
  • Definitely not `stop()` -- that method is [dangerous and deprecated](http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html). – yshavit Jul 22 '14 at 15:22