Based on the requirements sometimes you might want to suspend, resume or stop a thread. For doing such operations on a thread, before Java 2, thread API used to contain suspend(), resume() and stop() methods.
But all these methods might lead to undesired behavior of the program or machine. For example, these methods might lead to deadlock when a thread locked and is accessing a data structure and suddenly it is suspended or stopped. All other threads will be waiting indefinitely for gaining access to the data structure.
Because of this drawback of suspend(), resume() and stop() methods, they have been deprecated (should not be used) from Java 2 on wards.
Instead we have to check whether the thread is suspended or not through code itself. We can do that using a boolean variable which acts like a flag (ON / OFF).
Below program demonstrates the use of the boolean variable suspendFlag which is initially false. We write two more methods mySuspend() and myResume() which changes the value of the variable suspendFlag.
We have also used two more methods wait() and notify() from Object class. The wait() method suspends the current thread and the notify() method resumes (wakes up) the execution of the current thread.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | class MyThread implements Runnable { Thread t; boolean suspendFlag; MyThread(String name) { t = new Thread(this, name); System.out.println("Child thread: " + t); suspendFlag = false; t.start(); } @Override public void run() { try { for (int i = 1; i <= 5; i++) { System.out.println(t.getName() + ": " + i); Thread.sleep(500); synchronized(this) { while (suspendFlag) wait(); } } } catch (InterruptedException e) { System.out.println(t.getName() + " is interrupted!"); } System.out.println(t.getName() + " is terminated"); } public synchronized void mySuspend() { suspendFlag = true; } public synchronized void myResume() { suspendFlag = false; notify(); } } public class Driver { public static void main(String[] args) { MyThread thread1 = new MyThread("First Thread"); MyThread thread2 = new MyThread("Second Thread"); try { System.out.println("Main thread is waiting…"); Thread.sleep(1000); thread1.mySuspend(); System.out.println("—Suspending thread 1—"); Thread.sleep(1000); thread1.myResume(); System.out.println("—Resuming thread 1—"); Thread.sleep(1000); thread1.t.join(); thread2.t.join(); } catch (InterruptedException e) { System.out.println("Main thread is interrupted!"); } System.out.println("Main thread terminated"); } } |
Output of the above program is:
Child thread: Thread[First Thread,5,main]
Child thread: Thread[Second Thread,5,main]
Main thread is waiting…
First Thread: 1
Second Thread: 1
First Thread: 2
Second Thread: 2
—Suspending thread 1—
First Thread: 3
Second Thread: 3
Second Thread: 4
—Resuming thread 1—
First Thread: 4
Second Thread: 5
First Thread: 5
Second Thread is terminated
First Thread is terminated
Main thread terminated
In the above output you can observer that first thread is suspended for some time and has been resumed again.
Below program demonstrates stopping a thread:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class MyThread implements Runnable { Thread t; private volatile boolean stopFlag = false; MyThread() { t = new Thread(this); t.start(); } public void run() { while (!stopFlag) { System.out.println("Child thread running!"); try { Thread.sleep(200); } catch (InterruptedException e) {} } System.out.print("Child thread stopped!"); } public void stop() { stopFlag = true; } } public class Driver { public static void main(String[] args) throws InterruptedException { MyThread m = new MyThread(); Thread.sleep(1000); m.stop(); } } |
Output of the above program is:
Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread stopped!
Take your time to comment on this article.