{"id":1747,"date":"2016-09-23T17:03:37","date_gmt":"2016-09-23T17:03:37","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1747"},"modified":"2016-09-23T17:03:37","modified_gmt":"2016-09-23T17:03:37","slug":"does-servletcontext-objects-are-thread-safe","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/does-servletcontext-objects-are-thread-safe\/","title":{"rendered":"Does ServletContext objects are Thread Safe?"},"content":{"rendered":"<p>Publishing attributes via ServletContext#setAttribute is thread-safe! This can be derived from the Java Servlet Specification, chapter 4.5: (&#8230;) Any attribute bound into a context is available to any other servlet that is part of the same Web application.(&#8230;).<\/p>\n<p>(Reason: Making objects available to other servlets means also to make them available to other threads. This is only possible, if proper synchronization is used, so synchronization is mandatory for ServletContext#setAttribute).<\/p>\n<p>So the same is also true for reading published attributes via ServletContext#getAttribute.<\/p>\n<p><em>Note<\/em><br \/>\nBut if an object like a HashMap is shared between different threads, the developer must ensure that this shared object itself is accessed in a proper, thread-safe way! Using a ConcurrentHashMap is a possible solution, but does not solve the race condition when the attribute is initialized, as the null check will not be atomic.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nConcurrentMap&lt;String, Object&gt; shared = (...)servletContext.getAttribute(&quot;sharedData&quot;);\r\nif (shared == null) {\r\n    shared = new ConcurrentHashMap&lt;&gt;();\r\n    servletContext.setAttribute(&quot;sharedData&quot;, shared);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Publishing attributes via ServletContext#setAttribute is thread-safe! This can be derived from the Java Servlet Specification, chapter 4.5: (&#8230;) Any attribute bound into a context is available to any other servlet that is part of the same Web application.(&#8230;). (Reason: Making objects available to other servlets means also to make them available to other threads. This&hellip; <a href=\"https:\/\/codethataint.com\/blog\/does-servletcontext-objects-are-thread-safe\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[],"class_list":["post-1747","post","type-post","status-publish","format-standard","hentry","category-interview-questions-j2ee"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1747"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1747\/revisions"}],"predecessor-version":[{"id":1748,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1747\/revisions\/1748"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}