Back
Close

Computing with Data

elgeish
31.1K views
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
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants