Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Saturday, 26 November 2016

Suspending, Resuming, and Stopping Thread with Example.

Previously we have discussed about

Note : In Java 1.1 the suspend(), resume() and stop() methods were used to perform some operations like suspending, resuming and stopping a Thread. These three methods have been deprecated since the release of Java 2 and not used in modern Java programming. The main reason for deprecating these methods was to prevent the occurrence of deadlock situations and system failures in a multi-threaded environment.

The functions of suspending, resuming and stopping a thread are performed using Boolean type flags in a multi-threading environment.
These flags store the current status of the Threads. The run() method works according to the current boolean value of these three flags.

If the suspend flag is set to true then run() method will suspend the execution of the currently running thread. Similarly ,if the resume flag is set true then run() method will resume the execution of the suspended thread. Apart from the suspend and resume a thread will get terminated once the stop flag of the thread is set to true.

Example of Suspend,resume and stop Operations:

class MyThread implements Runnable {
Thread th;
boolean suspend_Flag;
boolean resume_Flag;
boolean stop_Flag;
public MyThread(String st) {
th = new Thread(this,st);
suspend_Flag = false;
stop_Flag = false;
th.start();
}
@Override
public void run() {
try {
int j = 1;
while(++j<20){
synchronized (this) {
while(suspend_Flag){
wait();
}
if(stop_Flag){
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
synchronized void suspend(){
suspend_Flag = true;
}
synchronized void resume(){
suspend_Flag = false;
notify();
}
synchronized void stop(){
suspend_Flag = false;
stop_Flag = true;
notify();
}
}
public class SysResAndStopExample {

public static void main(String[] args) {
try {
MyThread t = new MyThread("INDIA");
System.out.println("Thread MyThread is Created & Started :");
Thread.sleep(1000);
t.suspend();
System.out.println("Thread MyThread is Suspended");
Thread.sleep(1000);
t.resume();
System.out.println("Thread MyThread is Resume");
Thread.sleep(1000);
t.suspend();
System.out.println("Thread MyThread is Suspended.");
Thread.sleep(1000);
t.resume();
System.out.println("Thread MyThread is Resume.");
Thread.sleep(1000);
t.stop();
System.out.println("Thread MyThread is Stop.");
} catch (Exception e) {
e.printStackTrace();
}
}

}
Program Output:
Thread MyThread is Created & Started :
Thread MyThread is Suspended
Thread MyThread is Resume
Thread MyThread is Suspended.
Thread MyThread is Resume.
Thread MyThread is Stop.

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment