There are plenty of ways to manipulate threads.
suspend()
: puts a thread in a suspended state and can be resumed using resume()
stop()
: stops a thread
resume()
: resumes a thread, which was suspended using suspend()
.
notify()
: Wakes up a single thread.
wait()
: makes the current thread wait.. (or sleep) until another thread invokes the notify()
method for that thread.
notifyAll()
: will wake up all sleeping (waiting) threads.
Note
In the latest versions of Java resume( ), suspend( ) and stop( ) has been deprecated
Question from OP
but when I wake it up, it resumes from where? Does it start from the beginning or from where it left?
Imagine a simple for
-loop.
Starting thread 1.
Starting thread 2.
Thread 1: 0
Thread 2: 0
Thread 1: 1
Thread 2: 1
Pausing thread 1.
Thread 2: 2
Thread 2: 3
Thread 2: 4
Resuming thread 1.
Thread 1: 2
Thread 2: 5