Thread Synchronization in Java

In this section, you will learn Thread Synchronization in Java

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

Thread Synchronization in Java

Sometimes, when two or more threads need shared resource, they need a proper mechanism to ensure that the resource will be used by only one thread at a time.

The mechanism we use to achieve this is known as thread synchronization.

The thread synchronization is achieved through the synchronized keyword. The statements which need to be synchronized should put into the synchronized block It is also known as critical section. The synchronized block is defined as follows :

synchronized(object) {
   // statements to be synchronized
}

Object in Java which have synchronization block(critical section) gets a lock related with the object. To get access to the critical section or synchronization block you need the associated object's lock.

EXAMPLE

Given below the example using a synchronized block having thread synchronization :

class Print {
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}

class PrintCaller implements Runnable {
String msg;
Print target;
Thread t;

public PrintCaller(Print targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}

// synchronize calls to call()
public void run() {
synchronized (target) { // synchronized block
target.call(msg);
}
}
}

public class TreadSynchroniztion {
public static void main(String args[]) {
Print target = new Print();
PrintCaller ob1 = new PrintCaller(target, "Hello");
PrintCaller ob2 = new PrintCaller(target, "Synchronized");
PrintCaller ob3 = new PrintCaller(target, "World");

// wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}

OUTPUT :

[Hello]

[World]

[Synchronized]


Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

Thread Synchronization in Java

Posted on: July 22, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Thread Synchronization in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads