Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Synchronized member variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Synchronized member variables" Watch "Synchronized member variables" New topic
Author

Synchronized member variables

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Okay.. a question straight from the java round up game.. Why can't a member variable be synchronized? (or can it)

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Can a member variable be declared synchronized? No, because the word synchronized marks method code so that it can't be run by more than one thread at a time.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Dale
Like Marilyn said you can't declare a member varaiable synchronized like this:
public synchronized Integer i;
But you can declare a block of code to be synchronized on an object that is a member variable, for example in a method yu could have a block like this:
synchronized (i){
//do stuff
}

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
So automatic variables can be synchronized as such
synchronized
{
int i = 1;
code...
code...
}
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Dale
If its not a method then you ahve to synchronize on an object. You can't just synchronize a block of code. It has to be of the form:
synchronize(someObject){
... do stuff
}
The reason for this is because in order for synchronization to work there must be a lock for the thread to aquire. The only way to get the lock is to have an object whose lock you want. In the case of a method you are synchroniziung on the object that owns the method in the case of a block of code you have to give it the object whose lock you want. Thats why you can't say just:
synchronized {
... do stuff
}
there is no lock to get.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I think that what Dale is looking for is a volitile variable.
The reason that code is synchronized is so that different threads don't stomp on each others activities.
The way that shared variables are guaranteed to be thread-safe, and therefore won't get stomped on by other threads, is by marking them with the volatile keyword. This forces the synchronized code to run back and insure that the value of the working copy of the variable is the "most up to date" every time that it accesses the variable to do something with it.
Form 8.3.1.4 volatile Fields

A field may be declared volatile, in which case a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. Moreover, operations on the master copies of one or more volatile variables on behalf of a thread are performed by the main memory in exactly the order that the thread requested.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
OK, so I re-read the question, and he just wanted to answer the game question - so skip the volitile stuff and just remember the lock stuff.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized member variables