Saturday 18 February 2017

MultiThreaded Programming in Java ( PART - III )



                                                           
MultiThreaded Programming in Java ( PART - III )



 Suspending , Resuming and Stopping Threads

Suspending a thread is a simple mater. Also Resuming it afterwards is simple matter. Earlier , a program used suspend ( ) and resume ( ) , which are methods defined by thread  , to restart the execution of thread. 

The Thread class can also define a method called stop ( ) that stops a thread. Once a thread has been stopped , it cannot be restarted using resume ( ).


Now , in latest version methods of Java , the suspend ( ) method is deprecated. This was done because suspend ( ) can sometimes cause serious system failures.

If a thread has obtained locks on critical data structure  and if it is suspended at that point , those locks are not relinquished , other threads may be waiting for resources which can lead to deadlock.

The resume ( ) method has also been deprecated  as it cannot be used without suspend ( ) method as counterpart.

The stop ( )  method of thread class has also been deprecated. It was done because this method can sometimes can cause serious system failures.

If thread is writing to a critically important data structure and has completed only part of its changes. If the thread is stopped at that point , that data structure might be left in a corrupted state.

Since we cannot  use suspend ( ) , resume ( ) and stop ( ) methods in threads , there occurs a serious issue to pause , restart and terminate a thread. In the latest java software , the thread must be designed so that the run ( ) method periodically checks to determine whether that thread should suspend , resume or stop its own execution.

This is done by establishing a flag variable that indicates the execution state of thread. 

If the flag is set to "running" , the run ( ) method must continue to let the thread execute.  If the variable is set to "suspend" , the thread must pause. If it is set to "stop" , the thread must terminate.

Ex-  // Suspending and resuming a thread for java.

  class NewThread implements Runnable
   {
       String name ;
        Thread t;
       boolean suspendFlag ;

       NewThread ( String threadname )
      {
           name=threadname ;
           t= new Thread ( this ,name);
           System.out.println (" New Thread : " + t);
            suspendFlag = false ;
              t.start ( );
        }

  public void run ( )
  {
      try 
     {
          for ( int  i=15 ; i>0 ; i++)
         {
              System.out.println ( name + " : " + i ) ;
               Thread.sleep (200);
                synchronized (this)
                 {
                      while (suspendFlag)
                       {
                              wait ( );
                        }
                  }
             }
        }
        catch ( InterruptedException e)
       {
            System.out.println ( name + "interupted. ");
        }
          System.out.println ( name + "exiting. ");
     }

        void mySuspend ( )
       {
               suspendFlag =true ;
        }
   
          synchronized void myresume (  )
         { 
              suspendFlag = false ;
               notify ( );
        }
  }

  class SuspendResume 
 {
         public static void main ( String [] args )
    {
         NewThread ob1 = new NewThread ("one");
         NewThread ob2 = new NewThread ("Two");

        try
         {
                Thread.sleep (1000);
                 ob1.mysuspend ();
                System.out.println ( "Suspending thread one ");
                 Thread.sleep (1000);
                 ob1.myresume( );
                System.out.println ( " Resuming Thread one ");
                 ob2.mysuspend ( );
                System.out.println ( " Suspending thread one " );
                Thread.sleep(1000);
                 ob2.myresume ( );
                System.out.println ( " Resuming thread two " );
           }
         catch ( InterruptedException e)
          {
               System.out.println ( " Main thread Interrupted ") ;
          }
   
      try
       {
            System.out.println (" Waiting for threads to finish " );
            ob1.t.join ( );
            ob2.t.join ( );
       }
       catch ( InterruptedException e)
       {
            System.out.println ( " Main Thread Interrupted " );
       }
    
           System.out.println ( " Main thread exiting " );
    }
}

  Output:

  New Thread : Thread [ one ,5,main ]
  one : 15
  New Thread : Thread [ Two,5,main]
 two : 15
 one : 14
 two : 14
 one : 13
 two : 13
 one : 12
 two : 12
 one : 11
 two : 11
 suspending thread one
 two : 10
 two : 9
 two : 8
 two : 7
 two : 6
Resuming thread one
Suspending thread Two
 one :10
 one : 9 
 one : 8
 one : 7
 one : 6
Resuming thread two
Waiting for threads to finish.
 two : 5
 one : 5
 two : 4
 one : 4
 two : 3
 one : 3
 two : 2
 one : 2
 two : 1
 one : 1
 Two exiting.
 One exiting.
 Main Thread exiting. 

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *