Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

synchronization question

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't quite understand the answer to this question:

This code produces repeated output of 100. The question is why doesn't it display any values of 200. The answer states that although bContinue does get set to true, the setPrice call is blocked because setPrice and run are both synchronized.

I think that I am still missing something here. Are the setPrice and run methods somehow tied together?? So since the run method is locked, then the setPrice method is also locked??

Why is

(Please use tags around formatted Java code)
[ January 24, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that by default you have a thread called main. The main thread here will create Master object m and will run m.go().
Once inside m.go() a new thread t1 is created. Now we have main thread which is at the while(bContinue==false) statement and t1 thread running the run method of Slave.
The t1 thread operates on the Slave object s. Since the run method of slave is synchronized therefore the thread t1 has a lock on object s and main thread cannot run any other synchronized methods of s until t1 is done with the run method.
Since run method has an infite loop t1 will never release the lock of s and main will forever get stuck at s.setPrice(200) line. That's why you keep seeing repeated 100 but no 200.

Hope this helps.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All synchronized methods in one instance share a lock on that instance. A thread can't enter either setPrice() or run() while another thread is executing in either method.

Since bContinue is set to true when thread t1 is already in run(), thread main has no chance to try calling setPrice() until thread t1 holds the lock on Slave instance s. As Harsh pointed out, Thread t1 loops, continuously printing "100", and never releases the lock.
 
reply
    Bookmark Topic Watch Topic
  • New Topic