Created
April 8, 2014 01:54
-
-
Save cassc/10082220 to your computer and use it in GitHub Desktop.
Java interrupt medthod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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