undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: Starvation Next: Deadlocks - Part II

Deadlocks - Part I

Here's another example for issues that can occur due to lock objects being visible outside the enclosing class where they belong:

class A implements Runnable {
public void run() {
synchronized (A.class) {
System.out.println("A has A.class and now wants B.class");
try {
Thread.sleep(500); // simulates some work being done
} catch (InterruptedException ex) {
System.out.println(ex);
}
synchronized (B.class) {
System.out.println("A got both locks and made progress");
}
}
}
}
class B implements Runnable {
public void run() {
synchronized (B.class) {
System.out.println("B has B.class and now wants A.class");
synchronized (A.class) {
System.out.println("B got both locks and made progress");
}
}
}
}
public class Main {
public static void main(String args[]) throws Exception {
new Thread(new A()).start();
new Thread(new B()).start();
Thread.sleep(1000); // ad-hoc wait till threads finish
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content