{"id":12894,"date":"2023-02-14T08:53:47","date_gmt":"2023-02-14T08:53:47","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=12894"},"modified":"2023-10-16T07:00:47","modified_gmt":"2023-10-16T07:00:47","slug":"synchronization-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/","title":{"rendered":"Synchronization in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<p>Synchronization in Java is a critical concept, especially in the context of multi-threaded programming. In a multi-threaded environment, multiple threads can access and modify shared resources concurrently, leading to race conditions and unpredictable behavior. Mechanisms of Synchronization in Java provide a way to control access to shared resources, ensuring thread safety and preventing data corruption.<\/p>\n<p>In this article, we will explore the topic of synchronization in Java in-depth. We&#8217;ll discuss the importance of synchronization, various synchronization techniques, and the Java keywords synchronized and volatile. Additionally, we&#8217;ll cover common synchronization classes and best practices for writing thread-safe Java code. Understanding synchronization is essential for building robust and reliable multi-threaded applications in Java.<\/p>\n<h2>What is Synchronization in Java?<\/h2>\n<p>Synchronization in Java is a mechanism used to control access to shared resources in a multi-threaded environment. The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption and inconsistent results caused by multiple threads accessing and modifying the same data simultaneously.<\/p>\n<p>Java provides the <strong>synchronized<\/strong> keyword to implement synchronization. When a method or a block of code is marked as synchronized, it can only be accessed by one thread at a time. This is achieved by acquiring a lock on the object associated with the synchronized method or block.<\/p>\n<p>Synchronization is an important concept in multi-threaded programming, as it ensures that shared data remains consistent and in a valid state, even in the presence of multiple threads accessing and modifying it simultaneously.<\/p>\n<h2>Types of Synchronization in Java<\/h2>\n<p>There are two types of synchronization in Java:<\/p>\n<ul>\n<li><strong>Method-Level Synchronization:<\/strong> In method-level synchronization, the entire method is marked as synchronized and can only be accessed by one thread at a time. This is achieved by acquiring a lock on the object associated with the method.<\/li>\n<li><strong>Block-Level Synchronization:<\/strong> In block-level synchronization, only a portion of the method is marked as synchronized. This is achieved by wrapping the portion of code that requires synchronization within a synchronized block and acquiring a lock on a specified object.<\/li>\n<\/ul>\n<p>Both method-level and block-level synchronization can be used to control access to shared resources in a multi-threaded environment, but block-level synchronization provides more fine-grained control over the synchronization of a program. Block-level synchronization allows multiple threads to access different parts of a shared resource simultaneously, which can improve performance in certain cases.<\/p>\n<p>Let us discuss both of these types of Synchronization in detail with the help of suitable examples.<\/p>\n<h2>Java Synchronized Method<\/h2>\n<p>In Java Programming Language, we can make any method as Synchronized Method by using the synchronized keyword. This is used to lock a particular object for any shared resource.<\/p>\n<p>When a thread calls a java synchronized method, the lock for that object is automatically acquired and released when the thread completes its work.<\/p>\n<h3>Syntax for making Java Synchronized Method:<\/h3>\n<pre><code>Acess_modifiers synchronized return_type method_name (Method_Parameters) {\n    \/\/ Method Statements\n}<\/code><\/pre>\n<p>Let us first see the problem that arises if there is no Synchronization.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12525 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12525 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12525 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12525 .wpsm_nav-tabs > li.active > a, #tab_container_12525 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12525 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12525 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12525 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12525 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12525 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12525 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12525 .wpsm_nav-tabs > li > a:hover , #tab_container_12525 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12525 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12525 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12525 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12525 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12525 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12525 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12525 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12525 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12525 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12525 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12525 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12525\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12525\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12525_1\" aria-controls=\"tabs_desc_12525_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12525\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12525_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Table{  \r\n    void printTable(int n){ \r\n        for(int i=1;i&lt;=5;i++){  \r\n    \t\tSystem.out.println(n*i);  \r\n     \r\n    \t\ttry{  \r\n    \t\t\tThread.sleep(400);  \r\n    \t\t}catch(Exception e){\r\n    \t\t\tSystem.out.println(e);\r\n    \t\t}  \r\n        }  \r\n    }  \r\n}  \r\n  \r\nclass MyThread1 extends Thread{  \r\n    Table t;  \r\n    MyThread1(Table t){  \r\n        this.t=t;  \r\n    }  \r\n\r\n    public void run(){  \r\n        t.printTable(5);  \r\n    }  \r\n}  \r\n\r\nclass MyThread2 extends Thread{  \r\n    Table t;  \r\n    MyThread2(Table t){  \r\n        this.t=t;  \r\n    }\r\n    \r\n    public void run(){  \r\n        t.printTable(100);  \r\n    }  \r\n}  \r\n  \r\nclass Main{  \r\n    public static void main(String args[]){ \r\n        \/\/shared object\r\n        Table obj = new Table();  \r\n        MyThread1 t1=new MyThread1(obj);  \r\n        MyThread2 t2=new MyThread2(obj);  \r\n        t1.start();  \r\n        t2.start();  \r\n    }  \r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12525 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12525 a\"),jQuery(\"#tab-content_12525\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>5\n100\n10\n200\n15\n300\n20\n400\n25\n500<\/code><\/pre>\n<p>Here, we have got inconsistent output, since the object of class \u201cTable\u201d is being modified by the two methods concurrently. <\/p>\n<p>We can overcome this problem by using the synchronized method as shown in the code below:<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12526 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12526 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12526 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12526 .wpsm_nav-tabs > li.active > a, #tab_container_12526 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12526 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12526 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12526 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12526 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12526 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12526 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12526 .wpsm_nav-tabs > li > a:hover , #tab_container_12526 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12526 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12526 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12526 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12526 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12526 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12526 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12526 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12526 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12526 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12526 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12526 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12526\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12526\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12526_1\" aria-controls=\"tabs_desc_12526_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12526\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12526_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Table{  \r\n    synchronized void printTable(int n){ \r\n        for(int i=1;i&lt;=5;i++){  \r\n    \t\tSystem.out.println(n*i);  \r\n     \r\n    \t\ttry{  \r\n    \t\t\tThread.sleep(400);  \r\n    \t\t}catch(Exception e){\r\n    \t\t\tSystem.out.println(e);\r\n    \t\t}  \r\n        }  \r\n    }  \r\n}  \r\n  \r\nclass MyThread1 extends Thread{  \r\n    Table t;  \r\n    MyThread1(Table t){  \r\n        this.t=t;  \r\n    }  \r\n\r\n    public void run(){  \r\n        t.printTable(5);  \r\n    }  \r\n}  \r\n\r\nclass MyThread2 extends Thread{  \r\n    Table t;  \r\n    MyThread2(Table t){  \r\n        this.t=t;  \r\n    }\r\n    \r\n    public void run(){  \r\n        t.printTable(100);  \r\n    }  \r\n}  \r\n  \r\nclass Main{  \r\n    public static void main(String args[]){ \r\n        \/\/shared object\r\n        Table obj = new Table();  \r\n        MyThread1 t1=new MyThread1(obj);  \r\n        MyThread2 t2=new MyThread2(obj);  \r\n        t1.start();  \r\n        t2.start();  \r\n    }  \r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12526 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12526 a\"),jQuery(\"#tab-content_12526\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>5\n10\n15\n20\n25\n100\n200\n300\n400\n500<\/code><\/pre>\n<p>Here, we have modified the method as \u201csynchronized method\u201d. This ensures that the object in use by one thread is not available for the second thread fro making changes.<\/p>\n<h2>Java Synchronized Block<\/h2>\n<p>The synchronized block can be used to perform synchronization on any method resource.<\/p>\n<p>Suppose, we have 100 lines of code in our method but only want to synchronize 5 of them, we can use synchronized block. If we put all of the method&#8217;s codes in the synchronized block, it will work just like the synchronized method.<\/p>\n<h3>Syntax for writing Java Synchronized Block:<\/h3>\n<pre><code>synchronized (object reference expression) {     \n  \/\/statements\/methods    \n} <\/code><\/pre>\n<p>Let us see an example to use Java Synchronized Block.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_12528 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_12528 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_12528 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_12528 .wpsm_nav-tabs > li.active > a, #tab_container_12528 .wpsm_nav-tabs > li.active > a:hover, #tab_container_12528 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_12528 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_12528 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_12528 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_12528 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_12528 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_12528 .wpsm_nav-tabs > li > a:hover , #tab_container_12528 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_12528 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_12528 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_12528 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_12528 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_12528 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_12528 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_12528 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12528 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_12528 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_12528 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_12528 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_12528\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_12528\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_12528_1\" aria-controls=\"tabs_desc_12528_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_12528\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_12528_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Table{  \r\n    void printTable(int n){    \r\n        synchronized(this){\/\/synchronized block    \r\n    \t\tfor(int i=1;i&lt;=5;i++){    \r\n    \t\t\tSystem.out.println(n*i);    \r\n      \r\n    \t\t\ttry{    \r\n    \t\t\t\tThread.sleep(400);    \r\n    \t\t\t}catch(Exception e){\r\n    \t\t\t\tSystem.out.println(e);\r\n    \t\t\t}    \r\n    \t\t}    \r\n        }   \/\/ end of synchronized block \r\n    }\/\/end of the method    \r\n}    \r\n  \r\nclass MyThread1 extends Thread{  \r\n    Table t;  \r\n    MyThread1(Table t){  \r\n        this.t=t;  \r\n    }  \r\n\r\n    public void run(){  \r\n        t.printTable(5);  \r\n    }  \r\n}  \r\n\r\nclass MyThread2 extends Thread{  \r\n    Table t;  \r\n    MyThread2(Table t){  \r\n        this.t=t;  \r\n    }\r\n    \r\n    public void run(){  \r\n        t.printTable(100);  \r\n    }  \r\n}  \r\n  \r\nclass Main{  \r\n    public static void main(String args[]){ \r\n        \/\/shared object\r\n        Table obj = new Table();  \r\n        MyThread1 t1=new MyThread1(obj);  \r\n        MyThread2 t2=new MyThread2(obj);  \r\n        t1.start();  \r\n        t2.start();  \r\n    }  \r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_12528 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_12528 a\"),jQuery(\"#tab-content_12528\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>5\n10\n15\n20\n25\n100\n200\n300\n400\n500<\/code><\/pre>\n<p>Here, instead of making the whole method synchronized, we have only made a particular block \u201csynchronized\u201d. The synchronized block also locks that particular block free from the updation by another thread.<\/p>\n<h2>Need for Synchronized Keyword in Java<\/h2>\n<p>The synchronized keyword in Java is used to control access to a shared resource in a multi-threaded environment. The need for synchronization arises because multiple threads can access and modify shared data simultaneously, leading to inconsistencies and data corruption.<\/p>\n<p>The synchronized keyword is used to provide mutual exclusion, ensuring that only one thread can access the shared resource at a time. This ensures that the shared data remains consistent and in a valid state, even in a multi-threaded environment.<\/p>\n<p>Additionally, java synchronized helps to prevent race conditions, and deadlocks, and improve performance in certain cases. It is used to ensure the proper functioning of multi-threaded programs and to ensure that shared data is not corrupted by multiple threads.<\/p>\n<p>In short, the need for the synchronized keyword in Java arises from the requirement to control access to shared resources in a multi-threaded environment and to ensure that shared data remains consistent and in a valid state. Let\u2019s see the advantages of synchronization in Java.<\/p>\n<h2>Advantages of Synchronization in Java<\/h2>\n<p>Some of the advantages of using Synchronization in Java include<\/p>\n<ul>\n<li><strong>Thread Safety:<\/strong> Synchronization in Java helps to achieve thread safety by ensuring that only one thread can access the shared resource at a time.<\/li>\n<li><strong>Consistency:<\/strong> Synchronization ensures that the shared data is consistent and remains in a valid state throughout the program.<\/li>\n<li><strong>Deadlock Prevention:<\/strong> By using synchronization, deadlocks can be prevented in a multi-threaded environment.<\/li>\n<li><strong>Avoid Race Conditions:<\/strong> Synchronization helps to avoid race conditions that can occur in a multi-threaded environment, where multiple threads try to access a shared resource simultaneously.<\/li>\n<li><strong>Improved Performance:<\/strong> Synchronization can also improve performance in certain cases, as it reduces the number of threads that are waiting for access to a shared.<\/li>\n<\/ul>\n<p>As everyone knows that everything has its pros and cons. We have seen the advantages of synchronization in Java, now, let\u2019s look into disadvantages of synchronization in Java.<\/p>\n<h2>Disadvantages of Synchronization in Java<\/h2>\n<p>Despite having several advantages, Synchronization in Java also have some disadvantages. These disadvantages are listed below:<\/p>\n<ul>\n<li><strong>Performance Overhead:<\/strong> Synchronization can increase the overhead of a program, as it requires the threads to wait for access to the shared resource, leading to a decrease in overall performance.<\/li>\n<li><strong>Deadlocks:<\/strong> Although synchronization can prevent deadlocks, it can also lead to deadlocks if not used correctly.<\/li>\n<li><strong>Starvation:<\/strong> Synchronization can lead to starvation, where a thread waiting for access to a shared resource is blocked indefinitely.<\/li>\n<li><strong>Reduced Concurrency:<\/strong> Synchronization reduces concurrency as it limits the number of threads that can access a shared resource simultaneously. This can lead to decreased overall program efficiency.<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><br \/>\nSynchronization in Java is a fundamental concept that plays a crucial role in ensuring the correctness and reliability of multi-threaded applications. It provides the means to coordinate and control access to shared resources, preventing race conditions and data corruption.<\/p>\n<p>In this article, we&#8217;ve delved into the world of synchronization in Java, covering the importance of thread safety, synchronization techniques, and synchronization classes. We&#8217;ve discussed the use of the synchronized keyword for controlling access to critical sections of code and the volatile keyword for ensuring visibility of shared data.<\/p>\n<p>As you continue to develop Java applications, remember that synchronization is a powerful tool but must be used judiciously. Overusing synchronization can lead to performance bottlenecks, so it&#8217;s essential to strike a balance between thread safety and performance optimization. By mastering the art of synchronization, you can build multithreaded Java applications that are not only efficient but also reliable.<\/p>\n<h2>FAQs Related to Synchronization in Java<\/h2>\n<p>Let us see some Frequently Asked Questions on Synchronization in Java.<\/p>\n<p><strong>1. What are some best practices for writing thread-safe Java code?<\/strong><br \/>\nBest practices for writing thread-safe Java code include minimizing the use of synchronization when not needed, using thread-safe collections, and avoiding unnecessary shared state between threads.<\/p>\n<p><strong>2. What is the synchronized keyword in Java?<\/strong><br \/>\nThe synchronized keyword is used to create synchronized blocks or methods, ensuring that only one thread can execute the synchronized code at a time. It provides mutual exclusion and is a fundamental mechanism for achieving thread safety.<\/p>\n<p><strong>3. When should I use the volatile keyword in Java?<\/strong><br \/>\nThe volatile keyword is used to declare variables that are shared among threads. It ensures that changes to the variable are visible to all threads, preventing caching of the variable&#8217;s value.<\/p>\n<p><strong>4. What is a race condition in multi-threaded programming?<\/strong><br \/>\nA race condition occurs when two or more threads access shared data concurrently, and the final outcome depends on the relative timing of their execution. Race conditions can lead to data corruption and unexpected behavior.<\/p>\n<p><strong>5. Are there any built-in synchronization classes in Java?<\/strong><br \/>\nYes, Java provides built-in synchronization classes like java.util.concurrent.locks.Lock and java.util.concurrent.atomic.Atomic* classes that offer more advanced synchronization mechanisms for complex scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Synchronization in Java is a critical concept, especially in the context of multi-threaded programming. In a multi-threaded environment, multiple threads can access and modify shared resources concurrently, leading to race conditions and unpredictable behavior. Mechanisms of Synchronization in Java provide a way to control access to shared resources, ensuring thread safety and preventing data corruption. [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-12894","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Synchronization in Java<\/title>\n<meta name=\"description\" content=\"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Synchronization in Java\" \/>\n<meta property=\"og:description\" content=\"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-14T08:53:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-16T07:00:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Synchronization in Java\",\"datePublished\":\"2023-02-14T08:53:47+00:00\",\"dateModified\":\"2023-10-16T07:00:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\"},\"wordCount\":1472,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\",\"name\":\"Synchronization in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\",\"datePublished\":\"2023-02-14T08:53:47+00:00\",\"dateModified\":\"2023-10-16T07:00:47+00:00\",\"description\":\"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Synchronization in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Synchronization in Java","description":"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Synchronization in Java","og_description":"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.","og_url":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-02-14T08:53:47+00:00","article_modified_time":"2023-10-16T07:00:47+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Synchronization in Java","datePublished":"2023-02-14T08:53:47+00:00","dateModified":"2023-10-16T07:00:47+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/"},"wordCount":1472,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/","url":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/","name":"Synchronization in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg","datePublished":"2023-02-14T08:53:47+00:00","dateModified":"2023-10-16T07:00:47+00:00","description":"The goal of synchronization is to ensure that only one thread can access a shared resource at a time, to prevent data corruption.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/synchronization-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1676272536720-Synchronization%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/synchronization-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Synchronization in Java"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=12894"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12894\/revisions"}],"predecessor-version":[{"id":18195,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/12894\/revisions\/18195"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=12894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=12894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=12894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}