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

Why instance variables cannot be marked "synchronized"?  RSS feed

 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, please help me!
All we know instance variables cannot be marked "synchronized" but I don't know why?
Please give me an explanation!
Thanks,
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would it mean exactly for a variable to be synchronized? When a method is synchronized, specific bytecodes are inserted to grab/release a lock; maybe a "synchronized variable" would add the same instructions before any time it was read or written. That would be rather expensive!

In any case, there actually is something like "synchronized" for member variables: it's "volatile." A variable marked "volatile" won't be cached, and is therefore immune from some kinds of thread-related issues. But volatile helps with only a very specific class of problems; it's not a cure-all.
 
Henry Wong
author
Sheriff
Posts: 22832
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All we know instance variables cannot be marked "synchronized" but I don't know why?
Please give me an explanation!


Currently, there is no definition of what would happen to variables that are "synchronized". So, the compiler doesn't allow it, because it wouldn't know what to do with it.

BTW, what would you expect Java to behave with "synchronized variables"?

Henry
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:What would it mean exactly for a variable to be synchronized? When a method is synchronized, specific bytecodes are inserted to grab/release a lock; maybe a "synchronized variable" would add the same instructions before any time it was read or written. That would be rather expensive!

In any case, there actually is something like "synchronized" for member variables: it's "volatile." A variable marked "volatile" won't be cached, and is therefore immune from some kinds of thread-related issues. But volatile helps with only a very specific class of problems; it's not a cure-all.

Henry Wong wrote:
All we know instance variables cannot be marked "synchronized" but I don't know why?
Please give me an explanation!


Currently, there is no definition of what would happen to variables that are "synchronized". So, the compiler doesn't allow it, because it wouldn't know what to do with it.

BTW, what would you expect Java to behave with "synchronized variables"?

Henry

I thought that Java will behave with "synchronized variables" as with synchronized methods. Only one thread has permission to access them at a time, without seeking the lock of the object which owned these synchronized variables.
I think in this case, synchronized variables are similar to volatile variables.
Thanks for your explanations.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But remember that synchronization is done by using locks ("monitors") on objects. Variables are not objects so it wouldn't be possible to get a lock based on a variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!