

{"id":16012,"date":"2018-05-18T04:04:39","date_gmt":"2018-05-18T04:04:39","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=16012"},"modified":"2026-05-30T15:14:05","modified_gmt":"2026-05-30T09:44:05","slug":"java-synchronized","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-synchronized\/","title":{"rendered":"What is Java Synchronized Method | Java Synchronized  Block"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1945,&quot;href&quot;:&quot;https:\\\/\\\/docs.oracle.com\\\/javase\\\/tutorial\\\/essential\\\/concurrency\\\/syncmeth.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251007113349\\\/https:\\\/\\\/docs.oracle.com\\\/javase\\\/tutorial\\\/essential\\\/concurrency\\\/syncmeth.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-11 14:48:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-26 09:59:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-03 03:16:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 14:24:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-04 20:15:45&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-13 04:34:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-11 01:47:47&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-11 01:47:47&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In our last Java Tutorial, we studied about\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/deadlock-in-java\/\"><strong>Deadlock in Java<\/strong>.<\/a>\u00a0Here, we are going to learn What is Synchronization in Java. Moreover, we will discuss\u00a0the Java Synchronized method and\u00a0Java Synchronized block.<\/p>\n<p>So, let&#8217;s study Synchronized Methods &amp; Blocks in Java.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"628\" class=\"wp-image-16024 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg\" alt=\"&quot;&lt;yoastmark\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h3>What is Java Synchronized?<\/h3>\n<ul>\n<li>Multi-threaded programs often came to a situation where one resource access by a lot of threads and produce unforeseen results.<\/li>\n<li>So,\u00a0make sure that only one thread can access a particular resource at a given point in time with the help of <em><strong>Java synchronized method<\/strong>.<\/em><\/li>\n<li>Java itself provides a way to create a thread and to synchronizing their task by synchronized blocks. These <em><strong>Java synchronized blocks<\/strong> <\/em>are marked with the name of synchronized keywords.<\/li>\n<\/ul>\n<p><strong>Characteristics of Synchronized method in Java:<\/strong><\/p>\n<ul>\n<li><strong>No Collision:<\/strong> It ensures the complete execution of one thread that is currently running. When the execution of the current running thread gets completed then only the other thread gets executed. If a thread tries to come in the queue for further processing then the method temporarily blocks it so that the previous thread gets completed.<\/li>\n<li><strong>Fluent Processing:<\/strong> It allows all the threads of a process to run smoothly without any hindrance.<\/li>\n<li><strong>Visible Changes:<\/strong> If any of the threads modifies the object during execution then it will be displayed to all other threads also.<\/li>\n<\/ul>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/decision-making-in-java\/\">Do you know What is Decision Making in Java?<\/a><\/strong><\/p>\n<p><strong>The syntax of Synchronized in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\">synchronized(sync_object)\r\n{\r\n}<\/pre>\n<p>Java synchronization implements inside with a concept called as <em>monitors<\/em>. This monitor own by only one thread for a given time.<\/p>\n<h3>Example of Synchronized in Java<\/h3>\n<pre class=\"EnlighterJSRAW\">import java.io.*;\r\nimport java.util.*;\r\nclass Sender\r\n{\r\npublic void send(String msg)\r\n{\r\nSystem.out.println(\"Sending\\t\" + msg );\r\ntry\r\n{\r\nThread.sleep(1000);\r\n}\r\ncatch (Exception e)\r\n{\r\nSystem.out.println(\"Thread interrupted.\");\r\n}\r\nSystem.out.println(\"\\n\" + msg + \"Sent\");\r\n}\r\n}\r\nclass ThreadedSend extends Thread\r\n{\r\nprivate String msg;\r\nprivate Thread t;\r\nSender sender;\r\nThreadedSend(String m, Sender obj)\r\n{\r\nmsg = m;\r\nsender = obj;\r\n}\r\npublic void run()\r\n{\r\nsynchronized(sender)\r\n{\r\n\/\/ synchronizing the snd object\r\nsender.send(msg);\r\n}\r\n}\r\n}\r\nclass SyncDemo\r\n{\r\npublic static void main(String args[])\r\n{\r\nSender snd = new Sender();\r\nThreadedSend S1 =\r\nnew ThreadedSend( \" Hi \" , snd );\r\nThreadedSend S2 =\r\nnew ThreadedSend( \" Bye \" , snd );\r\nS1.start();\r\nS2.start();\r\ntry\r\n{\r\nS1.join();\r\nS2.join();\r\ncatch(Exception e)\r\n{\r\nSystem.out.println(\"Interrupted\");\r\n}\r\n}\r\n}<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/polymorphism-in-java\/\">Do you know about Polymorphism in Java\u00a0<\/a><\/strong><\/p>\n<p>In the example stated above we choose to synchronize the sender object inside the run()\u00a0method of the ThreadedSend class.<\/p>\n<pre class=\"EnlighterJSRAW\">class Sender\r\n{\r\npublic synchronized void send(String msg)\r\n{\r\nSystem.out.println(\"Sending\\t\" + msg );\r\ntry\r\n{\r\nThread.sleep(1000);\r\n}\r\ncatch (Exception e)\r\n{\r\nSystem.out.println(\"Thread interrupted.\");\r\n}\r\nSystem.out.println(\"\\n\" + msg + \"Sent\");\r\n}\r\n}<\/pre>\n<p>Synchronizing the whole a method is not always necessary. Sometimes parts of a method\u00a0synchronized in Java.<\/p>\n<pre class=\"EnlighterJSRAW\">class Sender\r\n{\r\npublic void send(String msg)\r\n{\r\nsynchronized(this)\r\n{\r\nSystem.out.println(\"Sending\\t\" + msg );\r\ntry\r\n{\r\nThread.sleep(1000);\r\n}\r\ncatch (Exception e)\r\n{\r\nSystem.out.println(\"Thread interrupted.\");\r\n}\r\nSystem.out.println(\"\\n\" + msg + \"Sent\");\r\n}\r\n}\r\n}<\/pre>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/hierarchical-data-structure\/\">Follow this link to know about Hierarchical Data Structure in Java<\/a><\/strong><\/p>\n<p>So, this was all about\u00a0synchronization in Java Tutorial. Hope you like our explanation.<\/p>\n<h3>Conclusion<\/h3>\n<p>Hence, we studied synchronization in java programming Langauge. In addition, we saw the Java\u00a0synchronized method and blocks in detail. In our next tutorial, we will see<a href=\"https:\/\/data-flair.training\/blogs\/java-semaphore\/\"><strong> Semaphore in Java<\/strong><\/a>.\u00a0Furthermore, if you have any query, feel free to ask in the comment section. Surely we will get back to you.<\/p>\n<p>Keep Learning and Keep Visiting!<\/p>\n<p style=\"font-weight: 400\">Related Topic-<strong><a href=\"https:\/\/data-flair.training\/blogs\/java-garbage-collection-algorithm\/\">\u00a0Java Garbage Collection Algorithm\u00a0<\/a><\/strong><\/p>\n<p style=\"font-weight: 400\"><strong><a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/syncmeth.html\">For reference\u00a0<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last Java Tutorial, we studied about\u00a0Deadlock in Java.\u00a0Here, we are going to learn What is Synchronization in Java. Moreover, we will discuss\u00a0the Java Synchronized method and\u00a0Java Synchronized block. So, let&#8217;s study Synchronized&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":16024,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[7710,7711,7712,7713,7714,7715,14027,14029],"class_list":["post-16012","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java-synchronized-block","tag-java-synchronized-method","tag-java-synchronized-object","tag-java-synchronized-static-method","tag-java-synchronized-variable","tag-java-synchronized-wait","tag-synchronized-class-in-java","tag-synchronized-keyword-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is Java Synchronized Method | Java Synchronized Block - DataFlair<\/title>\n<meta name=\"description\" content=\"Java Synchronized Tutorial- What is Synchronization in java, Java Synchronized Method, Java Synchronized Blocks, Example of Synchronization in Java\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-synchronized\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Java Synchronized Method | Java Synchronized Block - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Java Synchronized Tutorial- What is Synchronization in java, Java Synchronized Method, Java Synchronized Blocks, Example of Synchronization in Java\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-synchronized\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-18T04:04:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-30T09:44:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is Java Synchronized Method | Java Synchronized Block - DataFlair","description":"Java Synchronized Tutorial- What is Synchronization in java, Java Synchronized Method, Java Synchronized Blocks, Example of Synchronization in Java","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:\/\/data-flair.training\/blogs\/java-synchronized\/","og_locale":"en_US","og_type":"article","og_title":"What is Java Synchronized Method | Java Synchronized Block - DataFlair","og_description":"Java Synchronized Tutorial- What is Synchronization in java, Java Synchronized Method, Java Synchronized Blocks, Example of Synchronization in Java","og_url":"https:\/\/data-flair.training\/blogs\/java-synchronized\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-05-18T04:04:39+00:00","article_modified_time":"2026-05-30T09:44:05+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"What is Java Synchronized Method | Java Synchronized Block","datePublished":"2018-05-18T04:04:39+00:00","dateModified":"2026-05-30T09:44:05+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/"},"wordCount":405,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg","keywords":["Java Synchronized block","Java Synchronized method","java synchronized object","java synchronized static method","java synchronized variable","java synchronized wait","synchronized class in java","Synchronized Keyword in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-synchronized\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/","url":"https:\/\/data-flair.training\/blogs\/java-synchronized\/","name":"What is Java Synchronized Method | Java Synchronized Block - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg","datePublished":"2018-05-18T04:04:39+00:00","dateModified":"2026-05-30T09:44:05+00:00","description":"Java Synchronized Tutorial- What is Synchronization in java, Java Synchronized Method, Java Synchronized Blocks, Example of Synchronization in Java","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-synchronized\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/05\/2018-05-15.jpg","width":1200,"height":628,"caption":"What is Java Synchronized Method | Java Synchronized Block"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-synchronized\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"What is Java Synchronized Method | Java Synchronized Block"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=16012"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16012\/revisions"}],"predecessor-version":[{"id":148503,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/16012\/revisions\/148503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/16024"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=16012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=16012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=16012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}