• 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

Marcus Syncronization Q

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


The Output is : Compilation and repeated output of 100 agree but one of the comments provided is as follows Although bContinue does get set to true, the setPrice call is blocked because setPrice and run are both synchronized - (I am new to java and programming ) but would like to understand where is the slave class referring to the setPrice method , does a system.out.println(iPrice) within the while loop automatically call the setPrice method thereby preventing the method call s.setPrice(200) in the Master class from setting the price???
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm surprised that this code even works at all. Shouldn't there be a complier error because the public void run() method is missing? I thought you couldn't add the modifier synchronized to an overriding method.
 
Win jones
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erin, The code does compile (It compiles and gives a constant output of 100)and synchronized is fine
 
Erin Goettl
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks!
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setPrice() would be called by the go() method in Master, except that execution of go() never gets past t1.start(), which lets run() go into a loop while holding the lock.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

setPrice() would be called by the go() method in Master, except that execution of go() never gets past t1.start(), which lets run() go into a loop while holding the lock.
--------------------------------------------------------------------------------


The main thread will actually go past t1.start() and the while(bContinue==false) loop but will get stuck at the s.setPrice(200) because thread t1 holds the lock on Slave object 's' while executing run method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic