<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Sanjay&#039;s Blog</title>
	<atom:link href="http://sanjaysharma101.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sanjaysharma101.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 07 Aug 2009 04:19:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sanjaysharma101.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Sanjay&#039;s Blog</title>
		<link>http://sanjaysharma101.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sanjaysharma101.wordpress.com/osd.xml" title="Sanjay&#039;s Blog" />
	<atom:link rel='hub' href='http://sanjaysharma101.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Blocking Primitives in Java</title>
		<link>http://sanjaysharma101.wordpress.com/2009/06/30/blocking-primitives-in-java/</link>
		<comments>http://sanjaysharma101.wordpress.com/2009/06/30/blocking-primitives-in-java/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 04:57:48 +0000</pubDate>
		<dc:creator>sanjay101</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AbstractQueueSynchronizer]]></category>
		<category><![CDATA[AQS]]></category>
		<category><![CDATA[blocking]]></category>
		<category><![CDATA[Lock]]></category>
		<category><![CDATA[locks]]></category>
		<category><![CDATA[primitives]]></category>
		<category><![CDATA[Support]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://sanjaysharma101.wordpress.com/?p=10</guid>
		<description><![CDATA[Since Java 5, we have thread level blocking primitives for creating locks and other synchronization classes.  It is easy to build locks using spinlocks that would scale well especially if no of CPUs can match no of threads spinning. It could also waste memory cycles if contending threads outnumbers CPUs available. A prudent strategy is to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjaysharma101.wordpress.com&amp;blog=8041171&amp;post=10&amp;subd=sanjaysharma101&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since Java 5, we have thread level blocking primitives for creating locks and other synchronization classes.  It is easy to build locks using spinlocks that would scale well especially if no of CPUs can match no of threads spinning. It could also waste memory cycles if contending threads outnumbers CPUs available. A prudent strategy is to do some spinning followed by blocking/ unblocking and some more spinning &#8211; all in a loop till the system is in a desirable state.  No of spin cycles can be customized. Blocking mechanics could become a little complex.  A lot of code gets executed from the time its decided that a thread should block to the actual blocking, providing opportunities to slip in some more spinning.  Blocking primitives LockSupport.park() &amp; LockSupport.unpark(Thread th)  ( and their variants ) are in package <strong>java.util.concurrent.locks.</strong>  In general, a thread Thread-  like to block ( LockSupport.park() ) if the system is not in a desired state. It will be blocked till another thread Thread-B change that state in such a way that Thread-A can make progress. Here Thread-B is expected to wakeup Thread-A( LockSupport.unpark(Thread-A).  For Thread-B to wakeup Thread-A, Thread-A needs to save its reference to some data structure that Thread-B can access.  Here is what JavaDoc says about LockSupport.</p>
<p>&#8220;<em>Methods <tt>park</tt> and <tt>unpark</tt> provide efficient means of blocking and unblocking threads that do not encounter the problems that cause the deprecated methods <tt>Thread.suspend</tt> and <tt>Thread.resume</tt> to be unusable for such purposes: Races between one thread invoking <tt>park</tt> and another thread trying to <tt>unpark</tt>it will preserve liveness, due to the permit&#8221;.</em></p>
<p>To understand the internal mechanics of LockSupport, here is one such implementation uning intrinsic lock.  Pl. let me know if you find any bug with this code.</p>
<p>Practically speaking, Class <strong>java.util.concurrent.locks.AbstractQueuedSynchronizer </strong>provides everything needed to write custom synchronizers using a mix of spinning/ blocking primitives.</p>
<p>package sanjay.concurrent.example;</p>
<p>import java.util.concurrent.ConcurrentHashMap;<br />
 <br />
public class MyLockSupport {</p>
<p> final static ConcurrentHashMap&lt;Long, Boolean&gt; map = new ConcurrentHashMap&lt;Long, Boolean&gt;();<br />
 final static Boolean SIGNAL = new Boolean(true);<br />
 <br />
 public static void park() {<br />
  <br />
  Thread thread = Thread.currentThread();<br />
  Long threadId = thread.getId();<br />
  <br />
  synchronized ( thread ) {<br />
   while ( !isSignalled(threadId) ) {<br />
    try {<br />
     thread.wait();<br />
    } catch ( InterruptedException ie) {<br />
     thread.interrupt();<br />
     return;<br />
    }<br />
   }<br />
   consumeSignal(threadId);<br />
   return;<br />
  }<br />
 }</p>
<p> public static void unpark(Thread thread) {<br />
  synchronized ( thread ) {<br />
   signal(thread.getId());<br />
   thread.notify();   <br />
  }<br />
 }</p>
<p> private static boolean isSignalled(Long threadId) {  <br />
  if ( map.get(threadId) == null ) {<br />
   return false;<br />
  }<br />
  return true;<br />
 }</p>
<p> private static void consumeSignal(Long threadId) {<br />
  map.remove(threadId);<br />
 }</p>
<p> private static void signal(Long threadId) {<br />
  map.put(threadId, SIGNAL);<br />
 }</p>
<p>}</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sanjaysharma101.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sanjaysharma101.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sanjaysharma101.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sanjaysharma101.wordpress.com&amp;blog=8041171&amp;post=10&amp;subd=sanjaysharma101&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sanjaysharma101.wordpress.com/2009/06/30/blocking-primitives-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9c70d71ca720f15975306d5d720abb1e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">sanjay101</media:title>
		</media:content>
	</item>
	</channel>
</rss>
