Saturday, 25 January 2014

Suspending,Resuming,and Stoping Thread using java 2

modern way of suspending,resuming,stoping:...

The Modern Way of Suspending, Resuming, and Stopping Threads


The Modern Way of Suspending, Resuming, and Stopping Threads While the suspend(),resume( ), andstop( ) methods defined by Thread seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs.

Suspending


The suspend( ) method of theThread class was deprecated by Java 2 several years ago. This was done because suspend( ) can sometimes cause serious system failures. Assume that a
thread has obtained locks on critical data structures. If that thread is suspended atthat point, those locks are not relinquished. Other threads that may be waiting for those resources can be deadlocked.

Resuming


The resume( ) method is also deprecated. It does not cause problems, but cannot be used without the suspend( ) method as its counterpart.

Stopping 


The stop( ) method of theThread class, too, was deprecated by Java 2. This was done because this method can sometimes cause serious system failures. Assume that a thread is writing to a critically important data structure and has completed only part of its changes.If that thread is stopped at that point, that data structure might be left in a corrupted state.

Because you can’t now use the suspend( ),resume( ), or stop( ) methods to control a thread, you might be thinking that no way exists to pause, restart, or terminate a thread.But, fortunately, this is not true. Instead, a thread must be designed so that the run( ) method periodically checks to determine whether that thread should suspend, resume, or stop its own execution. Typically, this is accomplished by establishing a flag variable that indicates the execution state of the thread. As long as this flag is set to “running,” the run( ) method must continue to let the thread execute. If this variable is set to “suspend,” the thread must pause. If it is set to “stop,” the thread must terminate. 


Example:Suspending, resuming, and stopping a thread.

// Suspending, resuming, and stopping a thread. 
 using System; 
using System.Threading;  
class MyThread 
{  
public Thread thrd;  
 public MyThread(string name)
 {  
thrd = new Thread(new ThreadStart(this.run)); 
thrd.Name = name; 
thrd.Start();  
}  
// This is the entry point for thread.  
 void run() 
{
Console.WriteLine(thrd.Name + " starting."); 
for(int i = 1; i <= 1000; i++)
{  
Console.Write(i + " ");  
if((i%10)==0) 
Console.WriteLine(); 
Thread.Sleep(250); 
 } 
Console.WriteLine(thrd.Name + " exiting.");  
}  
public class SuspendResumeStop 
{  
public static void Main()
 {  
MyThread mt1 = new MyThread("My Thread");  
Thread.Sleep(1000); // let child thread start executing 
mt1.thrd.Suspend();  
Console.WriteLine("Suspending thread.");  
Thread.Sleep(1000); 
mt1.thrd.Resume();  
Console.WriteLine("Resuming thread.");  
Thread.Sleep(1000); 
mt1.thrd.Suspend();  
Console.WriteLine("Suspending thread.");  
Thread.Sleep(1000); 
mt1.thrd.Resume();  
Console.WriteLine("Resuming thread.");  
Thread.Sleep(1000); 
Console.WriteLine("Stopping thread.");  
mt1.thrd.Abort(); 
mt1.thrd.Join(); // wait for thread to terminate 
Console.WriteLine("Main thread terminating.");  
}  
}

When you run this program,you will see the output shown here:

 My Thread starting.

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

supending thread.
Resuming Thread.

41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 64 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80









































































































No comments :

Post a Comment