<div dir="ltr" style="text-align: justify;" trbidi="on">Synchronized block can be used to perform synchronization on any specific resource of the method.<br />
Suppose you have 100 lines of code in your method, but you want to synchronize only 5 lines, you can use synchronized block.<br />
 ;<br />
<b>Block synchronization</b> in java is preferred over method synchronization in java because by using block synchronization you only need to lock the critical section of code instead of whole method.</p>
<p> If you put all the codes of the method in the synchronized block, it will work same as the synchronized method.<br />
<b>Points to remember for Synchronized block</b></p>
<ol>
<li>Synchronized block is used to lock an object for any shared resource.</li>
<li>Scope of synchronized block is smaller than the method.</li>
</ol>
<p>
<b>Syntax to use synchronized block</b></p>
<pre class="highlight" name="code">synchronized (object reference expression) { 
 //code block 
 } 
</pre>
<div align='center' id="ads-id"></div>
<p><b>Example of <i>synchronized </i>block:</b><br />
Let&#8217;s see the simple example of synchronized block.</p>
<pre class="highlight" name="code">class First 
{ 
 public synchronized void display(String msg) 
 { 
 System.out.print ("["+msg); 
 try 
 { 
 Thread.sleep(500); 
 } 
 catch(InterruptedException e) 
 { 
 e.printStackTrace(); 
 } 
 System.out.println ("]"); 
 } 
} 
 
class Second extends Thread 
{ 
 String msg; 
 First fobj; 
 Second (First fp,String str) 
 { 
 fobj = fp; 
 msg = str; 
 start(); 
 } 
 public void run() 
 { 
 synchronized(fobj){ //synchronized block 
 fobj.display(msg); 
 } 
 } 
} 
 
public class SyncBlockDemo 
{ 
 public static void main (String[] args) 
 { 
 First fnew = new First(); 
 Second ss = new Second(fnew, "welcome"); 
 Second ss1 = new Second (fnew,"to"); 
 Second ss2 = new Second(fnew, "dineshonjava.com"); 
 } 
} 
</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/sync3.png"/></div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/synchronization-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/static-synchronization-non-static/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/synchronization-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/static-synchronization-non-static/">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: '476'});
});
</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…