CORE JAVA: Synchronization

 

Introduction

Synchronization in Java is the capability to control the access of multiple threads to any shared resource.Java Synchronization is a better option where we want to allow only one thread to access the shared resource.

Why use Synchronization?

The synchronization is mainly used to

  1. To prevent thread interference.
  2. To prevent consistency problem.

Types of Synchronization:

There are two types of synchronization

  1. Process Synchronization
  2. Thread Synchronization

Here, we will discuss only thread synchronization.


Understanding the problem without Synchronization

In this example, there is no synchronization, so the output is inconsistent. Let’s see the example:

Untitled.png

Output:

 5
 100
 10
 200
 15
 300
 20
 400
 25
 500

Java synchronized method

If you declare any method as synchronized, it is known as synchronized method.

Synchronized method is used to lock an object for any shared resource.

When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.

Untitled.png

Output:

5

10

15

20

25

100

200

300

400

500


Inter-thread communication in Java

Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other.

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class:

  • wait()
  • notify()
  • notifyAll()

wait() method:

 It tells the calling thread to give up the lock and go to sleep until some other thread enters the same monitor and calls notify(). The methodwait() releases the lock prior to waiting and reacquires the lock prior to returning from the methodwait(). The methodwait() is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism.

Syntax:

public final void wait(long timeout)
                throws InterruptedException

Untitled.png

 notify() method:

Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object’s monitor. A thread becomes the owner of the object’s monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type byClass, executing a synchronized static method of that class.

 

Syntax:

public final void notify()

notifyAll() method:

Wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the methodswait.The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object;  for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.

Syntax:

public final void notifyAll()

Example of inter thread communication in java

Untitled.png

Output:

going to withdraw...
Less balance; waiting for deposit...
going to deposit...
deposit completed...
withdraw completed

 



 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s