<div dir="ltr" style="text-align: justify;" trbidi="on">A synchronized method or block works on a given monitor. Synchronized non-static methods all synchronize on the Java instance of a class. Each instance has a lock monitor. For the case of static methods, what object does static synchronized methods use for locking? The static synchronized statements obtain a lock on the Class object.<br />
If you make any static method as synchronized, the lock will be on the class not on object.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/staticsynchronization.jpg" /></div>
<p><i><b>Why we use Static Synchronized Block/Method?</b></i> <br />
Suppose there are two objects of a shared class(e.g. Account) named object1 and object2.In case of synchronized method and synchronized block there cannot be interference between t1 and t2 or t3 and t4 because t1 and t2 both refers to a common object that have a single lock.But there can be interference between t1 and t3 or t2 and t4 because t1 acquires another lock and t3 acquires another lock. I want no interference between t1 and t3 or t2 and t4. <i><b>Static synchronization solves this problem</b></i>. </p>
<div align='center' id="ads-id"></div>
<p>The <b><a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">JVM </a></b>creates a Class object when the class is loaded (When it is used for the first time). Once a class is loaded into a <b><a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">JVM</a></b>, the same class will not be loaded again. The <b><a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">JVM </a></b>creates one instance of Class for each class that is loaded, The Class instances are Objects and can be synchronized via static synchronized methods.<br />
<b><br />
For example</b></p>
<pre class="highlight" name="code">class MyClass { 
 ... 
 public synchronized static someMethod() { 
 ... 
 } 
 ... 
} 
</pre>
<p>It is the equivalent to the following <i><b>static synchronized block</b></i>:</p>
<pre class="highlight" name="code">synchronized ( MyClass.class ) { 
... 
} 
</pre>
<p><b>Example of static synchronization</b><br />
In this example we are applying synchronized keyword on the static method to perform static synchronization.</p>
<pre class="highlight" name="code">class Account{ 
 
 synchronized static void showAccount(String accountName){ 
 System.out.println("My account name is "+accountName+" Holder Name is "+Thread.currentThread().getName()); 
 try{ 
 Thread.sleep(500); 
 }catch(Exception e){} 
 } 
} 
 
class MyThread1 extends Thread{ 
 public void run(){ 
 Account.showAccount("Dineshonjava.com"); 
 } 
} 
 
class MyThread2 extends Thread{ 
 public void run(){ 
 Account.showAccount("Linkedin.com"); 
 } 
} 
 
class MyThread3 extends Thread{ 
 public void run(){ 
 Account.showAccount("Facebook.com"); 
 } 
} 
 
class MyThread4 extends Thread{ 
 public void run(){ 
 Account.showAccount("Twitter.com"); 
 } 
} 
 
class StaticSyncDemo{ 
 public static void main(String t[]){ 
 MyThread1 t1 = new MyThread1(); 
 MyThread2 t2 = new MyThread2(); 
 MyThread3 t3 = new MyThread3(); 
 MyThread4 t4 = new MyThread4(); 
 t1.setName("DAV JavaServices"); 
 t2.setName("dinesh.rajput"); 
 t3.setName("dineshonjava"); 
 t4.setName("admin@dineshonjava.com"); 
 t1.start(); 
 t2.start(); 
 t3.start(); 
 t4.start(); 
 } 
} 
</pre>
<p>
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/05/staticsync.png" /></div>
<p>
<b>Synchronized block on a class lock:</b><br />
The block synchronizes on the lock of the object denoted by the reference .class. A static synchronized method<b><i> showAccount(String accountName)</i></b> in class <b><i>Account </i></b>is equivalent to the following declaration:</p>
<pre class="highlight" name="code">static void showAccount(String accountName) { 
 synchronized (Account.class) { // Synchronized block on class A 
 // ... 
 } 
} 
</pre>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/synchronized-block/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/deadlock-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/synchronized-block/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/deadlock-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '475'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…