0
public class SynchronizedCounter {
private int c = 0;

public synchronized void increment() {
    c++;
}

public synchronized void decrement() {
    c--;
}

public synchronized int value() {
    return c;
}
}

If there are two threads, each having the same instance of SynchronizedCounter, does this mean that if one thread is calling increment, the other can not call decrement. Is the above code equivalent to a synchronised object? i.e.

public void run(){
    synchronised( objectReferenceSynchronisedCounter){
        if(conditionToIncrement)
        objectReference....Counter.increment();
        else
        objectReference....Counter.decrement();
    }
}
Kraken
  • 23,393
  • 37
  • 102
  • 162
  • 4
    no, because the `if` part is not synchronized in the first case. – njzk2 Oct 07 '14 at 20:54
  • @njzk2 Ok, so maybe I have two runnables, one that increments, the other that decrements. No if statement. – Kraken Oct 07 '14 at 20:56
  • See also http://stackoverflow.com/questions/417285/equivalent-code-for-instance-method-synchronization-in-java – lexicore Oct 07 '14 at 20:56
  • Your code isn't complete, but seems like intended answer is: no, because in first version thread can be preempted between method calls (`increment`, `decrement`), and another can perform `increment`, thus giving non-zero change between calls. – Victor Sorokin Oct 07 '14 at 20:56
  • If your condition does not depend on the state of the object, or more generally speaking if you do not drag the state from one call to the other the code is not the same but might have the same outcome. Especially if you are not multi threaded :) – eckes Oct 07 '14 at 21:01
  • The question is a bit unclear, but you may be interested in http://stackoverflow.com/a/574267/2864740 – user2864740 Oct 07 '14 at 21:17

3 Answers3

1

The answer is no.

the synchronized scope is the modified method

See http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html

Gab
  • 7,869
  • 4
  • 37
  • 68
1

There are 2 questions:

If there are two threads, each having the same instance of SynchronizedCounter, does this mean that if one thread is calling increment, the other can not call decrement.

That is correct. The call to decrement will be blocked while the other thread executes increment. And vice-versa.

Is the above code equivalent to a synchronised object? [code follows]

Your second example is slightly different because you include an if statement in the synchronized block. And generally speaking if a synchronization bloc includes multiple calls, it is not equivalent to synchronizing each individual call.

There is no such thing as a synchronized object in Java. You synchronize methods or code blocks.

However, and maybe that is what you meant, in both your examples the lock is held on the same object, namely the instance of the object whose methods are called. So apart from the slightly different scope, the 2 examples synchronize in the same way on the same object.

Florian F
  • 1,300
  • 1
  • 12
  • 28
0

It is exactly a synchronized object. "synchronized" on the method locks "this" once one of your methods is called the others cannot execute until the method id exited.

BillFromHawaii
  • 334
  • 1
  • 7