thread_inturrpted.htm
  • One thread may interrupt another thread from the point of remote class where the thread was created.
    • The set of functions were used in the examples shown below.
      • at remote class: thread_object.interruopt();
      • in action class: Thread.currentThread().isInterrupted()
  • When interrupted a thread that had been in sleep mode, catch block in try-catch exception set, will display the message "java.lang.InterruptedException: sleep interrupted" , the function Thread.currentThread().isInterrupted() will report that the thread was stopped.

Some functions like destroy, suspended etc, are deprecated due to some safe-execution issues, the details can be read at this location. http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

import java.util.*;
import java.io.*;
//javac test_interrupt.java
public class test_interrupt
{
public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.println("-->Type exit during run time to intrrupt the thread<--");
System.out.print("type name as worker : ");
String str = br.readLine();
String s = str + " : "+ n1++;
s = str + " : "+ n1++;
s = str + " : "+ n1++;
Thread th = new Thread(new thread_interrupt(s)) ;
th.start();
Scanner scan = new Scanner(System.in);
while(!s.equals("exit"))
{ s= scan.next();
th.interrupt();
System.out.println("You entered " + s);
}

}
}
class thread_interrupt implements Runnable
{
public String str;
private int round = 0;
public thread_interrupt(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println("-- round: "+ round + " -- " + Thread.currentThread().getName());
//for (int i = 1; i <4 ; i++){
while (!Thread.currentThread().isInterrupted()){
System.out.println("that run : " + round+ " sleep " + str+ " <>" + Thread.currentThread().getId());
try{
Thread.sleep(1000);

}
catch (InterruptedException e) {
System.out.println(e);
break;
}
System.out.println("that run : " + round + " work " + str+ " <>" + Thread.currentThread().getId());
}
}
}

Eclipse version

import java.util.*;
import java.io.*;
public class test
{
public static int n1 = 1;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("-->Welcome to thread building<--");
System.out.print("type name as worker : ");
String str = br.readLine();
String s = str + " : "+ n1++;
s = str + " : "+ n1++;
s = str + " : "+ n1++;
Thread th = new Thread(new thread_that(s)) ;
th.start();
Scanner scan = new Scanner(System.in);
while(!s.equals("exit"))
{ s= scan.next();
th.interrupt();
System.out.println("You entered " + s);
}
}
}
class thread_that implements Runnable
{
public String str;
private int round = 1;
public thread_that(String str)
{ System.out.println("\tthat consturctor");
this.str = str;
}
public void run()
{
System.out.println("-- round: "+ round + " -- " + Thread.currentThread().getName());
//for (int i = 1; i <4 ; i++){
while (!Thread.currentThread().isInterrupted()){
round++;
System.out.println("that run : " + round + " sleep " + str+ " <>" + Thread.currentThread().getId());
try{
Thread.sleep(2000);
}
catch (InterruptedException e) {
System.out.println(e);
break;
}
System.out.println("that run : " + round + " work " + str+ " <>" + Thread.currentThread().getId());
}
}
}