• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Threads again

 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Morning folks. I was doing an mock on ExamLab then this question appeared:


It says that the output is: DAEBC. But my question is: Isn't the object s1 locked when the thread starts, by calling synchronized(this) ? How thread main can call stnchronizes(s1) if it has already been locked?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because wait() releases the lock, and that thread only re-acquires the lock when it's woken up (after the main thread releases it).

You can only call wait(), notify() and notifyAll() from within a synchronised block. So wait() would be entirely useless if it didn't release the lock, as no other thread would ever be able to get the lock to call notify().
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Because wait() releases the lock, and that thread only re-acquires the lock when it's woken up (after the main thread releases it).

You can only call wait(), notify() and notifyAll() from within a synchronised block. So wait() would be entirely useless if it didn't release the lock, as no other thread would ever be able to get the lock to call notify().



That's right, I forgot that wait() releases the lock.

Thanks Matthew =D
 
reply
    Bookmark Topic Watch Topic
  • New Topic