• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Synchronization

October 10, 2011 By j2eereference Leave a Comment

What is Synchronization?

Synchronization is the mechanism to control the access of multiple threads on a shared resource or on a block of code. In java synchronization can be achieved using synchronized keyword.

1
2
3
4
5
6
7
8
class Test
{
   ……
   public synchronized void doSomething()
   {
      //method body
   }
}

Why Synchronization is required?

Synchronization is required when java objects are shared among multiple threads to avoid any corruption of state of abject. If object is read-only or immutable object you don’t required synchronizing your code in multiple threads.

How Synchronization works?

Synchronization in java works with object locks. Every object has its own lock and one lock per object. When you enter the synchronized method, you automatically acquire a lock on a current object of the class. If one thread has acquires the lock no other thread can get the lock or can enter the synchronized block until first thread releases the lock.  Once the thread completes the execution of synchronized block, it releases the lock.

 Two ways to synchronize code

It’s always better to synchronize block of code rather than a method.   If you don’t want to synchronize entire method you can just synchronize block of code. Synchronization does hurt concurrency and performance hit, so it’s better to synchronize code only required part. The two ways to synchronize the code is shown below.

Synchronizing a Method

Below code shows how to synchronize a method. It acquires a lock on the object used to invoke the method.

1
2
3
4
5
6
7
8
9
10
11
12
public class TestSynchronize
{
  private int i = 4;
  public synchronized int getCount()
  {
   return i;
  }
  public synchronized void setCount(int i)
  {
   i++;
  }
}

 Synchronizing a Block

If you synchronize the block of code it acquires the lock on this (current) object.

1
2
3
4
5
6
7
8
9
10
11
class Test
{
   public void doMethod()
    {
      System.out.println(“ Its not synchronized “);
      Synchronized(this) 
       {
         System.out.println(“ This is synchronized “);
       }
    }
}

 

Synchronizing static methods

The static methods can also be synchronized. The static synchronized method will have lock on class instance java.lang.Class. One lock per class. Threads accessing static synchronized method will always block each other in the same class.

Ex;

1
2
3
4
5
6
7
8
class TestStatic
{
     private static int x=5;
     public static synchronized void doStatic()
       {
         System.out.println(“Value of x : “ +x);
       }
}

Some points to remember about Synchronization.

1.  Only methods or blocks can be synchronized.

2.  A class can have both synchronized and non-synchronized methods and multiple threads can still access the class’s non-synchronized method.

3. One lock per object.

4.  A thread can acquire more than one lock.

5. If thread has a lock and goes to sleep, it will not release the lock.

6. A static synchronized method and non-static synchronized method will not block each other because static method locks on class instance and not-static method on this instance.

Thread Deadlock

Deadlock occurs when two threads are waiting for each other to releases the lock forever.

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.