• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() question from kb book

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Am gonna tell what I understood from the following code. Can anyone tell me if am wrong?
1).At any point of time, main thread and thread b can be in their respective synchronized blocks at the same time (coz even though both the threads have their lock on object b, both are methods of different classes). 2).once the main thread encounters the wait method, it comes out of the synchronized block and then whatever is left in the main method is executed only after it is notified right? It does not go into the synchronized block again right?

Am I correct?



Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1).At any point of time, main thread and thread b can be in their respective synchronized blocks at the same time (coz even though both the threads have their lock on object b, both are methods of different classes).



Don't know what you mean by this, but yes, both can be within the block -- but only one is holding the lock. The wait() method releases the lock, which allows another thread to acquire and enter the sync block.

2).once the main thread encounters the wait method, it comes out of the synchronized block



No. The wait() method releases the lock. This isn't exactly "comes out of the synchronized block".

and then whatever is left in the main method is executed only after it is notified right? It does not go into the synchronized block again right?



When a notification occurs, the wait() method will be woken up.... but it will need to reacquire the locks again before it can return from the wait() method.

Henry
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okie, If at any particular point of time only one thread has the lock, then what if thread b enters the run method first and then the main method enters the synchronized block on which the wait method is called next? Then the program will just freeze right? I think am gettin lil confused here. In the above program, is it the case that the main method always enters the synchronized code first and only after that the thread b enters the synchronized block?

Thanks.
[ September 19, 2008: Message edited by: Arjun Reddy ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If thread b enters first, then the main thread will block at the synchronization block waiting for the lock. Thread b will then do the loop, and sent the notification.

Since the main thread has not called wait yet, then the notification is lost. Once thread b exits the synchronization lock, then the main thread enters and calls the wait method.

Henry
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

So when the main method enters the synchronized block and calls the wait(), since no notification on the object is sent, program will just freeze right?

Thanks.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right. In that situation, the program will probably freeze until you kill the process.

It's technically possible for the waiting thread to wake up without a notification (this is called a spurious wake-up) but in general, it's unlikely.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic