• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Threads Synchronization

August 21, 2007 //  by Krishna Srinivasan//  Leave a Comment

Synchronization is done in order to protect a segment of code from being accessed by more than a single Thread at any particular instance of time. In Java, synchronization is achieved with the use of synchronized keyword. Synchronization can be applied to methods as well as to a block of code.

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

The following code shows how to apply synchronization at the method level or to a block of code.

[code lang=”java”] //Synchronizing a method
public synchronized void myMethod() {
// perform some operation
}
[/code] [code lang=”java”] //Synchronizing a block of code
synchronized(this) {
// perform some operation
}
[/code]

When synchronization is applied to a method or a code block, the first Thread that accesses it would have a lock on it on that particular object which was used to invoke it. This lock would be released only after the first Thread completes its execution of that method or a code block.If any other threads have to access the same method or block of code on the same object instance, then they have to wait till the current thread accessing it completes execution and releases the lock on it.Consider the following class which models a shared data and it is expected to be accessed by one or more thread. Given below is the code snippet for the Shared Data class.

SharedData.java
[code lang=”java”] public class SharedData{

private String data = "HAPPY";

public void accessData(){
for(int i =0; i<20; i++){
data = data + " " + i + " ";
}
System.out.println(data);
}
}
[/code]

And here is the Thread code that tries to access the shared data.

MyThread.java
[code lang=”java”] public class MyThread extends Thread{

private SharedData data;

public MyThread(SharedData data){
this.data = data;
}

public void run(){
data.accessData();
}
}
[/code]

Let us initiate the above process by creating one or more threads and then to access on the shared data.

ThreadSynchronizationTest.java
[code lang=”java”] public class ThreadSynchronizationTest {

public static void main(String[] args) {
SharedData data = new SharedData();

MyThread one = new MyThread(data);
one.start();

MyThread two = new MyThread(data);
two.start();
}
}
[/code]

Actually, the expected results for this code is as follows,
[code] HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19
[/code] But, since the accessData() method is accessed by two different threads on the same instance of ShareData. The results in this case would be like,
[code] HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4

HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[/code] This is not the expected result and the cause for this is two threads have accessed the same method simultaneously without proper synchronization.

Let us see how the same code works when synchronization is applied to it, by the use of synchronized keyword for the method accessData().

[code lang=”java”] public synchronized void accessData(){
for(int i =0; i<20; i++){
data = data + " " + i + " ";
}
System.out.println(data);
}
}
[/code]

Since synchronization has been applied to the method accessData(), we get the proper expected results which is as follows,
[code] HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

HAPPY 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19
[/code] Synchronization is aimed at making an object thread-safe. But, before going for it, we have to know that performance would be slow when we use synchronized methods. Since overheads such as Object locks are a part of synchronization, invoking a synchronized method or block of code is considerably slower than invoking a non-synchronized method or block of code.

Hence, it is advised to analyze based on the application and ensure whether Synchronization is needed or not.

Category: JavaTag: Core Java, Threads

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Overriding the toString() method in Object class
Next Post: Storing Application Data using Preferences API »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact