This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Who is getting locked?

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I have a conceptual doubt about threads. If I have an object that has the method that follows:



I'd like to confirm that the object whose method is synchronized is the object that has been locked.
So the instance variables such as acct who is a reference to an object isn't locked.

Am I right?

Thanks
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We don't really refer to anything as "being locked." Rather, we say that we obtain a lock. But if by "locking X" you mean "obtaining X's lock", then, yes, you're correct. The object whose lock we obtain is the one whose synchronized method we are executing.



The above methods are equivalent. Both obtain the lock for the "this" object and hold it for the entire body of the method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!