×

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action.

Threads are autonomous. If an exception occurs in one thread, it does not affect the others. Moreover, it utilizes shared memory.

Thread Class in Java

To facilitate thread development, Java offers the Thread class. To generate and manage threads, the Thread class offers constructors and methods. Extending the Object class, the Thread class implements the Runnable interface.

Thread methods in Java

  • start() - This function begins the thread's execution.
  • Run() – This function executes a thread's command.
  • sleep()- puts a thread to sleep for the provided period of time.
  • currentThread()-returns a reference to the thread object that is currently running.
  • Join () - It watches for a thread to terminate.
  • getPriority() – This function returns the thread's priority.
  • setPriority()-modifies the thread's priority.
  • The thread's name is returned via the getName() function.
  • setName()-modifies the thread's name.
  • getId()- The Thread's id is returned via the getId() method.
  • isAlive()- It determines whether the Thread is active.
  • suspend()- The Thread is suspended using the suspend().
  • resume()-The suspended Thread can be resumed using the resume() function.
  • stop()-Use the stop() function to end the Thread.
  • interrupt()-The Thread is interrupted by interrupt().
  • isinterrupted()-The function isinterrupted() checks to see if the Thread has been halted.
  • interrupted()- It checks to see if the running Thread has been paused.
  • tostring()-The Thread's name, priority, and thread group are all included in the string representation of this Thread returned by the function toString().
  • notify()- Use the notify() function to send a notification to just one Thread that is waiting for a specific item.
  • notifyAll()- Use the notifyAll() function to send the notification to every waiting thread for a specific object.

Life Cycle of a Thread

Any one of the five states is possible for a thread. It is claimed that the Java thread life cycle has only four possible states: new, runnable, non-runnable, and terminated. No running state exists.

However, we are explaining it in the context of the five states to help with the threading.

JVM regulates a thread's life cycle when it is written in Java. These are the java thread states:

1) New

If you create an instance of the Thread class before calling the start() method, the Thread is in a new state.

2) Runnable (Operable)

After calling the start() method, the Thread is in a runnable state, but the thread scheduler has not chosen it to be the active Thread.

3)Running(Exercise)

If the thread scheduler has chosen it, the Thread is in the running state.

4) Non-Runnable (Blocked)

The Thread is still active in this state but is not currently permitted to execute.

5) Terminated (Discontinued)

When a thread's run() method exits, it is said to be in a terminated or dead state.

Thread Creation:

A thread can be made in one of two ways:

  1. Extending the thread class.
  2. By the implementation of the runnable interface.

The constructors and methods provided by the thread class allow users to create and manage threads. Extending the Object class, the Thread class implements the Runnable interface.

1. Extending the thread class example:

class Multithread extends Thread
{ 
public void run()
{ 
System.out.println("the thread is in the running state..."); 
} 
public static void main(String args[])
{ 
Multithread thr = new Multithread(); 
thr.start(); 
} 
}

Output:

the Thread is in the running state...

2. By the implementation of a runnable interface example:

class Multithread implements Runnable 
{ 
public void run()
{ 
System.out.println("the thread is in the running state..."); 
} 
public static void main(String args[])
{ 
Multithread mth = new Multithread(); 
        Thread thr=new Thread(mth);
thr.start(); 
} 
}

Output:

the Thread is in the running state...

Your class object will not be considered as a thread object if you are not extending the Thread class. Thus, you must explicitly create an instance of the Thread class. In order for the run() method of your class to function, we are supplying the object of your class that implements Runnable.

Sleep() method in Java

A thread can be put to sleep for a specified period of time using the sleep() function of the Thread class.

Example:

class Sleep extends Thread
{ 
public void run()
{ 
  for(int i=1; i<6; i++)
{ 
try
{
Thread.sleep(600);
}
catch(InterruptedException ex)
{
System.out.println(ex);
} 
System.out.println(i); 
  } 
} 
public static void main(String args[])
{ 
  Sleep slt1 = new Sleep(); 
  Sleep slt2 = new Sleep(); 
  slt1.start(); 
slt2.start(); 
} 
}

Output:

1
1
2
2
3
3
4
4
5
5

Thread Synchronization

Java's thread synchronization feature allows for the management of multiple threads' access to any shared resource.

Where we wish to allow only one Thread to access the shared resource, synchronization is a better alternative.

WHY…...?

The primary purpose of synchronization is to avoid consistency issues and prevent thread interference.

Thread synchronization can be divided into two categories: inter-thread communication and mutual exclusion.

  • Again, the mutual exclusion is categorized into three categories
  • Method of synchronization.
  • Block of synchronization.
  • Static synchronization.

Method of Synchronization:

Any method that has been declared as synchronized is referred to as method of synchronization.

An object is locked for any shared resource using a synchronized method.

A synchronized method call by a thread immediately gets the lock for that object and releases it after the method has finished running.

Example:

class Multiplication
{ 
synchronized void printMultiplication(int a) //method of synchronization
{
  for(int i=1; i<=5; i++)
{ 
            System.out.println(a*i); 
  try
{ 
  Thread.sleep(500); 
  }
catch(Exception ex)
{
System.out.println(ex);
} 
  } 
  } 
} 
class Testthread1 extends Thread
{ 
Multiplication m; 
Testthread1(Multiplication m)
{ 
this.m = m; 
  } 
  public void run()
{ 
m.printMultiplication(6); 
  } 
} 
class Testthread2 extends Thread
{ 
Multiplication m; 
Testthread2(Multiplication m)
{ 
this.m = m; 
} 
public void run()
{ 
m.printMultiplication(50); 
  } 
} 
class Synchronization
{ 
public static void main(String args[])
{ 
Multiplication object = new Multiplication();
Testthread1 thr1 = new Testthread1(object); 
Testthread2 thr2 = new Testthread2(object); 
thr1.start(); 
thr2.start(); 
} 
}

Output:

6
12
18
24
30
50
100
150
200
250

Block of Synchronization:

Any specific resource of the method can be synchronized using the synchronized block.

If your method contains 50 lines of code, but you want to synchronize 5 of them, you can use a synchronized block.

If you include all of the method's codes in the synchronized block, it will function similarly to the synchronous procedure.

Reminders for synchronized blocks

  • To lock an object for any shared resource, use a synchronized block.
  • The synchronized block's scope is less extensive than the methods.

Example: It is a block. And, we are not going to write the main method, and hence we will not get the output.

class Multiplication
{ 
void printMultiplcation(int a)
{ 
synchronized(this) //block of synchronization
{
  for(int i=1; i<=5; i++)
{ 
  System.out.println(a*i); 
  try
{ 
  Thread.sleep(500); 
  }
catch(Exception ex)
{
System.out.println(ex);
} 
  } 
  } 
}//end
}

Static synchronization:

If you synchronize any static method, the lock will be on the class rather than the object.

Example:

class Multiplication
{
  synchronized static void printMultiplication(int a)
{ 
  for(int i=1;i<=10;i++)
{ 
  System.out.println(a*i); 
  try
{ 
  Thread.sleep(500); 
  }
catch(Exception ex)
{
            
} 
  } 
} 
} 
class Testthread1 extends Thread
{ 
public void run()
{ 
Multiplication.printMultiplication(10); 
} 
} 
class Testthread2 extends Thread
{ 
public void run()
{ 
Multiplication.printMultiplication(10); 
} 
} 
class Testthread3 extends Thread
{ 
public void run()
{ 
Multiplication.printMultiplication(100); 
} 
} 
class Testthread4 extends Thread
{ 
public void run()
{ 
Multiplication.printMultiplication(1000); 
} 
} 
class Synchronisation
{ 
public static void main(String args[])
{ 
Testthread1 thr1 = new Testthread1(); 
Testthread2 thr2 = new Testthread2(); 
Testthread3 thr3 = new Testthread3(); 
Testthread4 thr4 = new Testthread4(); 
thr1.start(); 
thr2.start(); 
thr3.start(); 
thr4.start(); 
} 
}

Output:

10
20
30
40
50
60
70
80
90
100
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
10
20
30
40
50
60
70
80
90
100
100
200
300
400
500
600
700
800
900
1000

Inter-thread communication:

Having synchronized threads communicate with one another is what inter-thread communication or cooperation is all about.

Cooperation (Inter-Thread Communication) is a method that pauses a running thread. Another thread may enter (or lock) in the critical area of a thread's code without interfering with its critical section executed.

It is implemented via the following Object class methods:

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

Example:

class Employee
{ 
int cash=100000; 
synchronized void withdraw(int cash)
{ 
System.out.println("we are going to withdraw a cash..."); 
  if(this.cash<cash)
{ 
System.out.println("Low balance; waiting for the deposit of money..."); 
try
{
wait();
}
catch(Exception ex)
{
            
} 
} 
this.cash -= cash; 
System.out.println("the withdrawal is completed..."); 
} 
  synchronized void deposit(int cash)
{ 
System.out.println("the deposit of money start..."); 
this.cash += cash; 
System.out.println("the money deposited... "); 
notify(); 
} 
} 
class Main
{ 
public static void main(String args[])
{ 
final Employee e = new Employee(); 
new Thread()
{ 
public void run()
{
e.withdraw(150000);
} 
}.start(); 
new Thread()
{ 
public void run()
{
e.deposit(100000);
} 
}.start(); 
  }
}

Output:

we are going to withdraw a cash...
Low balance; waiting for the deposit of money...
the deposit of money start...
the money deposited... 
the withdrawal is completed...

Related Topics

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.