Core Java

Java Synchronized Keyword Example

Photo of Abhinav Nath GuptaAbhinav Nath GuptaNovember 7th, 2014Last Updated: May 19th, 2020
0 174 5 minutes read

In this example, we shall show you how to use the Java synchronized keyword and achieve synchronization in java.

1. What is Synchronized Keyword in Java?

Java allows us to use concurrency and multithreading pretty easily. Some of the most common use cases are those in which we have an object and many threads interacting with it. This can create many problems, because in cases that 2 different threads try to interact with the same resource, we have no idea what may actually happen (which is something that needs to be avoided in programming).

This is why Java provides thesynchronized keyword, which allows us to handle certain parts of code in anatomic way. Essentially, this means that when there are more than one threads that need to access asynchronized part of code, (increment a counter, add objects in a list, etc) there is no way that one thread will interrupt another thread, so we are perfectly sure that everything will work as intended.

2. How to use the Java Synchronized Keyword

There are two ways to use thesynchronized keyword to achieve synchronization in java. It can either be set in a method of a class, or in part of code inside a method, which becomes asynchronized block. In both cases, code inside asynchronized method or block is only accessed by one thread at a time.

Below, we present you both ways to use thesynchronized keyword.

2.1 Synchronized Method

TheSynchronizedMethodClass.java class below has one method,syncMethod(String threadName), stated with thesynchronized keyword.

SynchronizedMethodClass.java

import java.util.ArrayList;public class SynchronizedMethodClass {    private ArrayList<Integer> nums1;    private String pos1;    public SynchronizedMethodClass() {        nums1 = new ArrayList<Integer>();        nums1.add(0);        pos1 = "0";    }    public ArrayList<Integer> getNums1() {        return nums1;    }    public void setNums1(ArrayList<Integer> nums1) {        this.nums1 = nums1;    }    public String getPos1() {        return pos1;    }    public void setPos1(String pos1) {        this.pos1 = pos1;    }    public synchronized void syncMethod(String threadName) {        Integer number = nums1.get(nums1.size() - 1) + 1;        pos1 = String.valueOf(number);        nums1.add(number);        System.out.println("Thread " + threadName + " : "                + nums1.get(nums1.size() - 1) + " - " + pos1);    }}

TheSyncMethodRunnable.java class below is an implementation of theRunnable interface that will invoke thesynchronized method of itsSynchronizedMethodClass field, when started.

SyncMethodRunnable.java

public class SyncMethodRunnable implements Runnable {    private SynchronizedMethodClass synchronizedMethodClass;    private String threadName;    public SyncMethodRunnable(SynchronizedMethodClass synchronizedMethodClass, String threadName) {        this.synchronizedMethodClass = synchronizedMethodClass;        this.threadName = threadName;    }    public void run() {        for (int i = 0; i < 5; i++) {            synchronizedMethodClass.syncMethod(threadName);        }    }}

In order to run the example, we create twoThreads to run two instances of theSyncMethodRunnable. Both threads use the same instance of theSynchronizedMethodClass class. So both threads will invoke thesynchronized method of theSynchronizedMethodClass object.
we do itAppForSynchronizedMethod.java class.

AppForSynchronizedMethod.java

public class AppForSynchronizedMethod {    public static void main(String[] args) throws InterruptedException {        SynchronizedMethodClass example1 = new SynchronizedMethodClass();        System.out.println("**** Running AppForSynchronizedMethod.java *****");        System.out.println("**** Synchronized Method example*****");        Thread thread1 = new Thread(new SyncMethodRunnable(example1, "1"));        Thread thread2 = new Thread(new SyncMethodRunnable(example1, "2"));        thread1.start();        thread2.start();        thread1.join();        thread2.join();        System.out.println("List 1 is: " + example1.getNums1()                + " /Position 1: " + example1.getPos1()                + "\n");    }}

As a result, both threads start, but the first one to invoke thesynchronized method of theSynchronizedMethodClass object is Thread 1, and when it finishes Thread 2 manages to invoke the method. Since they both use the same instance of theSynchronizedMethodClass class, the list is finally filled with 10 numbers and the position is set to 10.

Output ofAppForSynchronizedMethod.java class is shown in Fig. 1 below.

Java Synchronized - Output of AppForSynchronizedMethod.java
Fig. 1 . Output of AppForSynchronizedMethod.java

2.2 Synchronized Block

TheSynchronizedBlockClass.java class below has one method, inside which thesynchronized keyword is set. Thesynchronized block created takes a parameter, which is the instance of theSynchronizedBlockClass.java class. This parameter may be one attribute of the class, or the whole class object, according to the needs of the programmer. Since the whole instance of the class is set as the parameter in thesynchronized block, it is now locked against changes until the thread finishes code execution in the block. The rest part of the method that is not inside the block may be accessed simultaneously by all threads that invoke the method.

