October 03, 2014

Simple Java Thread Deadlock programs using simple core concepts.

Write a deadlock program using object monitor lock in java :

Write a simple java deadlock program using Thread join() method


 public class Test  
 {  
      public static void main(String[] args)   
      {  
           Thread appThread = new AppThread(Thread.currentThread());  
           // appThread.setUncaughtExceptionHandler(new AppExceptionHandler());  
           appThread.start();  
           try {  
                appThread.join();  
                System.out.println(appThread.isAlive());  
           }  
           catch(InterruptedException ex) {  
                ex.printStackTrace();  
           }  
      }  
 }  
 class AppThread extends Thread  
 {  
      Thread t = null;  
      public AppThread(Thread t)   
      {  
           this.t = t;  
      }  
      public void run()  
      {  
           System.out.println("AppThreadRunning.");  
           try {  
                System.out.println("About to go deadlock state.");  
                t.join(1000*5);  
                System.out.println("I'm out from deadlock state");  
           }  
           catch(Exception e) {  
                e.printStackTrace();  
           }  
      }  
 }  

No comments :

Post a Comment