{"id":1158,"date":"2016-05-05T22:26:36","date_gmt":"2016-05-06T05:26:36","guid":{"rendered":"https:\/\/dubinko.info\/blog\/?p=1158"},"modified":"2016-05-11T22:56:11","modified_gmt":"2016-05-12T05:56:11","slug":"unsafe-java","status":"publish","type":"post","link":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/","title":{"rendered":"Unsafe Java"},"content":{"rendered":"<p>Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though.<\/p>\n<p><code>Entity e = new Entity();<br \/>\ne.setName(\"my new entity\");<br \/>\npersistanceLayer.put(e);<br \/>\n<\/code><\/p>\n<p>To provide some\u00c2\u00a0context, <code>Entity<\/code> is a <abbr title=\"plain old java object\">POJO<\/abbr> representing something we want to store in a database. And <code>persistanceLayer<\/code> is an instance of a Data Access Object responsible for storing the object in the database. Don\u00e2\u20ac\u2122t see it yet?<\/p>\n<p>Here\u00e2\u20ac\u2122s the function signature of that <code>put<\/code> method<\/p>\n<p><code>@Async<br \/>\n&lt;E extends AbstractEntity&gt; CompletableFuture&lt;E&gt; put(E newEntity);<br \/>\n<\/code><\/p>\n<p>Ah, yes, Spring, which because of the <a href=\"http:\/\/docs.spring.io\/spring\/docs\/current\/spring-framework-reference\/html\/scheduling.html#scheduling-annotation-support-async\"><code>@Async<\/code><\/a> annotation will proxy this method and cause it to execute in a different thread. The object getting passed in is clearly mutable, and by my reading of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_memory_model\">Java Memory Model<\/a>, there\u00e2\u20ac\u2122s no guarantee that the other thread\u00e2\u20ac\u2122s view of the update that happened in the <code>setName<\/code> method will be visible. In other words, there\u00e2\u20ac\u2122s no guarantee the two threads idea of \u00e2\u20ac\u0153before-ness\u00e2\u20ac\u009d will be the same; who knows what might have been reordered or cached?<\/p>\n<p>I\u00e2\u20ac\u2122m having trouble coming to terms with what seems like a gaping hole. Do you agree with this analysis? If so, how do you deal with it? Immutable object classes seem like an obvious choice, but will introduce lots of overhead in other parts of the code that need to manipulate the entities. A builder pattern gets awkward and verbose quickly, as it hoists all that complexity on to the caller.<\/p>\n<p>How do you handle situations like this? I\u00e2\u20ac\u2122d love to hear it.<\/p>\n<p>If you produce libraries, please include documentation on what is and isn\u00e2\u20ac\u2122t guaranteed w.r.t. async calls. -m<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&#8220;my new entity&#8221;); persistanceLayer.put(e); To provide some\u00c2\u00a0context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[27,22],"tags":[],"class_list":["post-1158","post","type-post","status-publish","format-standard","hentry","category-languages","category-software"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&quot;my new entity&quot;); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"mdubinko\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"MicahLogic Queryopticon | Yields undecidable when preceded by its quotation\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Unsafe Java | MicahLogic Queryopticon\" \/>\n\t\t<meta property=\"og:description\" content=\"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&quot;my new entity&quot;); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-05-06T05:26:36+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-05-12T05:56:11+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Unsafe Java | MicahLogic Queryopticon\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&quot;my new entity&quot;); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#article\",\"name\":\"Unsafe Java | MicahLogic Queryopticon\",\"headline\":\"Unsafe Java\",\"author\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/#organization\"},\"datePublished\":\"2016-05-05T22:26:36-07:00\",\"dateModified\":\"2016-05-11T22:56:11-07:00\",\"inLanguage\":\"en-US\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#webpage\"},\"articleSection\":\"languages, software\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/dubinko.info\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/tags\\\/software\\\/#listItem\",\"name\":\"software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/tags\\\/software\\\/#listItem\",\"position\":2,\"name\":\"software\",\"item\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/tags\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#listItem\",\"name\":\"Unsafe Java\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#listItem\",\"position\":3,\"name\":\"Unsafe Java\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/tags\\\/software\\\/#listItem\",\"name\":\"software\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/#organization\",\"name\":\"MicahLogic Queryopticon\",\"description\":\"Yields undecidable when preceded by its quotation\",\"url\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"mdubinko\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c1160c0ab9ce581a55013f96fd61356e166ffc9a00666a5062bc8121922fcdcd?s=96&d=retro&r=g\",\"width\":96,\"height\":96,\"caption\":\"mdubinko\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#webpage\",\"url\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/\",\"name\":\"Unsafe Java | MicahLogic Queryopticon\",\"description\":\"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(\\\"my new entity\\\"); persistanceLayer.put(e); To provide some\\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/2016\\\/05\\\/unsafe-java\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2016-05-05T22:26:36-07:00\",\"dateModified\":\"2016-05-11T22:56:11-07:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/\",\"name\":\"MicahLogic Queryopticon\",\"description\":\"Yields undecidable when preceded by its quotation\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/dubinko.info\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Unsafe Java | MicahLogic Queryopticon","description":"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(\"my new entity\"); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a","canonical_url":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#article","name":"Unsafe Java | MicahLogic Queryopticon","headline":"Unsafe Java","author":{"@id":"https:\/\/dubinko.info\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/dubinko.info\/blog\/#organization"},"datePublished":"2016-05-05T22:26:36-07:00","dateModified":"2016-05-11T22:56:11-07:00","inLanguage":"en-US","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#webpage"},"isPartOf":{"@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#webpage"},"articleSection":"languages, software"},{"@type":"BreadcrumbList","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog#listItem","position":1,"name":"Home","item":"https:\/\/dubinko.info\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog\/tags\/software\/#listItem","name":"software"}},{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog\/tags\/software\/#listItem","position":2,"name":"software","item":"https:\/\/dubinko.info\/blog\/tags\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#listItem","name":"Unsafe Java"},"previousItem":{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#listItem","position":3,"name":"Unsafe Java","previousItem":{"@type":"ListItem","@id":"https:\/\/dubinko.info\/blog\/tags\/software\/#listItem","name":"software"}}]},{"@type":"Organization","@id":"https:\/\/dubinko.info\/blog\/#organization","name":"MicahLogic Queryopticon","description":"Yields undecidable when preceded by its quotation","url":"https:\/\/dubinko.info\/blog\/"},{"@type":"Person","@id":"https:\/\/dubinko.info\/blog\/author\/admin\/#author","url":"https:\/\/dubinko.info\/blog\/author\/admin\/","name":"mdubinko","image":{"@type":"ImageObject","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/c1160c0ab9ce581a55013f96fd61356e166ffc9a00666a5062bc8121922fcdcd?s=96&d=retro&r=g","width":96,"height":96,"caption":"mdubinko"}},{"@type":"WebPage","@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#webpage","url":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/","name":"Unsafe Java | MicahLogic Queryopticon","description":"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(\"my new entity\"); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/dubinko.info\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/#breadcrumblist"},"author":{"@id":"https:\/\/dubinko.info\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/dubinko.info\/blog\/author\/admin\/#author"},"datePublished":"2016-05-05T22:26:36-07:00","dateModified":"2016-05-11T22:56:11-07:00"},{"@type":"WebSite","@id":"https:\/\/dubinko.info\/blog\/#website","url":"https:\/\/dubinko.info\/blog\/","name":"MicahLogic Queryopticon","description":"Yields undecidable when preceded by its quotation","inLanguage":"en-US","publisher":{"@id":"https:\/\/dubinko.info\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"MicahLogic Queryopticon | Yields undecidable when preceded by its quotation","og:type":"article","og:title":"Unsafe Java | MicahLogic Queryopticon","og:description":"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&quot;my new entity&quot;); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a","og:url":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/","article:published_time":"2016-05-06T05:26:36+00:00","article:modified_time":"2016-05-12T05:56:11+00:00","twitter:card":"summary","twitter:title":"Unsafe Java | MicahLogic Queryopticon","twitter:description":"Pop quiz. Why is the following Java 8 code unsafe? UPDATE: this code is fine, see comments. Still good to think about, though. Entity e = new Entity(); e.setName(&quot;my new entity&quot;); persistanceLayer.put(e); To provide some\u00c2 context, Entity is a POJO representing something we want to store in a database. And persistanceLayer is an instance of a"},"aioseo_meta_data":{"post_id":"1158","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-02-04 08:04:16","updated":"2025-12-10 03:46:27","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/dubinko.info\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/dubinko.info\/blog\/tags\/software\/\" title=\"software\">software<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUnsafe Java\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/dubinko.info\/blog"},{"label":"software","link":"https:\/\/dubinko.info\/blog\/tags\/software\/"},{"label":"Unsafe Java","link":"https:\/\/dubinko.info\/blog\/2016\/05\/unsafe-java\/"}],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8eo8l-iG","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":3,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"predecessor-version":[{"id":1176,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/posts\/1158\/revisions\/1176"}],"wp:attachment":[{"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dubinko.info\/blog\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}