• 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

Thread Lockdown

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

All:
Consider this code:

A given run of this code can produce the results "11a 10a 11b 10b".

OK. I understand that the locked() method is synchronized so that only one thread at a time can use it. I also see that the run() method is NOT synchronized. I don't understand how this result is possible because while the run() method is NOT synchronized the locked() method is. So lets see, one of the threads starts and gets into the run() method first and then invokes the locked() method, thereby getting the lock on the object. That is supposed to prevent the other thread from executing the same method at the same time, correct? Which is what we want because both threads are using the same runnable. But, obviously that is not happening here. Could the JVM be swapping out the two threads and that's why I am getting this kind of result occasionally? If not, please help me understand how this result occurs.

Thanks to all for your time and consultation.
Gary
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So lets see, one of the threads starts and gets into the run() method first and then invokes the locked() method, thereby getting the lock on the object



"locked" is an instance method(and more appropriately, not a static method). The 2 threads are not sharing the same lock object, there are 2 different lock objects(new LockDown()).
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OOPS... Looks like I gotta get better glasses...

Thank you for your time.
Gary
 
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

Please QuoteYourSources.

Henry
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry.

This question came from the ePractice: Java Standard Edition 6 Programmer Certified Professional Exam from Oracle.
 
reply
    Bookmark Topic Watch Topic
  • New Topic