synchronized Java Keyword with Examples

The synchronized keyword may be applied to a method or statement block and provides protection for critical sections that should only be executed by one thread at a time.
We can synchronize our code in either of two ways. Both involve the use of the synchronized keyword.
  1. Using Synchronized Methods
  2. Using Synchronized Statement Or Block

1. Using Synchronized Methods

To make a method synchronized, simply add the synchronized keyword to its declaration:
public class SynchronizedCounter {
    private int c = 0;

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

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

    public synchronized int value() {
        return c;
    }
}
If a count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:
  • First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
  • Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.

2. Using Synchronized Statement or Block

You do not have to synchronize a whole method. Sometimes it is preferable to synchronize only part of a method. Java synchronized blocks inside methods make this possible.
Here is a synchronized block of Java code inside an unsynchronized Java method:
  public void add(int value){

       synchronized(this){
           this.count += value;   
       }
  }
This example uses the Java synchronized block construct to mark a block of code as synchronized. This code will now execute as if it was a synchronized method.

Summary

  1. The synchronized keyword prevents a critical section of code from being executed by more than one thread at a time.
  2. When applied to a static method, the entire class is locked while the method is being executed by one thread at a time.
  3. When applied to an instance method, the instance is locked while being accessed by one thread at a time.
  4. When applied to an object or array, the object or array is locked while the associated code block is executed by one thread at a time.
Read more at Synchronization in Multithreading Java

All Java Keywords 

  1. abstract Java Keyword
  2. assert Java Keyword
  3. boolean Java Keyword
  4. break Java Keyword
  5. byte Java Keyword
  6. case Java Keyword
  7. catch Java Keyword
  8. char Java Keyword
  9. class Java Keyword
  10. continue Java Keyword
  11. default Java Keyword
  12. do Java Keyword
  13. double Java Keyword
  14. else Java Keyword
  15. enum Java Keyword
  16. extends Java Keyword
  17. final Java Keyword
  18. finally Java Keyword
  19. float Java Keyword
  20. for Java Keyword
  21. if Java Keyword
  22. implements Java Keyword
  23. import Java Keyword
  24. instanceof Java Keyword
  25. int Java Keyword
  26. interface Java Keyword
  27. long Java Keyword
  28. native Java Keyword
  29. new Java Keyword
  30. package Java Keyword
  31. private Java Keyword
  32. protected Java Keyword
  33. public Java Keyword
  34. return Java Keyword
  35. short Java Keyword
  36. static Java Keyword
  37. strictfp Java Keyword
  38. super Java Keyword
  39. switch Java Keyword
  40. synchronized Java Keyword
  41. this Java Keyword
  42. throw Java Keyword
  43. throws Java Keyword
  44. transient Java Keyword
  45. try Java Keyword
  46. void Java Keyword
  47. volatile Java Keyword
  48. while Java Keyword

Comments