| Refresh | Home EGTry.com

send and handle interrupt signal among threads


Interrupt.java

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread;

public class Interrupt extends Thread {

  public static void main(String[] args) {

    Interrupt thread1=new Interrupt();
    thread1.start();
    thread1.interrupt(); //send InterruptedException 

  }
  
  @Override
  public void run() {
		System.out.println("Thread started");
		try {
			Thread.sleep(20*1000); //suspect for 20 seconds
		} catch (InterruptedException e) {
			System.out.println("interrupted");
		}
		System.out.println("Child Thread: "+Thread.currentThread().getId());
  }
}



Output

Thread started
interrupted
Child Thread: 8