search
HomeJavajavaTutorialStatic Synchronization in Java

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{}

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 
<p><strong>Output:</strong></p>
<p><img  src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500474663661.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Static Synchronization in Java" ></p>


<p><strong>Explanation:</strong> 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.</p>
<h3 id="Advantages-of-Static-Synchronization-in-Java">Advantages of Static Synchronization in Java</h3>
<p>There are several advantages. They are:</p>
  • 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!

Statement
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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools