• 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

Sychronized behaviour

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Atul Prabhu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill

Now the things are clear.
[ February 01, 2005: Message edited by: Atul Prabhu ]
reply
    Bookmark Topic Watch Topic
  • New Topic