>

Saturday, December 15, 2012

Thread Part II - Stop the Thread and MultiThreading in java


Thread Part II
Stop the Thread and MultiThreading in java

Every thread that has been executed will be stop after every code in run() is finished executed, method will be automatic stop, but it doesn’t mean we can’t stop thread every time we want, we can stop thread using method stop(), but this method not recommended because the method is deprecated, the best thing that we can do by using boolean variable for controlling looping process.
Let’s see the example,

public class MyThreadStop extends Thread {
   private boolean finish = false;

   public void run() {
      int counter = 0;
      System.out.println("the child thread is executed...");

      while (!finish) {
        System.out.print((++counter) + " ");
       }
       System.out.println("the child thread is ended ");
      } 
// method as flag trigger to stop looping, once this method is
// invoke
   public void stopThread() {
    finish = true;
   }
}

No matter what in method run looping or not, as long as we give flag to stop the method run(), we can stop our thread as we see the code bellow

public class MyThreadStopDemo {

public static void main(String[]args)throws InterruptedException{
  
  System.out.println("the main thread is executed...");
  MyThreadStop myThreadStop = new MyThreadStop();
  myThreadStop.start();
  Thread.sleep(25);
 // triggering method to stop child thread
  myThreadStop.stopThread();
  System.out.println("\nEnd main Thread ");
  
  }

}


for code above we see that we didn’t called stop() method belong to the thread we just call a method stop contain a flag for stopping process in run() method.

in java program we need more than one child thread, program that contain thread more than one child thread called multithreaded. To prove java can handle multithreading we create two threads and invoke the two threads in demo class.

First thread :
public class MyThreadOne extends Thread{
   public void run(){
     try {
       for (int i = 0; i <10 ; i++) {
            System.out.println("First thread : "+(i+1));
            if( i!= 9){
               sleep(1000);
             }else{
              System.out.println("First thread finish....\n");
              }
            }
    }catch (InterruptedException e) {
      e.printStackTrace();
    }
   }
}

second thread :

public class MyThreadTwo extends Thread{
   public void run(){
     try {
       for (int i = 0; i < 5 ; i++) {
           System.out.println("Second thread :"+(i+1));
           if( i!= 4){
             sleep(1000);
             }else{
             System.out.println("Second thread finish....\n");
            }
           }
     } catch (InterruptedException e) {
       e.printStackTrace();
      }
    }
}

now we will call both thread
public class MutipleThreadDemo {

   public static void main(String[] args) {
    MyThreadOne myThreadOne = new MyThreadOne();
    myThreadOne.start();
    MyThreadTwo myThreadTwo = new MyThreadTwo();
    myThreadTwo.start();
   }
}


and if we execute demo class the result as shown bellow


First thread : 1
Second thread : 1
Second thread : 2
First thread : 2
First thread : 3
Second thread : 3
Second thread : 4
First thread : 4
First thread : 5
Second thread : 5
Second thread finish....

First thread : 6
First thread : 7
First thread : 8
First thread : 9
First thread : 10
First thread finish....

 
we see the result the first five line has same value it showed thread were executed together, after the sixth second running, second thread finished. Because only five looping steps for second thread. as the result above java has multithreading feature.

No comments:

Post a Comment