my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Sychronized behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Sychronized behaviour" Watch "Sychronized behaviour" New topic
Author

Sychronized behaviour

Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
Hi,



In this case if I create two instances of ThreadTest and call the method start().

The output of this code was :


My question is if the first thread calls goToWait() method then it goes to wait state i.e call to wait method does not return.

As the method is synchronised it allows only one thread to call goToWait() method. So concept wise when the first thread is in the wait mode at(// 1) then it should not allow any other thread to enter goToWait() method.

But in this case we see both the threads are in wait state. I think only one thread should be in wait state.

Could any one explain why such behaviour.

Regards
Atul
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The wait() method releases the lock before going into the "wait state," so that in fact it is entirely possible for two methods to be waiting at the same time. If wait() didn't release the lock, then only one thread could ever be waiting for a given monitor, right?

wait() won't return without re-acquiring the same lock.
[ February 01, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60
Thanks Ernest Friedman-Hill

Now the things are clear.
[ February 01, 2005: Message edited by: Atul Prabhu ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sychronized behaviour
 
Similar Threads
wait+ interruptedException
Thread functioning help
threads
synchronize
Problem with wait() / notify()