Importance of Thread Synchronization in Java
Last Updated :
11 Jan, 2025
Thread synchronization in Java is important for managing shared resources in a multithreaded environment. It ensures that only one thread can access a shared resource at a time, which enhances the overall system performance and prevents race conditions and data corruption.
Why is Thread Synchronization Important?
In a multithreaded environment, threads may compete for shared resources i.e. files, memory, etc. Without synchronization, simultaneous access can lead
- Race Conditions: Multiple Threads interchanging shared data at the same time and it results an unpredictable output.
- Data Corruption: Incomplete or corrupted data when multiple threads modify the same resource simultaneously.
Real-world Example:
Imagine multiple computers connected to a single printer:
- If two computers send print jobs simultaneously, the printer might mix their outputs and that leads to invalid results.
- Similarly, threads accessing the same resource without coordination can produce inconsistent data.
Thread Priorities
In Java, thread priorities determine the execution order, allowing higher-priority threads to preempt lower ones and access resources first. However, when threads of equal priority compete for the same resource, conflicts can lead to inconsistent or erroneous outcomes.
Mechanisms for Thread Synchronization
Thread synchronization basically refers to The concept of one thread execute at a time and the rest of the threads are in waiting state. This process is known as thread synchronization. It prevents the thread interference and inconsistency problem.
Synchronization is build using locks or monitor. In Java, a monitor is an object that is used as a mutually exclusive lock. Only a single thread at a time has the right to own a monitor. When a thread gets a lock then all other threads will get suspended which are trying to acquire the locked monitor. So, other threads are said to be waiting for the monitor, until the first thread exits the monitor. In a simple way, when a thread request a resource then that resource gets locked so that no other thread can work or do any modification until the resource gets released.
Types of Thread Synchronization
Thread synchronization are of two types:
Mutual Exclusion
While sharing any resource, this will keep the thread interfering with one another i.e. mutual exclusive. We can achieve this via
- Synchronized Method
- Synchronized Block
- Static Synchronization
1. Synchronized Method
We can declare a method as synchronized using the synchronized keyword. This will make the code written inside the method thread-safe so that no other thread will execute while the resource is shared.
Implementation:
We will be proposing prints the two threads simultaneously showing the asynchronous behavior without thread synchronization.
Example 1: Here, we will use non-synchronized method.
Java
// Extending Thread class
public class PrintTest extends Thread {
// Non synchronized Code
// Method 1
public void printThread(int n)
{
// This loop will print the
// currently executed thread
for (int i = 1; i <= 10; i++) {
System.out.println("Thread " + n
+ " is working...");
// Try block to check for exceptions
try {
// Pause the execution of current thread
// for 0.600 seconds using sleep() method
Thread.sleep(600);
}
// Catch block to handle the exceptions
catch (Exception ex) {
// Overriding existing toString() method and
// prints exception if occur
System.out.println(ex.toString());
}
}
// Display message for better readability
System.out.println("--------------------------");
try {
// Pause the execution of current thread
// for 0.1000 millisecond or 1sec using sleep
// method
Thread.sleep(1000);
}
catch (Exception ex) {
// Printing the exception
System.out.println(ex.toString());
}
}
}
// Class 2
// Helper class extending Thread Class
public class Thread1 extends Thread {
// Declaring variable of type Class1
PrintTest test;
// Constructor for class1
Thread1(PrintTest p) { test = p; }
// run() method of this class for
// entry point for thread1
public void run()
{
// Calling method 1 as in above class
test.printThread(1);
}
}
// Class 3
// Helper class extending Thread Class
public class Thread2 extends Thread {
// Declaring variable of type Class1
PrintTest test;
// Constructor for class2
Thread2(PrintTest p) { test = p; }
// run() method of this class for
// entry point for thread2
public void run() { test.printThread(2); }
}
// Class 4
// Main class
public class SynchroTest {
// Main driver method
public static void main(String[] args)
{
// Creating object of class 1 inside main() method
PrintTest p = new PrintTest();
// Passing the same object of class PrintTest to
// both threads
Thread1 t1 = new Thread1(p);
Thread2 t2 = new Thread2(p);
// Start executing the threads
// using start() method
t1.start();
t2.start();
// This will print both the threads simultaneously
}
}
Output:
Now using synchronized method, it will lock the object for the shared resource and gives the consistent output.
Example 2: Below example, lock the object for the shared resource.
Java
// Java Program Illustrating Lock the Object for
// the shared resource giving consistent output
public class PrintTest extends Thread {
// synchronized method will lock the object and
// releases when thread is terminated
synchronized public void printThread(int n)
{
for (int i = 1; i <= 10; i++) {
System.out.println("Thread " + n
+ " is working...");
try {
// pause the execution of current thread
// for 600 millisecond
Thread.sleep(600);
}
catch (Exception ex) {
// overrides toString() method and prints
// exception if occur
System.out.println(ex.toString());
}
}
System.out.println("--------------------------");
try {
// pause the execution of current thread for
// 1000 millisecond
Thread.sleep(1000);
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
// creating thread1 extending Thread Class
public class Thread1 extends Thread {
PrintTest test;
Thread1(PrintTest p) { test = p; }
public void run() // entry point for thread1
{
test.printThread(1);
}
}
// creating thread2 extending Thread Class
public class Thread2 extends Thread {
PrintTest test;
Thread2(PrintTest p) { test = p; }
public void run() // entry point for thread2
{
test.printThread(2);
}
}
public class SynchroTest {
public static void main(String[] args)
{
PrintTest p = new PrintTest();
// passing the same object of class PrintTest to
// both threads
Thread1 t1 = new Thread1(p);
Thread2 t2 = new Thread2(p);
// start function will execute the threads
t1.start();
t2.start();
}
}
Output:
2. Synchronized Block
If we declare a block as synchronized, only the code which is written inside that block is executed sequentially not the complete code. This is used when we want sequential access to some part of code or to synchronize some part of code.
Syntax:
synchronized (object reference)
{
// Insert code here
}
Example:
Java
// Java Program Illustrating Synchronized Code
// Using synchronized block
// Helper class extending Thread class
class PrintTest extends Thread {
// To print the thread
public void printThread(int n)
{
// Making synchronized block that
// makes the block synchronized
synchronized (this)
{
// Iterating using for loop
for (int i = 1; i <= 10; i++) {
System.out.println("Thread " + n
+ " is working...");
// Try block to check for exceptions
try {
// Making thread to pause for 0.6
// seconds
Thread.sleep(600);
}
// Catch block to handle exceptions
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
System.out.println("--------------------------");
try {
// Making thread t osleep for 1 sec
Thread.sleep(1000);
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
// Helper class extending Thread class
class Thread1 extends Thread {
PrintTest test;
Thread1(PrintTest p) { test = p; }
public void run() { test.printThread(1); }
}
// Helper class extending Thread class
class Thread2 extends Thread {
PrintTest test;
Thread2(PrintTest p) { test = p; }
public void run() { test.printThread(2); }
}
// Main class
class SynchroTest {
public static void main(String[] args) {
// Creating instance for class 1 inside main()
PrintTest p = new PrintTest();
// Creating threads and
// passing same object
Thread1 t1 = new Thread1(p);
Thread2 t2 = new Thread2(p);
// Starting these thread using start() method
t1.start();
t2.start();
}
}
Output:
3. Static Synchronization
In this, the synchronized method is declared as "static" which means the lock or monitor is applied on the class not on the object so that only one thread will access the class at a time.
Example:
Java
// Java Program Illustrate Synchronized
// Using static synchronization
// Helper class
class PrintTest extends Thread {
// Static synchronization locks the class PrintTest
synchronized public static void printThread(int n)
{
for (int i = 1; i <= 10; i++) {
System.out.println("Thread " + n
+ " is working...");
// Try block to check for exceptions
try {
// making thread to sleep for 0.6 seconds
Thread.sleep(600);
}
// Catch block to handle the exceptions
catch (Exception ex) {
System.out.println(ex.toString());
}
}
System.out.println("--------------------------");
try {
Thread.sleep(1000);
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
// Helper class extending Thread class
class Thread1 extends Thread {
// run() method for thread
public void run()
{
// Passing the class not the object
PrintTest.printThread(1);
}
}
// Helper class extending Thread class
class Thread2 extends Thread {
public void run()
{
// Passing the class not the object
PrintTest.printThread(2);
}
}
// Main class
class SynchroTest {
public static void main(String[] args)
{
// No shared object
// Creating objects of class 2 and 3 that
// are extending to Thread class
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
// Starting thread with help of start() method
t1.start();
t2.start();
}
}
Output:
Similar Reads
Basics of Java
Learn Java - A Beginners Guide for 2024If you are new to the world of coding and want to start your coding journey with Java, then this learn Java a beginners guide gives you a complete overview of how to start Java programming. Java is among the most popular and widely used programming languages and platforms. A platform is an environme
10 min read
Introduction to JavaJava is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Similarities and Difference between Java and C++Nowadays Java and C++ programming languages are vastly used in competitive coding. Due to some awesome features, these two programming languages are widely used in industries as well as competitive programming. C++ is a widely popular language among coders for its efficiency, high speed, and dynamic
6 min read
Setting up Environment Variables For Java - Complete Guide to Set JAVA_HOMEIn the journey to learning the Java programming language, setting up environment variables for Java is essential because it helps the system locate the Java tools needed to run the Java programs. Now, this guide on how to setting up environment variables for Java is a one-place solution for Mac, Win
6 min read
Java SyntaxJava is an object-oriented programming language that is known for its simplicity, portability, and robustness. The syntax of Java programming language is very closely aligned with C and C++, which makes it easier to understand. Java Syntax refers to a set of rules that define how Java programs are w
6 min read
Java Hello World ProgramJava is one of the most popular and widely used programming languages and platforms. In this article, we will learn how to write a simple Java Program. This article will guide you on how to write, compile, and run your first Java program. With the help of Java, we can develop web and mobile applicat
6 min read
Differences Between JDK, JRE and JVMUnderstanding the difference between JDK, JRE, and JVM plays a vital role in understanding how Java works and how each component contributes to the development and execution of Java applications. JDK: JDK stands for Java Development Kit. It is a set of development tools and libraries used to create
4 min read
How JVM Works - JVM ArchitectureJVM (Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE (Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one
7 min read
Java IdentifiersAn identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names used to identify programming elements. Every Java Variable must be identified with a unique name.Example:public class Test{ public static void main(String[] args) { int a = 2
2 min read
Variables & DataTypes in Java
Java VariablesIn Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.Key Components of Variables in Java:A variable in Java has three components, which are listed below:Data Type: Defines the kind
9 min read
Scope of Variables in JavaThe scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about
7 min read
Java Data TypesJava is statically typed and also a strongly typed language because each type of data, such as integer, character, hexadecimal, packed decimal etc. is predefined as part of the programming language, and all constants or variables defined for a given program must be declared with the specific data ty
14 min read
Operators in Java
Java OperatorsJava operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently. They can be classified into different categories based on their functionality. In this article, we will explore different
15 min read
Java Arithmetic Operators with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
6 min read
Java Assignment Operators with ExamplesOperators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read
Java Unary Operator with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions be it logical, arithmetic, relational, etc. They are classified based on the functionality they p
8 min read
Java Relational Operators with ExamplesOperators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
10 min read
Java Logical Operators with ExamplesLogical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
Java Ternary OperatorOperators constitute the basic building block of any programming language. Java provides many types of operators that can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provi
5 min read
Bitwise Operators in JavaIn Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T
6 min read
Packages in Java
Flow Control in Java
Loops in Java
Jump Statements in Java
Arrays in Java
Arrays in JavaArrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Java Multi-Dimensional ArraysMultidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
Jagged Array in JavaIn Java, a Jagged array is an array that holds other arrays. When we work with a jagged array, one thing to keep in mind is that the inner array can be of different lengths. It is like a 2D array, but each row can have a different number of elements.Example:arr [][]= { {10,20}, {30,40,50,60},{70,80,
6 min read
Strings in Java
Java StringsIn Java, a String is the type of object that can store a sequence of characters enclosed by double quotes, and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowi
9 min read
String Class in JavaA string is a sequence of characters. In Java, objects of the String class are immutable, which means they cannot be changed once created. In this article, we are going to learn about the String class in Java.Example of String Class in Java:Java// Java Program to Create a String import java.io.*; cl
7 min read
StringBuffer Class in JavaThe StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.Features of StringBuffer ClassThe key features of StringBuffer c
11 min read
Java StringBuilder ClassIn Java, the StringBuilder class is a part of the java.lang package that provides a mutable sequence of characters. Unlike String (which is immutable), StringBuilder allows in-place modifications, making it memory-efficient and faster for frequent string operations.Declaration:StringBuilder sb = new
7 min read
OOPS in Java
Java OOP(Object Oriented Programming) ConceptsJava Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Classes and Objects in JavaIn Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
11 min read
Java MethodsJava Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Access Modifiers in JavaIn Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
7 min read
Wrapper Classes in JavaA Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in a wrapper class, it contains a field, and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object. Let's check on the wr
6 min read
Need of Wrapper Classes in JavaFirstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These metho
3 min read