What is Synchronized Keyword and Method in Java? Example

synchronized keyword in Java is one of the two important keywords related to concurrent programming in Java, the other being a volatile keyword. the synchronized keyword is used to provide mutual exclusion, thread safety, and synchronization in Java. Unlike Volatile keyword, synchronized keyword is not an application to instance variable and you can only use synchronized keyword either with synchronized method or block. synchronized keyword work with the concept of lock, any thread which holds a lock on which synchronized method or block is locked, can enter otherwise thread will wait till it acquires the lock. In Java programming language every object holds a lock and every class holds a lock, like two String objects s1, s2 holds two different object lock,s and String.class is used to represent class lock.

In order to maintain mutual exclusion, a critical section of code is locked using synchronized keywords. In this Java tutorial we will learn What is synchronized keyword is, What is synchronized method both static and non-static and What is a synchronized block is in Java.

We will also explore some important details about synchronized keywords.


What is Synchronized Method in Java

Any method, either static or non-static can be made synchronized by using synchronized modifier in method signature, for example, public void read() is a non-synchronized method but public synchronized void read() is a synchronized method.

The difference between synchronized and non-synchronized method is that, multiple thread can execute non synchronized method simultaneously but only one thread can execute synchronized method at a time.

Because thread needs lock or monitor to enter synchronized method  or block, due to this contention between thread occurs, resulting synchronized method being much slower than there non-synchronized counterpart.

What is Synchronized Keyword and Method in Java? Example



One more thing to remember is that non-synchronized method is locked with object lock but static synchronized method is locked with class lock, which means two thread can execute both of these method simultaneously because they are locked with different lock or monitor. If you are guarding shared resource like that then it will fail. Read Java mistake 2: static and non static synchronized

here's an example of a synchronized method in Java:

public class ThreadSafeCounter{ 

private int counter = 0;

  public synchronized void incrementCounter() {
    counter++;
 }

}


In this example, we have a class called Example with a private instance variable called counter and a method called incrementCounter(). The incrementCounter() method is declared as synchronized, which means that only one thread can execute the method at a time. This ensures that the counter variable is incremented atomically, without any race conditions or other concurrency issues.

That's all about synchronized keyword and synchronized method in Java. The synchronized keyword and method in Java provide a mechanism for ensuring that only one thread can access a shared resource at a time. T

his prevents concurrent access and modification of the resource, which can cause race conditions and other issues. Synchronization can be applied to both methods and blocks of code, and it uses the concept of locks to control access. 

While synchronization can be an effective tool for managing concurrency, it can also have performance implications, so it should be used judiciously. 

By understanding how to use the synchronized keyword and method in Java, developers can write safer and more reliable concurrent code.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.