• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Multithreading » Thread Synchronization in Java
Next →
← Prev

Thread Synchronization in Java

By Dinesh Thakur

When more than one thread has to use a shared resource, Java finds a way of ensuring that only one thread uses the resources at one point of time; this is called synchronization.

In Java, each object is associated with a lock. The term lock refers to the access granted to a particular thread that has entered a synchronized method. Monitor refers to a portion of the code in a program. It contains one specific region (related to data or some resource) that can be occupied by only one thread at a time. This specific region within a monitor is known as critical section. A thread has exclusive lock from the time it enters the critical section to the time it leaves. That is, the data (or resource) is exclusively served for the thread, and any other thread has to wait to access that data (or resource). Some common terminology while using monitors is as follows: entering the critical section is known as acquiring the monitor, working with the critical section (data or resource) is known as owning the monitor and leaving the critical section is known as releasing the monitor.

The key concept in synchronization is the monitor. The monitor holds exclusive lock that can be owned by only one object at a time. The monitor allows only one thread to execute a synchronized method on the object at one time. This is accomplished by giving the ownership of the monitor to that object, or in other words, locking the object when the synchronized method is invoked. An object thus locked is also denoted to have obtained the lock.

When there are several synchronized methods attempting to act on an object, only one synchronized method may be active on an object at one instant of time and all other threads attempting to invoke synchronized methods must wait. Thus, other threads that attempt to lock the already locked monitor will be suspended. When the execution of that synchronized method is complete, the lock on the object is opened and the monitor allows the highest-priority runnable thread attempting to invoke a synchronized method to proceed.

A thread executing in a synchronized method may determine that it cannot proceed, so it voluntarily calls wait (). This removes the thread from contention for the processor and from contention for the monitor object. The thread now remains in the waiting state while other threads try to enter the monitor object. When the thread executing the synchronized method completes, it can notify a waiting thread to become ready again so that now that thread can attempt to obtain the lock on the monitor object again and be executed. The notify () method acts as a signal to the waiting thread that the condition to be satisfied for running the waiting thread is satisfied now. Then, the waiting thread can enter into the monitor. If a thread calls notifyAll (), then all threads waiting for the object become eligible to reenter the monitor (that is, they are all placed in a runnable state). Only one of those threads can obtain the lock on the object at a time.

The methods wait(), notify() and notifyAll() are the methods of the class Object. Since every  user class inherits the class Object, these methods are available to all classes. Therefore, any object can enter into a monitor.

The monitor maintains a list of all threads waiting to enter the monitor object to execute synchronized methods. A thread is inserted in the list and waits for the object if that thread calls a synchronized method of the object while another thread is already executing is a synchronized method of that object. A thread is inserted in the list also if the thread calls the wait () method while operating inside the object. Threads executing synchronized methods explicitly call wait () inside the monitor upon completion of a synchronized method. Other threads that are blocked because the monitor was busy can now proceed to enter into the object. Threads that explicitly invoked wait () can only proceed when they are notified by the notify or notifyAll methods called by another thread. When it is acceptable for a waiting thread to proceed, the scheduler selects the thread with the highest priority.

The keyword synchronized is used to synchronize the threads and there are two ways to do it: by using synchronized methods and using a synchronized statement.

We’ll be covering the following topics in this tutorial:

  • Synchronized methods
  • Synchronized statements
  • Deadlocks

Synchronized methods

If two or more threads modify an object, the methods that carry the modifications are declared synchronized. Constructors need not be synchronized because a new object is created in only one thread. The general syntax for declaring a method synchronized is the following:

Modifier synchronized <return_type> <method_name> (parameters)

To get a clear understanding of how this works, consider the Program.

Program Using unsynchronized threads.

import java.io.*;
class Class_A {
void printValue (){
         try{
                 for(int i =0; i<=5; i++){
                        System.out.print ( i +” “);
                        Thread.sleep (1000);
}
}
           catch(InterruptedException){}
}
}
class ThreadExample implements Runnable {
Class_A ob1;
    Thread t;
ThreadExample (Class_A c){
          this.ob1 =c;
t =newThread(this);
}
publicvoid run (){
ob1.printValue();
}
publicstaticvoid main (String[] args){
Class_A ca =new Class_A ();
ThreadExample one =new ThreadExample (ca);
one.t.start();
ThreadExample two =new ThreadExample (ca);
two.t.start ();
ThreadExample three =new ThreadExample (ca);
three.t.start ();
}
}

If we run the above example, we expect the following output:

0 1 2 3 4 5 0 1 2 3 4 5

0 1 2 3 4 5

But the result is unpredictable; it may result in the following:

0 0 0 1 1 1 2 2 2 3 3 3

4 4 4 5 5 5

The unpredictability of the output is a direct result of not synchronizing the printValue method. To see this and get the desired result let the printValue() method be declared synchronized. synchronized void printValue()

Now, only one thread can execute in the method printValue ( ) and the result will be as desired.

The following points are to be noted when implementing synchronized methods

• When a thread calls a non-static synchronized method, the thread acquires a lock on that object. Any other thread that tries to call the same method enters into the wait state.

• The synchronization of static methods is independent of the synchronization of non-static methods. That is, if a thread calls a static synchronized method, the other threads are free to call non-static synchronized methods.

• A sub-class can override a synchronized method in its super-class. The overridden method need not be synchronized.

• If a non-synchronized method of an object uses the super () method to call a synchronized method of its super-class, the object is blocked until the invocation has completed.

Synchronized statements

As an alternative to declaring a method synchronized, Java shows flexibility. The programmer can declare a part of the code as synchronized using synchronized statement. The general syntax of declaring a set of statements synchronized is as follows:

synchronized <object><statement>

In the case of synchronized statements, we need to specify an object to act as a monitor for this block. Each Java object can act as a monitor and hence any Java object can be specified synchronized.

In the example considered in Program we can achieve the desired output in another way, that is by declaring the method synchronized by using synchronized statement. For this the code is modified as in Program.

import java.io.*;
class Class_A {
void printValue () {
         try {
              for (int i =0; i<=5; i++) {
                  System.out.print ( i +” “);
Thread.sleep (1000);
}
}
         catch (InterruptedException e) { }
}
}
class ThreadExample implements Runnable {
Class_A ob 1;
       Thread t;
ThreadExample (Class_A c) {
this.ob1 = c;
t = new Thread (this);
}
       public void run () {
            synchronized (ob1) {
ob1.printValue ();
}
}
       public static void main (String [ ] args) {
Class_A ca = new Class_A ();
ThreadExample one=new ThreadExample (ca);
one.t.start ();
ThreadExample two=new ThreadExample (ca);
two.t.start ();
ThreadExample three=new ThreadExample (ca);
three.t.start ();
}
}

The output of Program is the following:

0 1 2 3 4 5 0 1 2 3 4 5

0 1 2 3 4 5

Deadlocks

Deadlocks occur on several occasions. These can be classified into one of the two following situations, namely, when two or more threads are waiting for two or more locks to be freed or circumstances in the program are such that the locks will never be freed.

Suppose one thread is a monitor of object A and another thread is monitor of the object B. In A and B are in the monitor because they are executing synchronized methods. At one instant, if A tries to access the synchronized method of B and B tries to access the synchronized method of A then they will enter into the Deadlock state.

If two or more threads are waiting for each other to unlock a synchronized block of code, where each thread relies on another to do the unlocking, then this situation is called deadlock situation. Deadlock situation occurs if the programmer is not careful while writing the synchronized methods.

You’ll also like:

  1. Synchronization in Java Example
  2. Method Level Synchronization in Java Example
  3. Explain Inter-Thread Communication in Multithreading
  4. Explain Java Thread Model
  5. Thread Life Cycle in Java
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW