{"id":63287,"date":"2017-01-18T19:00:47","date_gmt":"2017-01-18T17:00:47","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=63287"},"modified":"2017-01-18T11:54:15","modified_gmt":"2017-01-18T09:54:15","slug":"synchronized-decorators-replace-thread-safe-classes","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html","title":{"rendered":"Synchronized Decorators to Replace Thread-Safe Classes"},"content":{"rendered":"<p>You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to be thread-safe? Wrong again. I think none of them have to be thread-safe, while all of them have to provide synchronized decorators.<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n<figure id=\"attachment_63305\" aria-describedby=\"caption-attachment-63305\" style=\"width: 620px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin.jpg\"><img decoding=\"async\" class=\"wp-image-63305 size-large\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin-1024x576.jpg\" width=\"620\" height=\"349\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin-1024x576.jpg 1024w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin-300x169.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin-768x432.jpg 768w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2017\/01\/aladdin.jpg 1920w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><figcaption id=\"caption-attachment-63305\" class=\"wp-caption-text\">Aladdin (1992) by Ron Clements and John Musker<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s start with an example (it&#8217;s <a href=\"\/2014\/06\/09\/objects-should-be-immutable.html\">mutable<\/a>, by the way):<\/p>\n<pre class=\"brush:java\">class Position {\r\n  private int number = 0;\r\n  @Override\r\n  public void increment() {\r\n    int before = this.number;\r\n    int after = before + 1;\r\n    this.number = after;\r\n  }\r\n}<\/pre>\n<\/figure>\n<p>What do you think \u2014 is it <a href=\"https:\/\/en.wikipedia.org\/wiki\/Thread_safety\">thread-safe<\/a>? This term refers to whether an object of this class will operate without mistakes when used by multiple threads at the same time. Let&#8217;s say we have two threads working with the same object, <code>position<\/code>, and calling its method <code>increment()<\/code> at exactly the same moment in time.<\/p>\n<p>We expect the <code>number<\/code> integer to be equal to 2 when both threads finish up, because each of them will increment it once, right? However, most likely this won&#8217;t happen.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Let&#8217;s see what will happen. In both threads, <code>before<\/code> will equal <code>0<\/code> when they start. Then <code>after<\/code> will be set to <code>1<\/code>. Then, both threads will do <code>this.number = 1<\/code> and we will end up with <code>1<\/code> in <code>number<\/code> instead of the expected <code>2<\/code>. See the problem? Classes with such a flaw in their design are <strong>not thread-safe<\/strong>.<\/p>\n<p>The simplest and most obvious solution is to make our method <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/essential\/concurrency\/syncmeth.html\"><code>synchronized<\/code><\/a>. That will guarantee that no matter how many threads call it at the same time, they will all go sequentially, not in parallel: one thread after another. Of course, it will take longer, but it will prevent that mistake from happening:<\/p>\n<pre class=\"brush:java\">class Position {\r\n  private int number = 0;\r\n  @Override\r\n  public synchronized void increment() {\r\n    int before = this.number;\r\n    int after = before + 1;\r\n    this.number = after;\r\n  }\r\n}<\/pre>\n<p>A class that guarantees it won&#8217;t break no matter how many threads are working with it is called <strong>thread-safe<\/strong>.<\/p>\n<p>Now the question is: Do we have to make all classes thread-safe or only some of them? It would seem to be better to have all classes error-free, right? Why would anyone want an object that may break at some point? Well, not exactly. Remember, there is a performance concern involved; we don&#8217;t often have multiple threads, and we always want our objects to run as fast as possible. A between-threads synchronization mechanism will definitely slow us down.<\/p>\n<p>I think the right approach is to have two classes. The first one is not thread-safe, while the other one is a <strong>synchronized decorator<\/strong>, which would look like this:<\/p>\n<pre class=\"brush:java\">class SyncPosition implements Position {\r\n  private final Position origin;\r\n  SyncPosition(Position pos) {\r\n    this.origin = pos;\r\n  }\r\n  @Override\r\n  public synchronized void increment() {\r\n    this.origin.increment();\r\n  }\r\n}<\/pre>\n<p>Now, when we need our <code>position<\/code> object to be thread-safe, we decorate it with <code>SyncPosition<\/code>:<\/p>\n<pre class=\"brush:java\">Position position = new SyncPosition(\r\n  new SimplePosition()\r\n);<\/pre>\n<p>When we need a plain simple position, without any thread safety, we do this:<\/p>\n<pre class=\"brush:java\">Position position = new SimplePosition();<\/pre>\n<\/figure>\n<p>Making class functionality both rich <em>and<\/em> thread-safe is, in my opinion, a violation of that famous <a href=\"https:\/\/en.wikipedia.org\/wiki\/Single_responsibility_principle\">single responsibility principle<\/a>.<\/p>\n<p>By the way, this problem is very close to the one of <a href=\"\/2016\/01\/26\/defensive-programming.html\">defensive programming<\/a> and validators.<\/p>\n<p>You may also find these <strong>related<\/strong> posts interesting: <a href=\"\/2015\/10\/20\/interrupted-exception.html\">What Do You Do With InterruptedException?<\/a>; <a href=\"\/2016\/04\/26\/why-inputstream-design-is-wrong.html\">Why InputStream Design Is Wrong<\/a>; <a href=\"\/2014\/06\/20\/limit-method-execution-time.html\">Limit Java Method Execution Time<\/a>; <a href=\"\/2015\/04\/30\/iterating-adapter.html\">How to Implement an Iterating Adapter<\/a>; <a href=\"\/2016\/01\/26\/defensive-programming.html\">Defensive Programming via Validating Decorators<\/a>;<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"http:\/\/www.yegor256.com\/2017\/01\/17\/synchronized-decorators.html\">Synchronized Decorators to Replace Thread-Safe Classes<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/join-us\/jcg\/\">JCG partner<\/a> Yegor Bugayenko at the <a href=\"http:\/\/www.yegor256.com\/\">About Programming<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to be thread-safe? Wrong again. I think none of them have to be thread-safe, while all of them have to provide synchronized decorators. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &hellip;<\/p>\n","protected":false},"author":593,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[1425],"class_list":["post-63287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-concurreny"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/yegor256\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-18T17:00:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yegor Bugayenko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/yegor256\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yegor Bugayenko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html\"},\"author\":{\"name\":\"Yegor Bugayenko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\"},\"headline\":\"Synchronized Decorators to Replace Thread-Safe Classes\",\"datePublished\":\"2017-01-18T17:00:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html\"},\"wordCount\":515,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Concurreny\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html\",\"name\":\"Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2017-01-18T17:00:47+00:00\",\"description\":\"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2017\\\/01\\\/synchronized-decorators-replace-thread-safe-classes.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Synchronized Decorators to Replace Thread-Safe Classes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/f3ff9c97ecd948f271ebc5ead401d02d\",\"name\":\"Yegor Bugayenko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g\",\"caption\":\"Yegor Bugayenko\"},\"description\":\"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.\",\"sameAs\":[\"http:\\\/\\\/www.yegor256.com\\\/\",\"https:\\\/\\\/www.facebook.com\\\/yegor256\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/yegor256\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/yegor256\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yegor-bugayenko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks","description":"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to","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:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html","og_locale":"en_US","og_type":"article","og_title":"Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks","og_description":"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to","og_url":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/yegor256","article_published_time":"2017-01-18T17:00:47+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Yegor Bugayenko","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/yegor256","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yegor Bugayenko","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html"},"author":{"name":"Yegor Bugayenko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d"},"headline":"Synchronized Decorators to Replace Thread-Safe Classes","datePublished":"2017-01-18T17:00:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html"},"wordCount":515,"commentCount":3,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Concurreny"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html","url":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html","name":"Synchronized Decorators to Replace Thread-Safe Classes - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2017-01-18T17:00:47+00:00","description":"You know what thread safety is, right? If not, there is a simple example below. All classes must be thread-safe, right? Not really. Some of them have to","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2017\/01\/synchronized-decorators-replace-thread-safe-classes.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Synchronized Decorators to Replace Thread-Safe Classes"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/f3ff9c97ecd948f271ebc5ead401d02d","name":"Yegor Bugayenko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c3696c78da79ebdd9ffa8e87e8832461b7cd59659483373b34da4ae25dfb573a?s=96&d=mm&r=g","caption":"Yegor Bugayenko"},"description":"Yegor Bugayenko is an Oracle certified Java architect, CEO of Zerocracy, author of Elegant Objects book series about object-oriented programing, lead architect and founder of Cactoos, Takes, Rultor and Jcabi, and a big fan of test automation.","sameAs":["http:\/\/www.yegor256.com\/","https:\/\/www.facebook.com\/yegor256","https:\/\/www.linkedin.com\/in\/yegor256","https:\/\/x.com\/https:\/\/twitter.com\/yegor256"],"url":"https:\/\/www.javacodegeeks.com\/author\/yegor-bugayenko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/63287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/593"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=63287"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/63287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=63287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=63287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=63287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}