Run the following code to suspend and resume a thread.
Listing 5
class myThread implements Runnable
{
String threadName;
Thread t;
Public myThread(String name)
{
threadName = name;
t = new Thread(threadName);
System.out.println(“The value of name: ” + name);
t.start();
}
public void run()
{
try
{
for (int i = 15; i >= 1; i--)
{
System.out.println(“Thread ” + t + “Count: ” + i);
t.sleep(200);
}
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
System.out.println("Terminating”+t);
}
}
class demo
{
public static void main(String a[])
{
myThread T1 = new myThread(“First ”); myThread T2 = new myThread(“Second ”)
; Try
{
Thread.sleep(1000); T1.t.suspend(); System.out.println(“Thread ” + T1 +
“suspended ”); Thread.sleep(1000); T1.t.resume(); System.out.println
(“Thread ” + T1 + “resumed ”); T2.t.suspend(); System.out.println
(“Thread ” + T2 + “suspended ”); Thread.sleep(1000); T2.t.resume();
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
Try
{
T1.t.join(); T2.t.join();
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
System.out.println(“Finishing main thread ”);
}
}
The threads can communicate between each other by passing
some messages. When multiple threads are concurrently executed, a thread can be
suspended. One thread can suspend another thread. To do so, the thread must
invoke suspend() method of thread class. Once suspended, the threads can be
resumed also. The suspended thread can be resumed with the help of another
thread by invoking resume() method. Similarly, the execution of the thread can
be forcibly terminated by another thread with the help of stop() function.
However, all these functions are deprecated in Java 2. Whenever a thread is
suspended, it may lead to a serious problem. If a thread is using a resource
and another thread is waiting for that thread to release the resource, then if
the thread which has currently acquired the resource is suspended, the resource
cannot be released. It may lead to a dead lock situation. Similarly, the
resume() method is also ignored in Java 2 though it does not lead to any
problem, but still it cannot be used without suspend() method. In Java 2, there
are some other methods which when used can suspend or resume a thread. Those
are wait(), notify() and notifyall(). These methods must be called through the
extending class or implementing class. When the wait() method is invoked, then
the thread must release the resources and suspend temporarily. The notify()
method is used to restart the threads which are in waiting state (the first one
in queue). The notifyall() method will awake all the threads that have been in
the waiting state. The same program can be written as shown below without using
the suspend() and resume() method.
Listing 6
class myThread implements Runnable
{
String threadName;
Thread t;
public myThread(String name)
{
threadName = name;
t = new Thread(threadName);
flag = false;
System.out.println(“The value of name: ” + name);
t.start();
}
public void mySuspend()
{
flag = true;
}
public void myResume()
{
flag = false;
t.notify();
}
public void run()
{
try
{
for (int i = 15; i >= 1; i--)
{
System.out.println(“Thread ” + t + “Count: ” + i);
t.sleep(200);
while (flag)
{
t.wait();
}
}
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
System.out.println("Terminating”+t);
}
}
class demo
{
public static void main(String a[])
{
myThread T1 = new myThread(“First ”); myThread T2 = new myThread(“Second ”)
; Try
{
Thread.sleep(1000); T1.mySuspend(); System.out.println(“Thread ” + T1 +
“suspended ”); Thread.sleep(1000); T1.myResume();
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
Try
{
T1.t.join(); T2.t.join();
}
catch (InterruptedException e)
{
System.out.println("Thread Interrupted”);
}
System.out.println(“Finishing main thread ”);
}
}