Monday, November 5, 2012

Threads in Java

Here is a small demo of how to use threading capabilities in Java

File : ThreadModule.java

package com.giri.thread;

public class ThreadModule implements Runnable {
 
 private String threadName;
 private Thread t;
 private boolean suspendFlag;
 
 
 public Thread getT() {
  return t;
 }

 public void setT(Thread t) {
  this.t = t;
 }

 public String getThreadName() {
  return threadName;
 }

 public void setThreadName(String threadName) {
  this.threadName = threadName;
 }

 public ThreadModule(String name) {
  this.threadName = name;
  t = new Thread(this, name);
  System.out.println("\n ThreadModule() : New Thread : " + t);
  suspendFlag = false;
  t.start();
 }
 
 // this is the entry point for the thread
 @Override
 public void run() {
  
  System.out.println("\n run() block called for thread " + threadName + " ! Running... \n\n");
  try {
   for(int i=1; i<20; i++) {
    System.out.print("\n Value="+i+"["+this.threadName+"]");
    Thread.sleep(800);
    synchronized(this) {
     while(suspendFlag) {
      wait();
     }
    }
   }
  } catch(Exception e) {
   System.out.println("\nError in doing the run() of the thread : " + threadName);
   e.printStackTrace();
  }
  System.out.println("\n Exiting thread: " + threadName + "\n\n");
  
 }
 
 public void mySuspend() {
  System.out.println("\n\n mySuspend() called for thread " + threadName + " !");
  suspendFlag = true;
 }
 
 synchronized void myResume() {
  System.out.println("\n\n myResume() called for thread " + threadName + " !");
  suspendFlag = false;
  notify();
 }
 

}

File : ThreadDemo.java

package com.giri.thread;

public class ThreadDemo {

 public static void main(String[] args) {
  ThreadModule t1 = new ThreadModule(" { First  } ");
  ThreadModule t2 = new ThreadModule(" { Second } ");
  
  try {
   Thread.sleep(5000);
   t1.mySuspend();
   System.out.println("\n [ThreadDemo.java] : Suspending thread: " + t1.getThreadName());
   Thread.sleep(5000);
   t1.myResume();
   System.out.println("\n [ThreadDemo.java] : Resuming thread: " + t1.getThreadName());
   
   t2.mySuspend();
   System.out.println("\n [ThreadDemo.java] : Suspending thread: " + t2.getThreadName());
   Thread.sleep(5000);
   t2.myResume();
   System.out.println("\n [ThreadDemo.java] : Resuming thread: " + t2.getThreadName());
   
  } catch(Exception e) {
   System.out.println("\n Error in the main try {} block !");
   e.printStackTrace();
  }
  
  try {
   System.out.println("\n Waiting for the threads to finish...");
   t1.getT().join();
   t2.getT().join();
  } catch(Exception e) {
   System.out.println("\nError in doing a join() of the threads !");
   e.printStackTrace();
  }
  
  System.out.println("\n Done ! Exiting Main Thread..!");

 }
}

Output


 ThreadModule() : New Thread : Thread[ { First  } ,5,main]

 run() block called for thread  { First  }  ! Running... 



 Value=1[ { First  } ]
 ThreadModule() : New Thread : Thread[ { Second } ,5,main]

 run() block called for thread  { Second }  ! Running... 



 Value=1[ { Second } ]
 Value=2[ { First  } ]
 Value=2[ { Second } ]
 Value=3[ { First  } ]
 Value=3[ { Second } ]
 Value=4[ { First  } ]
 Value=4[ { Second } ]
 Value=5[ { First  } ]
 Value=5[ { Second } ]
 Value=6[ { Second } ]
 Value=6[ { First  } ]
 Value=7[ { First  } ]
 Value=7[ { Second } ]

 mySuspend() called for thread  { First  }  !

 [ThreadDemo.java] : Suspending thread:  { First  } 

 Value=8[ { Second } ]
 Value=9[ { Second } ]
 Value=10[ { Second } ]
 Value=11[ { Second } ]
 Value=12[ { Second } ]
 Value=13[ { Second } ]

 myResume() called for thread  { First  }  !

 [ThreadDemo.java] : Resuming thread:  { First  } 

 Value=8[ { First  } ]

 mySuspend() called for thread  { Second }  !

 [ThreadDemo.java] : Suspending thread:  { Second } 

 Value=9[ { First  } ]
 Value=10[ { First  } ]
 Value=11[ { First  } ]
 Value=12[ { First  } ]
 Value=13[ { First  } ]
 Value=14[ { First  } ]

 myResume() called for thread  { Second }  !

 [ThreadDemo.java] : Resuming thread:  { Second } 

 Value=14[ { Second } ]
 Waiting for the threads to finish...

 Value=15[ { First  } ]
 Value=15[ { Second } ]
 Value=16[ { First  } ]
 Value=16[ { Second } ]
 Value=17[ { First  } ]
 Value=17[ { Second } ]
 Value=18[ { First  } ]
 Value=18[ { Second } ]
 Value=19[ { First  } ]
 Value=19[ { Second } ]
 Exiting thread:  { First  } 



 Exiting thread:  { Second } 



 Done ! Exiting Main Thread..!

No comments :

Post a Comment