Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

help regarding synchronized methods

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can't understand the output of the following code:


Output (on my machine):
t1: in run
t1: about to sleep
t2: in run
t2: about to sleep
t1: dying
t2: dying


I thought (and still think) the output would be:
t1: in run
t1: about to sleep
t2: in run
t1: dying
t2: about to sleep
t2: dying

How is "t2: about to sleep" printed before "t1: dying"? Isn't "t1" supposed to die before "t2" can get a lock on "synchronized void meth()"?

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

There's no connection between mt1's and mt2's meth method. It's not a static method. synchronized void meth() guarantess only that the meth calls for the given instance will be synchronized.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronized method doesnt allow another thread enter to the method through the same object. In your case t1 and t2 enters the method meth() through different objects mt1 and mt2. So the lock doesnt prevent t2 entering meth() while t1 executing meth() or vice versa.

 
Venu Chakravorty
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks mates.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic