Thursday, April 21, 2011

write a program in java for thread interrupt


class MyThread1 implements Runnable {

  public void run() {
    try {
      Thread.sleep(4000);
    } catch (Exception exc) {
      System.out.println(Thread.currentThread().getName()
          + " is interrupted.");
    }
    System.out.println(Thread.currentThread().getName() + " is exiting.");
  }
}

public class ThreadInterrupt1 {
  public static void main(String args[]) throws Exception {
    Thread th1 = new Thread(new MyThread1(), "First Thread");
    Thread th2 = new Thread(new MyThread1(), "Second Thread");
    th1.start();
    Thread.sleep(2000);
    th1.interrupt();
    th2.start();
    Thread.sleep(5000);
    th2.interrupt();
  }
}

0 comments:

Post a Comment

Post Your comments,Views and thoughts Here, Give Us Time To Respond Your Queries