0
 class MutableInteger {

    private int value;

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

    public synchronized int getValue() {
        return value;
    }

 public void nonSync(){

 }

}

I am trying to understand how the synchronization keyword works.

I have a class with methods that are sychronized, this means that on that specific instance of the object, only one thread can call that method at a time? This only pertains to that method though? So if a thread A was calling incriment, thread B would have to wait until thread A was done executing the method? But this is only a method by method basis?

however, if I did

synchronized(this) {
  //code
}

that would lock the entire instance of the object?

Does that make sense.. I get in essence what this is supposed to be doing, just trying to fill the gaps

billybob2
  • 681
  • 1
  • 8
  • 17
  • 1
    They're the same thing. Synchronization exists on the instance, not on a method. – shmosel May 14 '19 at 00:24
  • `synchronized` methods synchronize on the monitor of the instance on which the method is invoked, or on that of the class for static methods. Each instance has one monitor, and each class is one, so no, it is not method by method. It is all instance methods of a given instance synchronizing with each other and with anything else synchronized on that object, and, separately, all static methods of the same class synchronizing with each other and with anything else synchronized on that class. – John Bollinger May 14 '19 at 00:28
  • As an example of synchronization, this is fine, but if you want to use such behavior do not forget about the Atomic classes provided by the SDK. The [AtomicInteger](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html) class does exactly the thing as your code, only in a professional way ;) – Magyar Dávid May 14 '19 at 00:42
  • See also: [locks](https://stackoverflow.com/questions/4201713/synchronization-vs-lock) and [volatile fields](https://stackoverflow.com/questions/3519664/difference-between-volatile-and-synchronized-in-java) – Blake May 14 '19 at 01:16

1 Answers1

0

You are right, synchronized methods are locking the instance itself. So writing the followings:

synchronized myMethod() {
    //code
}

Is essentially the same behavior as if you have written:

myMethod() {
    synchronized(this) {
        //code
    }
}

Note, that this is simply an Object and is used as the lock as any other object would be used - the lock can be owned by only one thread at a time, the others must wait for it to enter a synchronized block using the same object. Since methods having the synchronized keyword behave such a way, they share the lock being the instance itself.

So, if you have an increment() and decrement() method both marked synchronized, then only either of the two can be used and by one thread at a time.

Meanwhile, other methods without the synchronized keyword remain completely unaffected and will function the same, whether there are synchronized methods around them or not.