SynchronizedBlockClass.java

import java.util.ArrayList;public class SynchronizedBlockClass {    private ArrayList<Integer> nums2;    private String pos2;    private int counter;    public SynchronizedBlockClass() {        nums2 = new ArrayList<Integer>();        nums2.add(0);        pos2 = "0";    }    public ArrayList<Integer> getNums2() {        return nums2;    }    public void setNums2(ArrayList<Integer> nums2) {        this.nums2 = nums2;    }    public String getPos2() {        return pos2;    }    public void setPos2(String pos2) {        this.pos2 = pos2;    }    public int getCounter() {        return counter;    }    public void setCounter(int counter) {        this.counter = counter;    }    public void syncBlock(String threadName) {        counter++;        System.out.println("Thread " + threadName + " - counter: " + counter);        synchronized (this) {            Integer number = nums2.get(nums2.size() - 1) + 1;            pos2 = String.valueOf(number);            nums2.add(number);            System.out.println("Thread " + threadName + " Added to list: "                    + nums2.get(nums2.size() - 1) + " - " + pos2);        }    }}

TheSyncBlockRunnable.java class below is an implementation of theRunnable interface that will invoke thesynchronized method of itsSynchronizedBlockClass field, when started.

SynchronizedBlockRunnabled.java

public class SyncBlockRunnable implements Runnable {    private SynchronizedBlockClass synchronizedBlockClass;    private String threadName;    public SyncBlockRunnable(SynchronizedBlockClass synchronizedBlockClass, String threadName) {        this.synchronizedBlockClass = synchronizedBlockClass;        this.threadName = threadName;    }    public void run() {        for (int i = 0; i < 5; i++) {            synchronizedBlockClass.syncBlock(threadName);        }    }}

Now inAppForSynchronizedBlock.java class, we create twoThreads to run two instances of theSyncBlockRunnable. Both threads use the same instance of theSynchronizedBlockClass class. So both threads will invoke its method, but only one of them will get in thesynchronized block of the method at a time.

AppForSynchronizedBlock.java

public class AppForSynchronizedBlock {    public static void main(String[] args) throws InterruptedException {        SynchronizedBlockClass example2 = new SynchronizedBlockClass();        System.out.println("**** Running AppForSynchronizedBlock.java *****");        System.out.println("**** Synchronized Block example*****");        Thread syncMethodThread1 = new Thread(new SyncBlockRunnable(example2, "1"));        Thread syncMethodThread2 = new Thread(new SyncBlockRunnable(example2, "2"));        syncMethodThread1.start();        syncMethodThread2.start();        syncMethodThread1.join();        syncMethodThread2.join();        System.out.println("List 2 is: " + example2.getNums2()                + " /Position 2: " + example2.getPos2()                + " / counter : " + example2.getCounter());    }}

As a result, both threads invoke the method of theSynchronizedBlockClass object. But only one thread at a time locks the object to run thesynchronized block. Thecounter increment is out of thesynchronized block, so thecounter result is wrong. But the rest of the method that is inside thesynchronized block is protected against being invoked by many threads simultaneously and achieve synchronization in java, so thelist andposition result is correct.

Output ofAppForSynchronizedBlock.java class is shown in Fig. 2 below.

Java Synchronized - Output of AppForSynchronizedBlock.java
Fig. 2 Output of AppForSynchronizedBlock.java

3. Download the Source Code

Download
You can download the full source code ofsynchronized example here : Java Synchronized Keyword Example

Last updated on Aug 23, 2019

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Abhinav Nath GuptaAbhinav Nath GuptaNovember 7th, 2014Last Updated: May 19th, 2020
0 174 5 minutes read
Photo of Abhinav Nath Gupta

Abhinav Nath Gupta

Abhinav holds a Master degree in Computer Science and Engineering from the National Institute of Technology Karnataka. He has finished his graduation from Information Technology Department in the Anand Engineering College, Agra. During his studies he has been involved with a large number of projects ranging from Networking and Cryptography. He works as a software development engineer at a software development firm in bengaluru where he is mainly involved with projects based on Nodejs. He is interested in cryptography, data security, cryptocurrency and cloud computing, and published articles regarding these topics. He can be reached at abhi.aec89@gmail.com.

Related Articles

Bipartite Graph

Java not equal Example

January 17th, 2020
Bipartite Graph

Java API Tutorial

October 26th, 2020
Bipartite Graph

Java Struct Example

January 8th, 2020
Bipartite Graph

Java Node Example

November 20th, 2019
Bipartite Graph

Java Swing MVC Example

January 26th, 2016
Bipartite Graph

How to call a method in Java

December 26th, 2019
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.