Thread DeadLock in java

Thread DeadLock in java
SOURCE : Deadlock.java


public class Deadlock {

public static void main(String[] args) {

final String r1 = "resource1";
final String r2 = "resource2";

Thread t1 = new Thread() {

public void run() {

synchronized (r1) {

System.out.println(" i m in resource 1 , waiting for 2 ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

synchronized (r2) {
System.out.println(" i m in resource 2 ");
}

}

}
};

Thread t2 = new Thread() {

public void run() {

synchronized (r1) {

System.out.println(" i m in resource 2 , waiting for 1 ");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

synchronized (r2) {
System.out.println(" i m in resource 1 ");
}

}

}

};
t1.start();
t2.start();
}

}


INPUT





OUTPUT

i m in resource 1 , waiting for 2
i m in resource 2
i m in resource 2 , waiting for 1
i m in resource 1






Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment