Skip to content

Instantly share code, notes, and snippets.

@cassc
Created April 8, 2014 01:54
Show Gist options
  • Save cassc/10082220 to your computer and use it in GitHub Desktop.
Save cassc/10082220 to your computer and use it in GitHub Desktop.
Java interrupt medthod
/*
* Demonstrate thread.interrupt() method;
*/
package test.another;
import java.util.concurrent.TimeUnit;
public class TestInterrupt {
public static void main(String[] args) {
Thread t1,t2;
t1 = new ThreadOne("t1",null,6);
t2 = new ThreadOne("t2",t1,3);
t1.start();
t2.start();
}
static class ThreadOne extends Thread{
private Thread other;
private int sleepTime;
public ThreadOne(String name, Thread other, int sleepTime){
this.other=other;
this.setName(name);
this.sleepTime=sleepTime;
}
@Override
public void run(){
System.out.println(this.getName()+" thread starts.");
try {
TimeUnit.SECONDS.sleep(sleepTime);
} catch (InterruptedException e) {
// e.printStackTrace();
}
System.out.println(this.getName()+" awakes.");
if(other!=null)
other.interrupt();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment