Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Monitor and Instance Data ?  RSS feed

 
satyendra gurjar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a thread acquire the monitor on an object can another thread still access instance/static data member of the same object?

If a thread calls a() method on a reference to an object of class Q7ed5, say obj, then can another thread access instance data i using same object obj.
Thanks.
satyendra
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If you don't want this to happen, declare i to be private, and make sure that all accesses to it are in synchronized methods or synchronized blocks.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello gurjar
I'd add that static fields aren't protected by the monitor of any instance of the class, but by the lock corresponding to Class object that represents the class in which they were declared.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!