{"id":24,"date":"2015-03-14T11:05:30","date_gmt":"2015-03-14T16:05:30","guid":{"rendered":"https:\/\/www.staging.javanator.org\/?p=24"},"modified":"2016-07-04T15:28:00","modified_gmt":"2016-07-04T20:28:00","slug":"thread-confinement","status":"publish","type":"post","link":"https:\/\/www.staging.javanator.org\/?p=24","title":{"rendered":"Thread Confinement"},"content":{"rendered":"<p>True to the mission of this site, my first post reflects something for which I forgot the name and had to look up, so in the Javanator hopper it goes. <em>Thread Confinement<\/em> is a technique for making the use of an object thread-safe by guaranteeing that only one thread ever uses it. This is in contrast to many of the other techniques for engineering thread safety that involve a lot of guarding and synchronizing and general-purpose sweat of the mind.<\/p>\n<p>The simplest way to ensure thread confinement is to write a single-threaded application. Unfortunately, this is harder to do than it sounds. Think you&#8217;ve written a single-threaded app? Don&#8217;t be so sure. Does your app use Swing? Definitely not single-threaded, then. Just because you haven&#8217;t spawned any threads doesn&#8217;t mean one of your frameworks hasn&#8217;t and frameworks, it seems, are everywhere these days, even for the smallest of tasks.<\/p>\n<p>I first saw the term <em>thread confinement<\/em> in the book <a href=\"http:\/\/www.amazon.com\/gp\/product\/0321349601\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321349601&amp;linkCode=as2&amp;tag=javanatororg-20&amp;linkId=NYA2FL2AYLANEYBO\" target=\"_blank\">Java Concurrency in Practice<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"http:\/\/ir-na.amazon-adsystem.com\/e\/ir?t=javanatororg-20&amp;l=as2&amp;o=1&amp;a=0321349601\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/>. That reference contains a fine description of the technique, and includes subsections on implementing thread confinement using stack confinement, <code>ThreadLocal<\/code> variables, and ad-hoc techniques.<\/p>\n<p><script type=\"text\/javascript\" src=\"http:\/\/wms-na.amazon-adsystem.com\/20070822\/US\/js\/link-enhancer-common.js?tag=javanatororg-20&#038;linkId=CPEQK3CSMQ7QVPTZ\">\n<\/script><br \/>\n<noscript><br \/>\n    <img decoding=\"async\" src=\"http:\/\/wms-na.amazon-adsystem.com\/20070822\/US\/img\/noscript.gif?tag=javanatororg-20&#038;linkId=CPEQK3CSMQ7QVPTZ\" alt=\"\" \/><br \/>\n<\/noscript><\/p>\n","protected":false},"excerpt":{"rendered":"<p>True to the mission of this site, my first post reflects something for which I forgot the name and had to look up, so in the Javanator hopper it goes. Thread Confinement is a technique for making the use of an object thread-safe by guaranteeing that only one thread ever uses it. This is in &hellip; <a href=\"https:\/\/www.staging.javanator.org\/?p=24\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Thread Confinement&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":7,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":58,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/58"}],"wp:attachment":[{"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.staging.javanator.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}