{"id":1769,"date":"2016-09-24T05:11:40","date_gmt":"2016-09-24T05:11:40","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1769"},"modified":"2016-09-24T05:11:40","modified_gmt":"2016-09-24T05:11:40","slug":"what-does-atomic-mean-in-programming","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/what-does-atomic-mean-in-programming\/","title":{"rendered":"What does \u201catomic\u201d mean in programming?"},"content":{"rendered":"<p>Suppose foo is a variable of type long. The following operation is not an atomic operation:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfoo = 65465498L;\r\n<\/pre>\n<p>Indeed, the variable is written using two separate operations: one that writes the first 32 bits, and a second one which writes the last 32 bits. That means that another thread might read the value of foo, and see the intermediate state.<\/p>\n<p>Making the operation atomic consists in using synchronization mechanisms in order to make sure that the operation is seen, from any other thread, as a single, atomic (i.e. not splittable in parts), operation. That means that any other thread, once the operation is made atomic, will either see the value of foo before the assignment, or after the assignment. But never the intermediate value.<\/p>\n<p>A simple way of doing this is to make the variable volatile:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate volatile long foo;\r\n<\/pre>\n<p>Or to synchronize every access to the variable:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic synchronized void setFoo(long value) {\r\n    this.foo = value;\r\n}\r\n\r\npublic synchronized long getFoo() {\r\n    return this.foo;\r\n}\r\n\/\/ no other use of foo outside of these two methods, unless also synchronized\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Suppose foo is a variable of type long. The following operation is not an atomic operation: foo = 65465498L; Indeed, the variable is written using two separate operations: one that writes the first 32 bits, and a second one which writes the last 32 bits. That means that another thread might read the value of&hellip; <a href=\"https:\/\/codethataint.com\/blog\/what-does-atomic-mean-in-programming\/\">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":[193],"tags":[],"class_list":["post-1769","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1769","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=1769"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1769\/revisions"}],"predecessor-version":[{"id":1770,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1769\/revisions\/1770"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}