| Author |
Why instance variables cannot be marked "synchronized"?
|
yen hoang
Ranch Hand
Joined: Apr 05, 2009
Posts: 58
|
|
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,
|
I love Mozart (^o^)
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24043
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
And welcome to JavaRanch
|
 |
yen hoang
Ranch Hand
Joined: Apr 05, 2009
Posts: 58
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
|
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.
|
 |
 |
|
|
subject: Why instance variables cannot be marked "synchronized"?
|
|
|