Synchronization in Java

Synchronization is a process that at any point in time will not allow more than one thread for a resource. In multi-threaded environment, multiple threads attempt to access the same resource, leading in erroneous and unforeseen outcomes. To prevent this, Synchronization controls threads for sequential access to resources.



Threads without Synchronization

To understand Synchronization, let’s start with threads running in parallel without synchronization. Threads will run parallel and attempt to access resource concurrently. The outcomes are printed in unpredictable order as shown in the below example.

MyThread.java

package com.yawintutor;

public class MyThread extends Thread {

	public void run() {
		for (int i = 0; i < 5; i++) {
			System.out.println("Thread name : " + getName() + " count : " + i);
			try {
				sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

MyThreadMain.java

package com.yawintutor;

public class MyThreadMain {
	public static void main(String[] args) throws Exception {

		MyThread thread1 = new MyThread();
		thread1.setName("THREAD 1");
		thread1.start();

		MyThread thread2 = new MyThread();
		thread2.setName("THREAD 2");
		thread2.start();

	}
}

Output

Thread name : THREAD 1 count : 0
Thread name : THREAD 2 count : 0
Thread name : THREAD 1 count : 1
Thread name : THREAD 2 count : 1
Thread name : THREAD 2 count : 2
Thread name : THREAD 1 count : 2
Thread name : THREAD 2 count : 3
Thread name : THREAD 1 count : 3
Thread name : THREAD 2 count : 4
Thread name : THREAD 1 count : 4


Synchronized Threads

There is a controlled execution of synchronized threads. Threads are performed one after the other to access the resource. In this example, The data is printed one thread after another thread.

SyncThread.java

package com.yawintutor;

public class SyncThread extends Thread {
	public String lockingObject;

	public void run() {
		synchronized (lockingObject) {
			for (int i = 0; i < 5; i++) {
				System.out.println("Thread name : " + getName() + " count : " + i);
				try {
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

SyncThreadMain.java

package com.yawintutor;

public class SyncThreadMain {
	public static void main(String[] args) throws Exception {
		String obj = "lockobject";

		SyncThread thread1 = new SyncThread();
		thread1.setName("THREAD 1");
		thread1.lockingObject = obj;
		thread1.start();

		SyncThread thread2 = new SyncThread();
		thread2.setName("THREAD 2");
		thread2.lockingObject = obj;
		thread2.start();
	}
}

Output

Thread name : THREAD 1 count : 0
Thread name : THREAD 1 count : 1
Thread name : THREAD 1 count : 2
Thread name : THREAD 1 count : 3
Thread name : THREAD 1 count : 4
Thread name : THREAD 2 count : 0
Thread name : THREAD 2 count : 1
Thread name : THREAD 2 count : 2
Thread name : THREAD 2 count : 3
Thread name : THREAD 2 count : 4



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *