{"id":70,"date":"2005-11-24T19:40:16","date_gmt":"2005-11-24T19:40:16","guid":{"rendered":"https:\/\/kari.world.ikari.fi\/2005\/11\/24\/to-synchronize-or-to-not\/"},"modified":"2005-11-24T19:40:16","modified_gmt":"2005-11-24T19:40:16","slug":"to-synchronize-or-to-not","status":"publish","type":"post","link":"https:\/\/kari.world.ikari.fi\/?p=70","title":{"rendered":"To synchronize, or to not"},"content":{"rendered":"<p>Reading Java VM specification gives interesting insights of the synchronized keyword usage.<\/p>\n<p><a href=\"http:\/\/java.sun.com\/docs\/books\/vmspec\/2nd-edition\/html\/Threads.doc.html#2321294\">VM Specification: Threads and Locks<\/a><\/p>\n<p>For good fun, read chapter 8.11. I.e.<\/p>\n<blockquote><p>\n&#8230;  Because there is no synchronization, it is at the option of the implementation whether or not to store the assigned values back to main memory! &#8230;\n<\/p><\/blockquote>\n<p>This means that if synchronization is <b>not<\/b> used properly. Then code is <b>broken<\/b>. Which tends to mean that it works in developer&#8217;s own limited &#8221;test cases&#8221;, while failing in real life occasionally with mysterious results.<\/p>\n<p>When dealing with synchronization, it must be noted that these <i>&#8221;main memory&#8221;<\/i> and <i>&#8221;working memory&#8221;<\/i> concepts for threads are very likely  to become issue when SMP systems are used. While on the other hand, it is very likely that developer&#8217;s are themselves testing only (a) without server JVM, (b) under debugger, (c) using single CPU machine, (d) using only single OS.<\/p>\n<p>Luckily chapter 8.9 states:<\/p>\n<blockquote><p>\n.. Locking any lock conceptually flushes <b>all<\/b> variables from a thread&#8217;s working memory, and unlocking any lock forces the writing out to main memory of <b>all<\/b> variables that the thread has assigned. &#8230;\n<\/p><\/blockquote>\n<p>This means that synchronized flushes not only changes occuring during synchronized block, but also <b>pending<\/b> changes which potentially exist in thread&#8217;s local working memory. However, beware the beholder, this means also that using synchronized is expensive since it requires accessing shared memory (i.e. reading\/writing data to\/from main memory).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading Java VM specification gives interesting insights of the synchronized keyword usage. VM Specification: Threads and Locks For good fun, read chapter 8.11. I.e. &#8230; Because there is no synchronization, it is at the option of the implementation whether or not to store the assigned values back to main memory! &#8230; This means that if&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":0,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}