Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Semaphore;
- public class Deadlocker {
- public static void main(String[] args) {
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- try {
- one();
- System.out.println("one");
- } catch (InterruptedException e) {}
- }
- }
- }).start();
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- try {
- two();
- System.out.println("two");
- } catch (InterruptedException e) {}
- }
- }
- }).start();
- }
- private static final Semaphore foo = new Semaphore(1);
- private static final Semaphore bar = new Semaphore(1);
- private static void one() throws InterruptedException {
- foo.acquire();
- bar.acquire();
- bar.release();
- foo.release();
- }
- private static void two() throws InterruptedException {
- bar.acquire();
- foo.acquire();
- foo.release();
- bar.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement