Written by Sai Sandeep Thota on December 22, 2013 in Java > Programming

Java Program to implement Thread Synchronization

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Thread is a light weight process. Threads are economical to create and destroy. Here in this program context if two threads A & B are present, if A is executing then B will not interrupt method that Thread A is using until it has completed its execution, i.e., if two threads are present and they want to share a common resource, thread synchronization will ensure that it will be shared by only one thread at a time.

In this program we are using 3 classes, myThread, Syn, sycnDemo.

The myThread class is inheriting or extending the Thread class. We are getting the name of Thread from syncDemo class and passing it the super(Thread) class and have defined a String array which contains three strings “I”, “love”, “Java”. In the run() method we are creating object for Syn class and passing current Thread name and String array. getName() method is used to get the current thread name.

The Syn contains a method called display. We have placed synchronized keyword before method name in-order to implement Synchronization. Then inside the body of the method we are using a for loop to print the current Thread name and String array which are parameters.

In the syncDemo we are creating 2 myThread class instances: t1, t2 and passing name of the Thread as parameter and start executing the threads with start() method.

Summary:
#1. synDemo objects of the myThread class t1 and t2 invoke the method start().
#2. Now the control is transferred to myThread class and it passes the name of the thread to super(Thread) class and run() method is invoked as we have invoked start() method, objects of Syn class are created.
#3. In Syn class we are having a synchronized display method which is used to enforce that the method is used by only one thread at a time.

/*
Download more at ProgrammingUnit.com
*/
class myThread extends Thread {
	static String msg[] = {"I", "love", "JAVA"};
	public myThread(String id)
	{
		super(id);
	}

	public void run()
	{
		Syn ob = new Syn();
		ob.display(getName(), msg);	
	}
}

class Syn extends Thread{
	public static synchronized void display(String id, String list[])
	{
		for(int i=0;i<list.length;i++)
		{
			System.out.println(id + " " + list[i]);
			try {
				sleep(1000);
			}
			catch(InterruptedException ie)
			{
				System.out.println(ie);
			}
		}
	}
}

class syncDemo {
	public static void main(String args[])
	{
		myThread t1 = new myThread("Thread 1");
		myThread t2 = new myThread("Thread 2");

		t1.start();
		t2.start();
	}
}

We can also implement Thread Priority using setPriority() method. For instance if we want Thread 1 to use the display() method then t1.setPriority(Thread.MAX_PRIORITY); will do the work for us.

Download Source Code

Output:
Java Program to implement Thread Synchronization

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: