Home > Java > javaTutorial > Static Synchronization in Java

Static Synchronization in Java

WBOY
Release: 2024-08-30 15:59:04
Original
853 people have browsed it

The execution of thread becomes execution of one thread for each instance if a method of the instance is synchronized but when there are more than once instance of the same class, it becomes a problem which requires synchronization at the class level for providing only one lock for all the instances of the class than having synchronization at the object level and this is called static synchronization in Java which can be performed in two ways, one is by having a static synchronized method and the other one is having a synchronized block of code within the static method.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The syntax is as follows:

synchronized static return_type class_name{}
Copy after login

where return type is the type of the value returned from the class and class_name is the name of the class.

How does Static Synchronization work in Java?

Each instance of the class has a lock on the object of the class in Java. If any static method is made synchronized, then the lock is not on the object of the class, but it is on the class itself. Let us say, there are two objects of a class called obj1 and obj2 and the threads t1 and t2 are operating on the object obj1. Likewise, threads t3 and t4 are operating on the object obj2. If the block of code or the method is made synchronized, no interference can be there between the threads t1 and t2 because both the threads t1 and t2 are referring to the same object obj1 that has a single lock. Likewise, no interference can be there between the threads t3 and t4 because both the threads t3 and t4 are referring to the same object obj2 that has a single lock.

But interference can be there between the threads t1 and t3 because both the threads t1 and t3 have obtained different objects with different locks. Likewise, interference can be there between the threads t2 and t4 because both the threads t2 and t4 have obtained different objects with different locks. We do not want to have any sort of interference between any of the threads and this problem can be solved by using Static Synchronization in Java. A class object is created by the Java Virtual Machine when the loading of the class happens for the first time. The same class will not be loaded again after the loading of the class happens for the first time. An instance of the class is created for every class that is loaded, by the Java Virtual Machine. These instances of the class are called objects and object synchronization can be done using Static Synchronization in Java.

Example to Implement Static Synchronization in Java

Below is the example:

Example #1

Program to demonstrate Static Synchronization in Java:

Code:

//a class called check is defined
class check
{
// a method called Line is define and only one thread will be able to access this method at once because it is synchronized
synchronized public void Line()
{
//a for loop is defined to loop from values 0 to 3
for (int r = 1; r <5; r++)
{
System.out.println(r);
try
{
Thread.sleep(390);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
}
//a class called Trap is defend which extends a thread of the class
class Trap extends Thread
{
//a variable of the class check is defined
check line1;
//a constructor of the class trap is defined with check class variable
Trap(check line1)
{
this.line1 = line1;
}
//the standard run method is overridden
public void run()
{
line1.Line();
}
}
//a class called program is defined
public class program
{
//main method is called
public static void main(String[] args)
{
//an instance of the class check is defined
check object = new check();
// two threads of the class trap is created and they share the same object
Trap tra1 = new Trap(object);
Trap tra2 = new Trap(object);
//Beginning the execution of two threads
tra1.start();
tra2.start();
}
}
Copy after login

Output:

Static Synchronization in Java

Explanation: In the above program, a class called check is defined. Then a method called Line is defined and only one thread will be able to access this method at once because it is synchronized. Then a for loop is defined to loop from values 0 to 3. Then a class called Trap is defend which extends a thread of the class. Then a variable of the class check is defined. a constructor of the class trap is defined with check class variable as the parameter. Then the standard run method is overridden. Then a class called program is defined. Then the main method is called. Then an instance of the class check is defined. Then two threads of the class trap, tra1 and tra2 are created and they share the same object. Then the execution of the two threads tra1 and tra2 begins in such a way that execution of one thread do not interrupt the execution of other thread or execution of one thread do not lock the execution of the other thread. The output of the program is as shown in the snapshots above.

Advantages of Static Synchronization in Java

There are several advantages. They are:

  • The problems of data inconsistency can be solved using Static Synchronization in Java.
  • By using Static Synchronization in Java, an object can be accessed by only one thread at a given point of time. The object that is to be accessed by only one thread must be declared with the keyword synchronized.
  • The concurrency of different threads and different processes can be managed by using Static Synchronization in Java. That is two or more threads, or two or more processes can be executed simultaneously at the same time making sure that one thread does not lock or interrupt the execution of other threads.

Conclusion

In this tutorial, we understand the concept of Static Synchronization in Java through definition, syntax to declare static synchronization in Java, working of Static Synchronization in java through examples and their outputs and advantages of static synchronization in java.

The above is the detailed content of Static Synchronization in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template