<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java and Me &#187; synchronized</title>
	<atom:link href="http://rpktech.com/tag/synchronized/feed/" rel="self" type="application/rss+xml" />
	<link>http://rpktech.com</link>
	<description>An Interesting Journey</description>
	<lastBuildDate>Sun, 22 Nov 2015 11:01:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Java Memory Model and Synchronization</title>
		<link>http://rpktech.com/2015/02/03/java-memory-model-and-synchronization/</link>
		<comments>http://rpktech.com/2015/02/03/java-memory-model-and-synchronization/#comments</comments>
		<pubDate>Tue, 03 Feb 2015 14:11:53 +0000</pubDate>
		<dc:creator><![CDATA[Rahul Kulshreshtha]]></dc:creator>
				<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[Java Memory Model]]></category>
		<category><![CDATA[memory barrier]]></category>
		<category><![CDATA[synchronized]]></category>

		<guid isPermaLink="false">http://rpktech.com/?p=36</guid>
		<description><![CDATA[Java Memory Model The Java memory model specifies how and when different threads can see values written to shared variables by other threads, and how to synchronize access to shared variables when necessary. Java memory model which was revised in Java]]></description>
				<content:encoded><![CDATA[<p><span style="text-decoration: underline;"><strong>Java Memory Model</strong></span></p>
<p>The Java memory model specifies how and when different threads can see values written to shared variables by other threads, and how to synchronize access to shared variables when necessary. Java memory model which was revised in Java 1.5 is still in use in Java 8. Jenkov has explained very well in his <a href="http://tutorials.jenkov.com/java-concurrency/java-memory-model.html" target="_blank">article about JMM</a>. Here is the summary from that article :</p>
<ol>
<li><strong>Primitive</strong> variables inside thread are not shareable, they are kept in thread-stack which is <strong>non-shareable</strong> between threads.</li>
<li><strong>Objects</strong> are created inside heap and heap area is <strong>shareable</strong> between threads.</li>
<li><strong>Object&#8217;s member variabl</strong>e are also stored in heap and hence are <strong>shareable</strong> even though they are primitive. But all the variable declared inside object&#8217;s method are local to that method and they will be store in thread-stack.</li>
<li>Static variables are also stored into heap along with the class definition.</li>
</ol>
<p>Heap is shareable so synchronization is required.  Lets first understand the synchronization and how lock works.</p>
<p><span style="text-decoration: underline;"><strong>Synchronization</strong></span> : In an <a href="http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html" target="_blank">article of &#8220;Brian Goetz&#8221;</a>, synchronization is explained like this</p>
<blockquote><p>Java&#8217;s primary tool for rendering interactions between threads predictably is the <code>synchronized</code> keyword. Many programmers think of <code>synchronized</code> strictly in terms of enforcing a mutual exclusion semaphore (<em>mutex</em>) to prevent execution of critical sections by more than one thread at a time. Unfortunately, that intuition does not fully describe what <code>synchronized</code> means.</p>
<p>The semantics of <code>synchronized</code> do indeed include mutual exclusion of execution based on the status of a semaphore, but they also include rules about the synchronizing thread&#8217;s interaction with main memory. In particular, the acquisition or release of a lock triggers a <strong><em>memory barrier</em></strong> &#8212; a forced synchronization between the thread&#8217;s local memory and main memory. When a thread exits a <code>synchronized</code> block, it performs a write barrier &#8212; it must flush out any variables modified in that block to main memory before releasing the lock. Similarly, when entering a <code>synchronized</code> block, it performs a read barrier &#8212; it is as if the local memory has been invalidated, and it must fetch any variables that will be referenced in the block from main memory.</p>
<p>The proper use of synchronization guarantees that one thread will see the effects of another in a predictable manner.</p></blockquote>
<p>Below examples will show the visibility of variables in Java Memory Model which was explained by Jenkov in his <a href="http://tutorials.jenkov.com/java-concurrency/java-memory-model.html" target="_blank">article</a>.</p>
<p><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Frpktech.com%2F2015%2F02%2F03%2Fjava-memory-model-and-synchronization%2F&amp;linkname=Java%20Memory%20Model%20and%20Synchronization" title="Facebook" rel="nofollow" target="_blank"></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Frpktech.com%2F2015%2F02%2F03%2Fjava-memory-model-and-synchronization%2F&amp;linkname=Java%20Memory%20Model%20and%20Synchronization" title="Twitter" rel="nofollow" target="_blank"></a><a class="a2a_button_google_plus" href="http://www.addtoany.com/add_to/google_plus?linkurl=http%3A%2F%2Frpktech.com%2F2015%2F02%2F03%2Fjava-memory-model-and-synchronization%2F&amp;linkname=Java%20Memory%20Model%20and%20Synchronization" title="Google+" rel="nofollow" target="_blank"></a><a class="a2a_dd a2a_target addtoany_share_save" href="https://www.addtoany.com/share#url=http%3A%2F%2Frpktech.com%2F2015%2F02%2F03%2Fjava-memory-model-and-synchronization%2F&amp;title=Java%20Memory%20Model%20and%20Synchronization" id="wpa2a_2"></a></p>]]></content:encoded>
			<wfw:commentRss>http://rpktech.com/2015/02/03/java-memory-model-and-synchronization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
