Ok I tested the code and obviously you’re correct. My question was about how far the locks were relinquished by the Thread. For the answer becomes in that case b) YES while a) is NO. let’s see the test case now!
Thereafter, the “test unit” proved that it can handle one Object-locking process at a time, I mean C-wait() called on a multiple lock on A,B,C,D does give the unlocking process to N locks on C only, where A,B,D stay locked.
I’d say then to launch a new Thread if I wanted to have the lock released for A,B,D. But this is your code I modified the end to get it exited properly, as you brought the good answers. Let’s see the new output :
java Main
>> GOING TO STATE 3 (T locks a,b,c,d, waits on c)
T blocked on a
T aquired a
T blocked on b
T aquired b
T blocked on c
T aquired c
T blocked on d
T aquired d
T [waits] on c
>> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)
T:grabber -> blocked on c...
T:grabber -> aquired c
T:grabber -> blocked on d... darn it!
T-notify's changing STATE : 5 (Main will wait 10s and exit.)
T-notify's notifying T locks on c.
T [woke] on c
T:grabber -> aquired d
T-notify acquired d.
Main is waiting on c for 10s then exits.
..........
And the adapted code :
class State
{
volatile int value = 0;
}
class Main {
public static void main(String[] args) {
final State state = new State();
final Object a = "a";
final Object b = "b";
final Object c = "c";
final Object d = "d";
new Thread()
{
public void run()
{
while (state.value != 3)
continue;
System.out.println("T blocked on " + a);
synchronized (a)
{
System.out.println("T aquired " + a);
System.out.println("T blocked on " + b);
synchronized (b)
{
System.out.println("T aquired " + b);
System.out.println("T blocked on " + c);
synchronized (c)
{
System.out.println("T aquired " + c);
System.out.println("T blocked on " + d);
synchronized (d)
{
System.out.println("T aquired " + d);
while (state.value < 5)
{
try
{
System.out.println("T [waits] on " + c);
c.wait();
System.out.println("T [woke] on " + c);
}
catch (InterruptedException exc)
{
exc.printStackTrace();
}
}
}
}
}
}
}
}.start();
new Thread()
{
public void run()
{
while (state.value != 4)
continue;
System.out.println("T:grabber -> blocked on " + c + "...");
synchronized (c)
{
System.out.println("T:grabber -> aquired " + c);
}
System.out.println("T:grabber -> blocked on " + d + "... darn it!");
synchronized (d)
{
System.out.println("T:grabber -> aquired " + d);
}
}
}.start();
new Thread(new Runnable() { public void run() {
while (state.value != 4)
continue;
System.out.println("T-notify's changing STATE : 5 (Main will wait 10s and exit.)");
state.value = 5;
synchronized(c) {
System.out.println("T-notify's notifying T locks on c.");
c.notify();
}
synchronized(d) {
System.out.println("T-notify acquired d.");
}
}}).start();
try
{
System.out.println(">> GOING TO STATE 3 (T locks a,b,c,d, waits on c)");
state.value = 3;
Thread.sleep(1000);
System.out.println(">> GOING TO STATE 4 (T:grabber lock c, locks d ->> FAILS)");
state.value = 4;
while(state.value < 5);
synchronized(c) {
System.out.println("Main is waiting on c for 10s then exits. ");
long time = System.currentTimeMillis();
long elapsed = 0;
do {
if(elapsed % 1000 >= 0 && elapsed % 1000 <= 10)
System.out.print(".");
c.wait(10);
}while((elapsed = System.currentTimeMillis() - time) < 10000);
}
}
catch (InterruptedException exc)
{
exc.printStackTrace();
} finally {
System.exit(0);
}
}
}
Thank you for your help!