Can you use "synchronized" for data members

Discussion in 'Java' started by Nagrik, Nov 9, 2011.

  1. Nagrik

    Nagrik Guest

    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
    Nagrik, Nov 9, 2011
    #1
    1. Advertising

  2. Nagrik

    markspace Guest

    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.
    markspace, Nov 9, 2011
    #2
    1. Advertising

  3. Nagrik

    Tom Anderson Guest

    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
    Tom Anderson, Nov 9, 2011
    #3
  4. 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.
    Joshua Maurice, Nov 10, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    4
    Views:
    131,748
    tonni
    Aug 11, 2010
  2. Pep
    Replies:
    6
    Views:
    29,235
  3. CoolPint
    Replies:
    8
    Views:
    955
    Jeff Schwab
    Dec 14, 2003
  4. dmcreyno
    Replies:
    9
    Views:
    9,545
    Mark Space
    Jun 27, 2006
  5. ankur
    Replies:
    4
    Views:
    1,408
    Eric Sosman
    Nov 28, 2008
Loading...

Share This Page