All Thread locks be unlocked with wait???

Hi! I’m just about fixing the synch for a Demo but I do have some doubts about Thread locks. Be one Thread T having N objects locked by some previous synchronized locks, then an Object O calls its own wait() method at some point in T.

a) Will T relinquish the N previous locks for ANYONE of the objects declared as synchronized ? YES/NO
b) Will T relinquish the X previous locks for ONE of the objects declared as synchronized, that be O ? YES/NO

??? ??? ??? ( I’ve just been reading the last paragraph of this document from Sun which seems to get the answer for a) and b) )

a) NO
b) NO

if T1 locks object A,B,C,D and waits on C, T2 can now aquire C, but not A,B,D.

         class State
         {
            volatile int value = 0;
         }

         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 (true)
                           {
                              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();

         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;
         }
         catch (InterruptedException exc)
         {
            exc.printStackTrace();
         }

Output:

>> 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!

a sidenote: Give decent names to mutexes, always lock on the lowest possible level. It must be a very very ‘funky’ demo considering the amount of locks involved

Yeah, I rarely have to lock on two mutexes, I’ve never locked on three or more… but I guess the question was about the theory behind locks, his N could have been 2 anyway.

b) answer is NO, surely. But as for a) object O is Added to the Thread wait set, and a) answer be NO would interfere with this statement from the attached document I’m quoting : [quote=17.14 Wait Sets and Notification]The method wait should be called for an object only when the current thread (call it T) has already locked the object’s lock. Suppose that thread T has in fact performed N lock actions that have not been matched by unlock actions. The wait method then adds the current thread to the wait set for the object, disables the current thread for thread scheduling purposes, and performs N unlock actions to relinquish the lock.
[/quote]
, wouldn’t it?

You first have to tell me whether O is on of the N objects.

Further, X (in your first post) is undefined, and objects are never ‘declared synchronized’.

It’s very hard in complex topics to understand what somebody else means when the terminology is skewy.

If you have run my test-app, you see that T1 only relinquishes C, not A,B,D.

About that quote from the language-spec: I don’t fully understand why it says it does N unlocks, but it’s clear that it ends with “to relinquish the lock” (not “locks”), so C is relinquished, by N unlock actions (which I cannot explain).

HTH :-\

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!