Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Can you use "synchronized" for data members

Reply
Thread Tools

Can you use "synchronized" for data members

 
 
Nagrik
Guest
Posts: n/a
 
      11-09-2011
Hello Group,

Can the "synchronized" kew word be used in front of data members. I
am aware that it can be used in fron of methods, and a block.
Something like this.

public class myclass {

synchronized prinvate int counter; // Is it allowed

}

If the answer is yes then in what situation it is adviseable.

Thanks in advance..

nagrik
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      11-09-2011
On 11/9/2011 10:32 AM, Nagrik wrote:
> Hello Group,
>
> Can the "synchronized" kew word be used in front of data members.



No. Use "volatile" for that.


 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      11-09-2011
On Wed, 9 Nov 2011, markspace wrote:

> On 11/9/2011 10:32 AM, Nagrik wrote:
>
>> Can the "synchronized" kew word be used in front of data members.

>
> No. Use "volatile" for that.


Yes. Although it isn't *quite* the same thing.

By which i mean that:

class Smeagol {
private volatile int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}

And:

class Deagol {
private int x;
public synchronized int getX() {
return x;
}
public synchronized void setX(int x) {
this.x = x;
}
}

Have slightly different semantics. If thread A calls getX, and then thread
B calls setX, then with Deagol, there is a happens-before relationship
between the two calls. With Smeagol, there is not. Whereas if A calls setX
and then B calls getX, both Smeagol and Deagol will generate a
happens-before relationship.

Or so i believe. I hope someone will correct me if i'm wrong.

The good news is that in most cases, the weaker guarantees provided by
Smeagol's volatile are actually just what you want (because you don't care
that a write to a variable happens after a read), and the JVM can generate
a more streamlined sequence of instructions for it.

tom

--
Eight-bit is forever
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      11-10-2011
On Nov 9, 10:40*am, markspace <-@.> wrote:
> On 11/9/2011 10:32 AM, Nagrik wrote:
>
> > Hello Group,

>
> > Can the "synchronized" kew word be used in front of data members.

>
> No. *Use "volatile" for that.


Oh goodness no. He's trying to make a simple int counter. Volatile
won't give atomic incrementing. He needs to have a synchronized block,
something like:
synchronized (someObject) {
++counter;
}
(Or something else fancier from the concurrent packages maybe (?) if
you're super concerned about speed and measurement says it helps.)

As a precaution, note that it will not work if you change counter to
Integer and synchronize on that, because Integers are immutable which
means different threads will be synchronizing on different objects
which doesn't give you the guarantees you need.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Templates: Members Vs. non-members Dave C++ 3 08-10-2004 11:23 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments