This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized member variables

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So automatic variables can be synchronized as such
synchronized
{
int i = 1;
code...
code...
}
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic