aspose file tools*
The moose likes Beginning Java and the fly likes Why instance variables cannot be marked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why instance variables cannot be marked "synchronized"?" Watch "Why instance variables cannot be marked "synchronized"?" New topic
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: 24184
    
  34

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: 18846
    
  40

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: 38881
    
  23
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: 18570
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why instance variables cannot be marked "synchronized"?