I am little confused about how synchronized works in Java for resource allocation. When I am given with the following code in Java:
import java.util.concurrent.Semaphore;
public class Deadlock {
public Deadlock () {
Semaphore mutex[] = new Semaphore[4];
for (int i = 0; i < 4; i++)
mutex[i] = new Semaphore(1);
A threadA = new A(mutex);
B threadB = new B(mutex);
C threadC = new C(mutex);
threadA.start();
threadB.start();
threadC.start();
}
private class A extends Thread {
private Semaphore[] resource;
public A(Semaphore[] m) {
resource = m;
}
public void run() {
System.out.println("A started");
synchronized( resouce[1] ) {
System.out.println("A locks rsc 1");
synchronized (resource[0]) {
System.out.println("A locks rsc 0");
}
}
System.out.println("A finished");
}
}
private class B extends Thread {
private Semaphore[] resource;
public B(Semaphore[] m) {
resource = m;
}
public void run() {
System.out.println("B started");
synchronized( resouce[3] ) {
System.out.println("B locks rsc 3");
synchronized (resource[0]) {
System.out.println("B locks rsc 0");
synchronized (resource[2]) {
System.out.println("B locks rsc 2");
}
}
}
System.out.println("B finished");
}
}
private class C extends Thread {
private Semaphore[] resource;
public C(Semaphore[] m) {
resource = m;
}
public void run() {
System.out.println("C started");
synchronized( resouce[2] ) {
System.out.println("C locks rsc 2");
synchronized (resource[1]) {
System.out.println("C locks rsc 1");
}
}
System.out.println("C finished");
}
}
}
To my understanding, when thread A starts, thread A has lock on resource 1 and resource 0. Therefore, when Thread B starts, it will acquire lock on resource 3, but will be waiting on resource 0 to be released from Thread A. Since Thread B does not have a lock on resource 0, it will not be able to be wait on resource 2. When Thread C starts, it will have a lock on Resource 2, but also waiting on Resource 1 to be released from Thread A.
So, when its drawn out as resource allocation graph, it will look like the following:
Here, where node from P to R refers to Process requesting for the resource. and the node from R to P means that the process has lock on the resource.
Am I understanding this correctly? Any help is welcome. Thank you.