It's all in the title, really.
I know Thread.sleep()
only makes the current thread sleep, but is there a way to force all threads to sleep simultaneously, or would that be down to self-management?
Asked
Active
Viewed 2,070 times
1

BlueTooth4269
- 113
- 3
- 14
-
1Each thread would have to put itself to sleep. – Andy Turner Aug 18 '16 at 12:31
-
Maybe this question will help: http://stackoverflow.com/questions/20666454/how-to-pause-all-running-threads-and-then-resume – byxor Aug 18 '16 at 12:32
-
Did you try to iterate over all the running threads and stop them one by one? – Abdulrahman Bres Aug 18 '16 at 12:33
-
@Abdul `Thread.sleep()` is a static method: you can't specify the thread you want to put to sleep. – Andy Turner Aug 18 '16 at 12:37
-
1Why do you need to make all the threads sleep? – Kayaman Aug 18 '16 at 12:44
2 Answers
3
It would be dangerous to put thread into sleep from outside, since it can be currently in some intermediate state (that's the reason why it's not good to kill thread from outside, for example). So please make it via self-management.

Anton Malyshev
- 8,686
- 2
- 27
- 45
-
If a program's threads are properly synchronized then the worst harm that an external agent could cause by temporarily pausing one or more of the threads would be to make the program miss a real-time deadline or fail to meet a performance requirement. It could not cause data corruption. That is, *IF* the program is properly synchronized. Even if it was not properly synchronized, then simultaneously pausing _all_ of its threads for precisely the same interval of time would cause no harm either. – Solomon Slow Aug 18 '16 at 18:01
-
1@jameslarge Your thread is controlling a stepper motor. Because it was put to sleep, it didn't tell it to stop. It now hit the wall, damaging the device. Not a real life example? I broke a 20K prototype with just this problem in C (the thread was slept because I had a debugger on it). It is not generically safe just because its synchronized. – Gabe Sechan Aug 18 '16 at 20:15
-
@GabeSechan, That's the sort of thing I meant when I said, "miss a real-time deadline," but I guess the emphasis came out wrong. Real-time software is a whole category unto itself. I know what you're talking about, only in my case it was an interior wall that took the damage (fixed with some drywall, mud, and paint), and the prototype was unharmed. P.S.: My official recommendation to the mechanical design team was, "We could do this with smaller motors." – Solomon Slow Aug 18 '16 at 21:14
3
The answer is simple : there is no way to make all the thread sleep from Java (from the inside of the application).
Moreover, on Java, there is a lot of internal thread (like GC thread, Finalization thread, ... ) that you cannot manipulate so "all" will never be possible.
Regards,
Loïc

loicmathieu
- 5,181
- 26
- 31