Created
August 16, 2014 06:43
-
-
Save LBRapid/fc00a6d324275fe83abe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.concurrent.locks.ReentrantLock; | |
public class Interruptible { | |
public static void main(String[] args) throws InterruptedException { | |
final ReentrantLock l1 = new ReentrantLock(); | |
final ReentrantLock l2 = new ReentrantLock(); | |
Thread t1 = new Thread() { | |
public void run() { | |
try { | |
l1.lockInterruptibly(); | |
Thread.sleep(1000); | |
l2.lockInterruptibly(); | |
} catch (InterruptedException e) { System.out.println("t1 interrupted"); } | |
} | |
}; | |
Thread t2 = new Thread() { | |
public void run() { | |
try { | |
l2.lockInterruptibly(); | |
Thread.sleep(1000); | |
l1.lockInterruptibly(); | |
} catch (InterruptedException e) { System.out.println("t2 interrupted"); } | |
} | |
}; | |
t1.start(); t2.start(); | |
Thread.sleep(2000); | |
t1.interrupt(); t2.interrupt(); | |
t1.join(); t2.join(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment