<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7784832589817032133</id><updated>2024-09-16T16:48:34.204-07:00</updated><category term="Java"/><category term="performance"/><category term="concurrency"/><category term="thread-safe"/><category term="Atomicity"/><category term="jdk7"/><category term="contention"/><category term="fork and join"/><category term="lock"/><category term="multi-threaded"/><category term="notify"/><category term="CAS"/><category term="CMS collector"/><category term="CountDownLatch"/><category term="CyclicBarrier"/><category term="Netbeans"/><category term="Parallel collector"/><category term="Phaser"/><category term="String"/><category term="StringBuilder"/><category term="cache"/><category term="folder"/><category term="garbage collection"/><category term="gc"/><category term="monitoring"/><category term="mount"/><category term="notifyall"/><category term="opensolaris"/><category term="share"/><category term="visualize"/><category term="wait"/><category term="windows xp"/><title type='text'>Curiosity matters : Learning new things is always fun!</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-6371391009662820615</id><published>2011-05-17T07:13:00.000-07:00</published><updated>2011-05-17T07:13:40.941-07:00</updated><title type='text'>An Introduction to Parallel Computing: Videos from Oracle</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;I found it useful, at least the first 2-3 videos where the guy explains the internals of multi-core processors. Somehow it made me remember the Computer System Architecture lectures during my bachelor studies ;).&lt;br /&gt;
&lt;br /&gt;
Here is the link.&lt;br /&gt;
&lt;a href=&quot;http://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/programming-jsp-139962.html&quot;&gt;http://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/programming-jsp-139962.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
More related docs here...&amp;nbsp;&lt;a href=&quot;http://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/index.html&quot;&gt;http://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/index.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Have fun ;)&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/6371391009662820615/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/05/introduction-to-parallel-computing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6371391009662820615'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6371391009662820615'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/05/introduction-to-parallel-computing.html' title='An Introduction to Parallel Computing: Videos from Oracle'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-323298450866494802</id><published>2011-04-28T08:44:00.000-07:00</published><updated>2011-04-28T09:14:01.752-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="contention"/><category scheme="http://www.blogger.com/atom/ns#" term="CountDownLatch"/><category scheme="http://www.blogger.com/atom/ns#" term="CyclicBarrier"/><category scheme="http://www.blogger.com/atom/ns#" term="fork and join"/><category scheme="http://www.blogger.com/atom/ns#" term="jdk7"/><category scheme="http://www.blogger.com/atom/ns#" term="lock"/><category scheme="http://www.blogger.com/atom/ns#" term="multi-threaded"/><category scheme="http://www.blogger.com/atom/ns#" term="notify"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="Phaser"/><category scheme="http://www.blogger.com/atom/ns#" term="thread-safe"/><title type='text'>A peek into JDK7 - Java Phaser: Taking concurrency to the next level</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Some days ago there was a discussion with one of my colleague regarding synchronization points between several Java threads. Of course there are CountDownLatch and CyclicBarrier for solving this kind of problems. But even with them we need to know how may threads are we going to start before hand. Because both &amp;nbsp;&lt;a href=&quot;http://download.java.net/jdk7/docs/api/java/util/concurrent/CountDownLatch.html&quot;&gt;CountDownLatch &lt;/a&gt;and &lt;a href=&quot;http://download.java.net/jdk7/docs/api/java/util/concurrent/CyclicBarrier.html&quot;&gt;CyclicBarrier &lt;/a&gt;expects the number of parties to be synchronized as the constructor argument.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;new CountDownLatch(count);
new CyclicBarrier(parties);
&lt;/pre&gt;&lt;br /&gt;
&lt;div&gt;Then I came across &lt;a href=&quot;http://download.java.net/jdk7/docs/api/java/util/concurrent/Phaser.html&quot;&gt;Phaser&lt;/a&gt;.&amp;nbsp;In case of&amp;nbsp;Phaser you don&#39;t need to give the number of parties as an constructor argument.&amp;nbsp;&lt;/div&gt;&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;new Phaser();
&lt;/pre&gt;&lt;br /&gt;
&lt;div&gt;It is one of the concurrency features coming in Java 7. It is designed in such a way that a thread that needs to be in sync with other threads can register() themselves. Like a CyclicBarrier a Phaser can be reused (during several phases of the task). For this use arriveAndAwaitAdvance(), i.e after completing each phase you arrive and wait for all the other threads and once they all reach you advance (move) to the next phase.&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;In &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/PhaserTest.java&quot;&gt;this example code&lt;/a&gt; you see 3 threads being started. Once a task begins the thread will register itself with the Phaser. After completing each phase of the task each thread will tell the Phaser that it has completed a certain phase by calling &amp;nbsp;arrive() and then wait for the others with awaitAdvance().&amp;nbsp;And at any phase one party can ask the Phaser for the number of parties those have not yet arrived be calling&amp;nbsp;getUnarrivedParties().&lt;/div&gt;&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;@Override
  public void run() {
   
   _phaser.register(); // register on the fly

   // First phase
   doSomeWork();

   int arr1 = _phaser.arrive(); // let the phaser know that you have arrived this point of the task
   int unarrivedParties1 = _phaser.getUnarrivedParties(); // ask the phaser how many parties are not yet here
   
   if (_phaser.getPhase() == arr1) {
    System.out.println(_taskId + &quot; completed phase &quot; + arr1 + &quot; and waiting arraival of &quot; + 
         unarrivedParties1 + &quot; threads so that it can enter phase &quot; + (arr1 + 1));
   }
   else {
    System.out.println(_taskId + &quot; completed phase &quot; + (arr1) + 
         &quot; (the last one to reach) and now all tasks will proceed to phase &quot; + (arr1 + 1));
   }
   
   _phaser.awaitAdvance(arr1); // be in sync with other threads

   
   
   // Second phase
   doSomeWork();

   int arr2 = _phaser.arrive(); // let the phaser know that you have arrived this point of the task
   int unarrivedParties2 = _phaser.getUnarrivedParties(); // ask the phaser how many parties are not yet here

   if (_phaser.getPhase() == arr2) {
    System.out.println(_taskId + &quot; completed phase &quot; + arr2 + &quot; and waiting arraival of &quot; + 
         unarrivedParties2 + &quot; threads so that it can enter phase &quot; + (arr2 + 1));
   }
   else {
    System.out.println(_taskId + &quot; completed phase &quot; + (arr2) + 
         &quot; (the last one to reach) and now all tasks will proceed to phase &quot; + (arr2 + 1));
   }
   _phaser.awaitAdvance(arr2); // be in sync with other threads

   // and so on ...
   
   // at some point a task could de-register itself from the phaser
   _phaser.arriveAndDeregister(); // de-registered threads is not considered need not be in sync with the other threads any more
   

  }
&lt;/pre&gt;&lt;br /&gt;
And this is the output:&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;Thread_1 completed phase 0 and waiting arraival of 2 threads so that it can enter phase 1
Thread_2 completed phase 0 and waiting arraival of 1 threads so that it can enter phase 1
Thread_0 completed phase 0 (the last one to reach) and now all tasks will proceed to phase 1
Thread_0 completed phase 1 and waiting arraival of 2 threads so that it can enter phase 2
Thread_1 completed phase 1 and waiting arraival of 1 threads so that it can enter phase 2
Thread_2 completed phase 1 (the last one to reach) and now all tasks will proceed to phase 2
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/323298450866494802/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/04/peek-into-jdk7-phaser-taking.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/323298450866494802'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/323298450866494802'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/04/peek-into-jdk7-phaser-taking.html' title='A peek into JDK7 - Java Phaser: Taking concurrency to the next level'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-4979764527894690189</id><published>2011-04-12T10:15:00.000-07:00</published><updated>2011-04-12T10:19:35.104-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="contention"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="lock"/><category scheme="http://www.blogger.com/atom/ns#" term="monitoring"/><category scheme="http://www.blogger.com/atom/ns#" term="multi-threaded"/><category scheme="http://www.blogger.com/atom/ns#" term="notify"/><category scheme="http://www.blogger.com/atom/ns#" term="notifyall"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="wait"/><title type='text'>Monitoring Java thread contention</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;In multi-threaded Java applications it is possible to see some thread contention due to some design or coding problem. Nevertheless if we can pinpoint the contention then it will be easy to find a way to solve the problem. Here we will see how to identify where the contention is and what the lock behind that contention is.&lt;br /&gt;
&lt;br /&gt;
Using&amp;nbsp; DTrace&#39;s monitor probes we can track the contention. &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/ThreadContention.java&quot;&gt;Here&lt;/a&gt; you can download the Java source code that I used.&lt;br /&gt;
&lt;br /&gt;
In the code you can see that I am using a plain Java Object as a monitor object (also called as lock object).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;Object monitorObject = new Object(); // our intrinsic lock
&lt;/pre&gt;&lt;br /&gt;
Inside the run() method of my MyCpuIntensiveTask, a executor thread will obtain this lock object before doing some CPU intensive calculation. By the mean time all the other executor threads will be waiting for the lock. Once the first thread exists the synchronized block one of the waiting thread will be granted the lock and so on....&lt;br /&gt;
&lt;br /&gt;
DTrace has the following monitor related probes.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-contended-enter&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-contended-entered&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-contended-exit&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-wait&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-waited&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-notify&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; monitor-notifyAll&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
The following is the DTrace script (using two of the probes) that I have used to test my Java code.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;monitor-contended-enter
{
this-&amp;gt;threadid = arg0;
this-&amp;gt;monitorid = arg1;
this-&amp;gt;monitorclass = (string)copyin(arg2, arg3+1);
printf(&quot;Thread %d trying to acquire monitor %d of type %s&quot;, this-&amp;gt;threadid, this-&amp;gt;monitorid, this-&amp;gt;monitorclass);
}

monitor-contended-entered
{
this-&amp;gt;threadid = arg0;
this-&amp;gt;monitorid = arg1;
this-&amp;gt;monitorclass = (string)copyin(arg2, arg3+1);
printf(&quot;Thread %d acquired monitor %d of type %s&quot;, this-&amp;gt;threadid, this-&amp;gt;monitorid, this-&amp;gt;monitorclass);
}
&lt;/pre&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;monitor-contended-enter - will be fired when a thread is trying to acquire a lock which is already held be another thread.&lt;/li&gt;
&lt;/ul&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;monitor-contended-entered - will be fired when a blocked thread enters successfully after acquiring the lock.&lt;/li&gt;
&lt;/ul&gt;The following is the Java code&#39;s output. As you can see Thread-7 wasn&#39;t blocked since it was the first one to acquire the lock, whereas the others (8,9,10,11) were blocked for some time.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;ram@opensolaris:~$ java -XX:+DTraceMonitorProbes -XX:+ExtendedDTraceProbes ThreadContention
Thread 7 trying to acquire lock.
Thread 7 entered.
Thread 8 trying to acquire lock.
Thread 9 trying to acquire lock.
Thread 10 trying to acquire lock.
Thread 11 trying to acquire lock.
Thread 7 exiting.
Thread 11 entered.
Thread 11 exiting.
Thread 10 entered.
Thread 10 exiting.
Thread 9 entered.
Thread 9 exiting.
Thread 8 entered.
Thread 8 exiting.
&lt;/pre&gt;&lt;br /&gt;
Exactly the same information is provided by the DTrace script as well but in a more detailed manner. Here we can see the monitor object to acquire which the threads were contending. 135291856 is the id of the object that the threads were trying to acquire. It is of type java/lang/Object. Ofcourse we can use an Integer or any other Java object as a lock.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;ram@opensolaris:~# dtrace -s threadContentionMonitor.d
dtrace: script &#39;threadContentionMonitor.d&#39; matched 2 probes
CPU     ID                    FUNCTION:NAME
  0   5076 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-enter Thread 8 trying to acquire monitor 135291856 of type java/lang/Object
  0   5076 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-enter Thread 9 trying to acquire monitor 135291856 of type java/lang/Object
  0   5076 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-enter Thread 10 trying to acquire monitor 135291856 of type java/lang/Object
  0   5076 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-enter Thread 11 trying to acquire monitor 135291856 of type java/lang/Object
  0   5077 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-entered Thread 11 acquired monitor 135291856 of type java/lang/Object
  0   5077 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-entered Thread 10 acquired monitor 135291856 of type java/lang/Object
  0   5077 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-entered Thread 9 acquired monitor 135291856 of type java/lang/Object
  0   5077 __1cNObjectMonitorFenter6MpnGThread__v_:monitor-contended-entered Thread 8 acquired monitor 135291856 of type java/lang/Object
&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/4979764527894690189/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/04/monitoring-java-thread-contention.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4979764527894690189'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4979764527894690189'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/04/monitoring-java-thread-contention.html' title='Monitoring Java thread contention'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-1411714972051012411</id><published>2011-04-02T04:02:00.000-07:00</published><updated>2011-04-02T04:02:41.249-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Atomicity"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="thread-safe"/><category scheme="http://www.blogger.com/atom/ns#" term="visualize"/><title type='text'>Visualizing Java Concurrency</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Have you ever wanted to visualize how a BlockingQueue or an Executor or a CountDownLatch or an AtomicInteger works.&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Download the jar from&amp;nbsp;&lt;a href=&quot;http://sourceforge.net/projects/javaconcurrenta/&quot;&gt;here&lt;/a&gt; or &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/concurrentanimated.jar&quot;&gt;here&lt;/a&gt; and have fun on watching the animation along with the corresponding code side-by-side.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;As a sample here is the screenshot for&amp;nbsp;BlockingQueue.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNb3XjWf1ZkMrryIhdgOzj3f2HPGrHxREpfpA114M2rlTT1E8AxP5_C3x_WZNmSo5Bc0TJfniE7xhG1LRDCvMQyn2TqGYNsbg4KEs-p10ZMj8J0qnPBEM7MvrwIsC9CC_qEIZ9xdvPCFkh/s1600/BlockingQueue.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNb3XjWf1ZkMrryIhdgOzj3f2HPGrHxREpfpA114M2rlTT1E8AxP5_C3x_WZNmSo5Bc0TJfniE7xhG1LRDCvMQyn2TqGYNsbg4KEs-p10ZMj8J0qnPBEM7MvrwIsC9CC_qEIZ9xdvPCFkh/s1600/BlockingQueue.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #362b36; font-family: Helvetica, Arial, sans-serif; font-size: 13px;&quot;&gt;Sync your files online and across computers with&amp;nbsp;&lt;span&gt;Dropbox&lt;/span&gt;. 2GB account is free!&amp;nbsp;&lt;a href=&quot;http://db.tt/307gDHm&quot; original-title=&quot;&quot; style=&quot;color: #666655; text-decoration: none;&quot;&gt;http://db.tt/307gDHm&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/1411714972051012411/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/04/visualizing-java-concurrency.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/1411714972051012411'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/1411714972051012411'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/04/visualizing-java-concurrency.html' title='Visualizing Java Concurrency'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNb3XjWf1ZkMrryIhdgOzj3f2HPGrHxREpfpA114M2rlTT1E8AxP5_C3x_WZNmSo5Bc0TJfniE7xhG1LRDCvMQyn2TqGYNsbg4KEs-p10ZMj8J0qnPBEM7MvrwIsC9CC_qEIZ9xdvPCFkh/s72-c/BlockingQueue.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-6278876657496802105</id><published>2011-04-02T03:23:00.000-07:00</published><updated>2011-04-02T03:33:31.502-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="fork and join"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="jdk7"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="thread-safe"/><title type='text'>Visualizing FJ (Fork And Join) Framework</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Earlier I made a post on the new FJ framework in JDK7 &lt;a href=&quot;http://kramasundar.blogspot.com/2010/03/peek-into-jdk7-forkjointask.html&quot;&gt;here&lt;/a&gt;. By the time there was no tool to visualize how FJ works. Today I came across a link where you can download a jar which helps us to visualize FJ.&lt;br /&gt;
&lt;br /&gt;
You can download the jar file from the &lt;a href=&quot;http://blog.krecan.net/2011/03/27/visualizing-forkjoin/&quot;&gt;original link&lt;/a&gt; or &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/fj-demo-1.0-SNAPSHOT-jar-with-dependencies.jar&quot;&gt;here&lt;/a&gt; from my &lt;a href=&quot;http://db.tt/t6l7NMr&quot;&gt;Dropbox&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Here is a sample screen shop of the jar demo, which sorts numbers from 1-32.&lt;br /&gt;
&lt;br /&gt;
Have fun.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeXKMPDL127_hqu01V4V2fflDUJVBFGk-r8egZ0u3AU_Lse7M2fMZumg8ky9iz91AX0kcqHp3CEevp6R0e4abAjNhVGeoEjEHmXHSZrD0Esw0El77qBjQIZdcXhjIlqhyphenhyphenSBt0AruDzYmpY/s1600/FJ.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeXKMPDL127_hqu01V4V2fflDUJVBFGk-r8egZ0u3AU_Lse7M2fMZumg8ky9iz91AX0kcqHp3CEevp6R0e4abAjNhVGeoEjEHmXHSZrD0Esw0El77qBjQIZdcXhjIlqhyphenhyphenSBt0AruDzYmpY/s1600/FJ.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/6278876657496802105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/04/visualizing-fork-and-join-framework.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6278876657496802105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6278876657496802105'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/04/visualizing-fork-and-join-framework.html' title='Visualizing FJ (Fork And Join) Framework'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeXKMPDL127_hqu01V4V2fflDUJVBFGk-r8egZ0u3AU_Lse7M2fMZumg8ky9iz91AX0kcqHp3CEevp6R0e4abAjNhVGeoEjEHmXHSZrD0Esw0El77qBjQIZdcXhjIlqhyphenhyphenSBt0AruDzYmpY/s72-c/FJ.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-6609016467734423703</id><published>2011-01-11T03:32:00.000-08:00</published><updated>2011-01-11T03:32:34.845-08:00</updated><title type='text'>A bit about java byte code</title><content type='html'>&lt;a href=&quot;http://arhipov.blogspot.com/2011/01/java-bytecode-fundamentals.html&quot;&gt;http://arhipov.blogspot.com/2011/01/java-bytecode-fundamentals.html&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/6609016467734423703/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2011/01/bit-about-java-byte-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6609016467734423703'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6609016467734423703'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2011/01/bit-about-java-byte-code.html' title='A bit about java byte code'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-4351937007826499142</id><published>2010-11-27T10:19:00.000-08:00</published><updated>2010-11-27T10:19:52.558-08:00</updated><title type='text'>Ever wondered why google&#39;s home page is that quick ?</title><content type='html'>Because they cheat ... Read more here :)&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://blog.benstrong.com/2010/11/google-and-microsoft-cheat-on-slow.html&quot;&gt;http://blog.benstrong.com/2010/11/google-and-microsoft-cheat-on-slow.html&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/4351937007826499142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/11/ever-wondered-why-googles-home-page-is.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4351937007826499142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4351937007826499142'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/11/ever-wondered-why-googles-home-page-is.html' title='Ever wondered why google&#39;s home page is that quick ?'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-3356315873767822026</id><published>2010-05-17T07:50:00.000-07:00</published><updated>2011-01-03T06:30:07.818-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CMS collector"/><category scheme="http://www.blogger.com/atom/ns#" term="garbage collection"/><category scheme="http://www.blogger.com/atom/ns#" term="gc"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Parallel collector"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><title type='text'>Parallel GC Vs Concurrent GC</title><content type='html'>Although the words&amp;nbsp;&quot;Parallel&quot; and&amp;nbsp;&quot;Concurrent&quot; are used&amp;nbsp;synonymously, they are not the same. Ok, not in the context of Java garbage collection at least.&lt;br /&gt;
&lt;br /&gt;
Parallel collector belongs to the family of pure-stop-the-world collectors. That means, GC won&#39;t kick in until JVM runs out of memory in the old-generation part of the heap. And when it starts all the mutator (application) threads will stop running.&lt;br /&gt;
&lt;br /&gt;
Where-as the concurrent collector (CMS) runs mostly-concurrent along with the other mutator (application) threads, and tries to free up memory so the mutator threads can keep on running. Nevertheless it also stops-the-world for 2 very short time periods called initial-mark and remark phases.&lt;br /&gt;
&lt;br /&gt;
I am not going to explain the internals of common GC techniques. For that purpose, &lt;a href=&quot;http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html&quot;&gt;please read this.&lt;/a&gt;&amp;nbsp;But I am going to show you visually what is the difference between these two collectors in terms of application throughput and responsiveness.&lt;br /&gt;
&lt;br /&gt;
In the test program (&lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/GcComparison.java&quot;&gt;download here&lt;/a&gt;) there are 3 mutator threads that&amp;nbsp;continuously&amp;nbsp;produce strings and put them into a map. Every 2 seconds another thread clears the map, i.e. all the cleared string objects are garbage and can be garbage collected. Both the test runs were of 60 seconds each. The tests are carried on a 16 core machine.&lt;br /&gt;
&lt;br /&gt;
During the first run parallel-old GC is used and the resulting VisualGC output is shown below:&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;java -XX:+PrintGCTimeStamps -verbose:gc -Xmx2G -Xms2G -XX:+UseParallelOldGC GcComparison
&lt;/pre&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeD2DsQjW6_-x3HJOoaZ-xcZ1s0-FaFWBW-0iWSFbg65kTlwVjIU3dYIcE3NrTVlIJbjgqT9gKx80DHaOLiI1foEE3S0dWLh1m3p_UEPwe1FzDaMg_bwlOqxp9FeKUkRWp2FnEtaJDS4mH/s1600/Parallel.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeD2DsQjW6_-x3HJOoaZ-xcZ1s0-FaFWBW-0iWSFbg65kTlwVjIU3dYIcE3NrTVlIJbjgqT9gKx80DHaOLiI1foEE3S0dWLh1m3p_UEPwe1FzDaMg_bwlOqxp9FeKUkRWp2FnEtaJDS4mH/s640/Parallel.PNG&quot; width=&quot;537&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
During the second run CMS collector was used and the resulting VisualGC output is shown below:&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;java -XX:+PrintGCTimeStamps -verbose:gc -Xmx2G -Xms2G -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=30 GcComparison&lt;/pre&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirR7pEN-5APKuFUpj1L73XMkEm0PxiZHt1AjxCjQ5aOMuTIg1FzdRz2YfYGlOkrRawEkkO-tQd16pyH4K1aXkuw4bDQi8_wf9lTSeUcyxHJwrrM9TBB1SVsndFbd6rIVUO6WgwTLCAf_Vc/s1600/Cms.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirR7pEN-5APKuFUpj1L73XMkEm0PxiZHt1AjxCjQ5aOMuTIg1FzdRz2YfYGlOkrRawEkkO-tQd16pyH4K1aXkuw4bDQi8_wf9lTSeUcyxHJwrrM9TBB1SVsndFbd6rIVUO6WgwTLCAf_Vc/s640/Cms.PNG&quot; width=&quot;540&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Let us look into some graphs.&lt;br /&gt;
&lt;br /&gt;
GC Time (3rd from top):&lt;br /&gt;
- Parallel: 32.4s for 59 collections. 55 (young gen) + 4 (old gen).&lt;br /&gt;
- Cms: 28.01s for 104 collections. 78&amp;nbsp;(young gen)&amp;nbsp;+ 26&amp;nbsp;(old gen).&lt;br /&gt;
- Even though the number of collections for parallel &amp;lt; cms, the total time for which the application threads were stopped for parallel &amp;gt; cms.&lt;br /&gt;
- Note that the light green area &amp;gt; parallel. This means cms was running for more time than parallel collector. But even then the overall time consumption was less because it does the collecting process concurrently with the application threads.&lt;br /&gt;
&lt;br /&gt;
Eden Space (4th from top):&lt;br /&gt;
-There is not much difference here.&lt;br /&gt;
&lt;br /&gt;
Old Gen (7th from top or 2nd from bottom):&lt;br /&gt;
-Parallel: 4.2s for 4 collections. 4 Full GC. ie. application threads were stopped for 4.2s.&lt;br /&gt;
-Cms: 1.1s for 26 collections. No Full GC.&amp;nbsp;ie. application threads were stopped only for 1.1s.&lt;br /&gt;
-Also you can see that cms collector works concurrently (gradual rise and fall) whereas parallel stops-the-world(4 spikes).&lt;br /&gt;
-The height of the gradual rise and fall of cms can be adjusted with-XX:CMSInitiatingOccupancyFraction option. This options tells at which point cms collector should start working.&lt;br /&gt;
&lt;br /&gt;
Hence it is better to use:&lt;br /&gt;
1.cms collector when&lt;br /&gt;
-you have high number of cpus&lt;br /&gt;
-your application demands short pauses&lt;br /&gt;
-you have more memory&lt;br /&gt;
&lt;br /&gt;
2.parallel collector when&lt;br /&gt;
-you have less number of cpus&lt;br /&gt;
-your application demands throughput and can withstand recurring long pauses&lt;br /&gt;
-you have less memory</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/3356315873767822026/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/05/parallel-gc-vs-concurrent-gc.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3356315873767822026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3356315873767822026'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/05/parallel-gc-vs-concurrent-gc.html' title='Parallel GC Vs Concurrent GC'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeD2DsQjW6_-x3HJOoaZ-xcZ1s0-FaFWBW-0iWSFbg65kTlwVjIU3dYIcE3NrTVlIJbjgqT9gKx80DHaOLiI1foEE3S0dWLh1m3p_UEPwe1FzDaMg_bwlOqxp9FeKUkRWp2FnEtaJDS4mH/s72-c/Parallel.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-3409114953024852094</id><published>2010-05-12T02:10:00.000-07:00</published><updated>2010-05-12T02:14:13.850-07:00</updated><title type='text'>Firefox Vs Chrome - Screen area</title><content type='html'>Some of my friends and colleagues say that Chrome is occupying less screen area than Firefox (ie. the menubar + navigationbar + etc). I think it is true when you run firefox with default settings. But when you tweak a bit you get more browsing space in Firefox than in Chrome.&lt;br /&gt;
&lt;br /&gt;
Have a look in the following picture and decide yourself. I think that the difference is not that much as the others exaggerate.&lt;br /&gt;
&lt;br /&gt;
My vote is always for Firefox. &lt;br /&gt;
&lt;br /&gt;
Browse more - learn more :) &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1EfWY2ZQxFt4X5DEmSpqby51o86kyFeDMFkbBMa6vAK6C3WY420je4rsYI2s7qsXOz-Ap-_wCH1gA0oran3xYCT2QWfqqhqBKl4eaj7R9cXgsZlZvP7tKk77m0EXEcwP3bIRLnDcWAXOB/s1600/FirefoxVsChrome.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;50&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1EfWY2ZQxFt4X5DEmSpqby51o86kyFeDMFkbBMa6vAK6C3WY420je4rsYI2s7qsXOz-Ap-_wCH1gA0oran3xYCT2QWfqqhqBKl4eaj7R9cXgsZlZvP7tKk77m0EXEcwP3bIRLnDcWAXOB/s640/FirefoxVsChrome.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span id=&quot;goog_1120871895&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_1120871896&quot;&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/3409114953024852094/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/05/firefox-vs-chrome-screen-area.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3409114953024852094'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3409114953024852094'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/05/firefox-vs-chrome-screen-area.html' title='Firefox Vs Chrome - Screen area'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1EfWY2ZQxFt4X5DEmSpqby51o86kyFeDMFkbBMa6vAK6C3WY420je4rsYI2s7qsXOz-Ap-_wCH1gA0oran3xYCT2QWfqqhqBKl4eaj7R9cXgsZlZvP7tKk77m0EXEcwP3bIRLnDcWAXOB/s72-c/FirefoxVsChrome.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-3923840048416148300</id><published>2010-04-06T03:32:00.000-07:00</published><updated>2010-04-06T04:39:51.812-07:00</updated><title type='text'>A peek into JDK7 - java.util.Objects class</title><content type='html'>There are some nice(?) utility methods available with JDK7.&lt;br /&gt;
&lt;br /&gt;
The java.util.Objects comes with nearly a dozen of very simple static utility methods. But at least I don&#39;t see any big usage of these methods ;), since most of them are available (in some other form) in the older JDK versions.&lt;br /&gt;
&lt;br /&gt;
In the following we will see some sample code for 2 of the newly introduced methods.&lt;br /&gt;
&lt;a href=&quot;http://download.java.net/jdk7/docs/api/java/util/Objects.html&quot;&gt;You can have a look at the rest of them here.&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
equals(Object a, Object b) - Returns true if the arguments are equal to each other and false otherwise.&lt;br /&gt;
hashCode(Object o) - Returns the hash code of a non-null argument and 0 for a null argument.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;Integer obj1 = new Integer( 1 );
Integer obj2 = new Integer( 1 );
Integer objNull = null;
System.out.println( Objects.equals( obj1, obj2 ) );
System.out.println( Objects.equals( obj1, objNull ) );
&lt;/pre&gt;&lt;br /&gt;
prints the following...&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;true
false
&lt;/pre&gt;&lt;br /&gt;
One can get the hash code of an object using (Line 1) Objects.hashCode(Object o). But i don&#39;t know what is its purpose since we already have Object.hashCode() that does exactly the same(Line 2).&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;Integer obj5 = new Integer( 5 );
  //(Line 1)new one
  System.out.println( Objects.hashCode( obj5 ) );
  //(Line 2)this is the already existing method
  System.out.println( obj5.hashCode() );
&lt;/pre&gt;Prints the following &lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;5
5
&lt;/pre&gt;&lt;br /&gt;
Let us see whether the Java guys add some more utility methods before the release of JDK7.&lt;br /&gt;
&lt;br /&gt;
Stay tuned.</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/3923840048416148300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/04/peek-into-jdk7-javautilobjects-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3923840048416148300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/3923840048416148300'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/04/peek-into-jdk7-javautilobjects-class.html' title='A peek into JDK7 - java.util.Objects class'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-7567612833178392385</id><published>2010-03-31T04:13:00.000-07:00</published><updated>2010-08-12T13:20:55.966-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Atomicity"/><category scheme="http://www.blogger.com/atom/ns#" term="cache"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="thread-safe"/><title type='text'>Java - Cache (frequently used immutable objects) using ConcurrentHashMap.</title><content type='html'>One of the better way to reduce the impact of garbage collection in any Java application is to reduce the number of newly created objects thereby reducing the amount of garbage produced.&lt;br /&gt;
&lt;br /&gt;
Let us take an example of university admission application. There are two possible titles (Masters and Bachelors) and two possible majors (Computer and Electrical). Let us name the combination of title-and-major as Degree.&lt;br /&gt;
&lt;br /&gt;
Assume that there are thousands of applications submitted every day. Each application will be for the particular combination of the above mentioned Degree. eg. Masters-Computer, Master-Electrical, Bachelor-Computer...&lt;br /&gt;
&lt;br /&gt;
Instead of creating new Degree object for each and every application, we can cache the Degree object. When a particular Degree object is demanded, look for that in the cache. If the cache doesn&#39;t contain that, then create it, put it inside the cache and then return it. Since the cache is built using&amp;nbsp;ConcurrentHashMap, it is also thread safe. i.e, Even when there are more than 1 thread running the Degree.valueOf() method for a same set of &quot;title&quot; and &quot;major&quot; strings, ONLY one instance of that particular Degree instance will be constructed and will be used by the threads.&lt;br /&gt;
&lt;br /&gt;
It is clear that we will produce less garbage using caching. But is there is another add-on advantage. It is easier to check whether two Degree objects are equal using reference equality (==) rather that equals() method. ie. we can do Master_Computer==Master_Computer_ANOTHER instead of Master_Computer.equals(Master_Computer_ANOTHER). On my machine == is 9 times faster than equals(). Thereby saving some CPU cycles.&lt;br /&gt;
&lt;br /&gt;
Have a look in the following code on how to build a cache of Objects (Degree) having two String properties. Complete code &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/MapCacheTest.java&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
The main method is the Degree.valueOf() method. Where all the caching is done.&lt;br /&gt;
&lt;br /&gt;
This is just an example. This technique can be used in the telco application servers running with load in terms of 1000s of TPS. Eg. Consider a header/value type of protocol. Instead of creating a particular header 1000s of times per second we can reuse the existing cached header thereby relaxing the CPU and RAM for other useful processing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;......

        Degree MASTER_COMPUTER = Degree.valueOf(MASTER, COMPUTER);
        Degree MASTER_ELECTRICAL = Degree.valueOf(MASTER, ELECTRICAL);
        Degree BACHELOR_COMPUTER = Degree.valueOf(BACHELOR, COMPUTER);
        Degree BACHELOR_ELECTRICAL = Degree.valueOf(BACHELOR, ELECTRICAL);
&lt;/pre&gt;&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;......

        // we ask the cache for all the possible present values that were created by the above lines.
        // Therefore it returns the existing values; Nothing is created anew.
        System.out.println(&quot;\nNo more new constructions for existing entries...&quot;);
        Degree MASTER_COMPUTER_1 = Degree.valueOf(MASTER, COMPUTER);

......
        // now ask for something that is not there; cache will create it anew and caches them
        System.out.println(&quot;\nNew constructions for non-existing entries...&quot;);
        Degree.valueOf(&quot;Phd&quot;, &quot;Computer&quot;);

......    
        // one more advantage of caching : we can compare the reference of two objects instead of checking their equal()ity
        // this is because all requests to Degree.valueOf(MASTER, COMPUTER) always return the very same object
        System.out.println(&quot;\nFaster equality check...&quot;);

        

&lt;/pre&gt;Output&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;New Degree object: Master_Computer
New Degree object: Master_Electrical
New Degree object: Bachelor_Computer
New Degree object: Bachelor_Electrical

No more new constructions for existing entries...
Returning existing instance of Master_Computer

New constructions for non-existing entries...
New Degree object: Phd_Computer

Faster equality check...
Checked with equals().
Checked with reference equality.
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/7567612833178392385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/03/java-cache-frequently-used-immutable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/7567612833178392385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/7567612833178392385'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/03/java-cache-frequently-used-immutable.html' title='Java - Cache (frequently used immutable objects) using ConcurrentHashMap.'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-9097300824530506427</id><published>2010-03-11T14:24:00.000-08:00</published><updated>2010-08-12T13:30:16.480-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="jdk7"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><title type='text'>A peek into JDK7 - ForkJoinTask example (RecursiveAction example, Forkjoinpool example)</title><content type='html'>Consider tasks like sorting an array, doing a complex math on each and every element in an array. Eg. we want to increment by 1 all the elements in the array {0,1,2,3,4,5,6,7,8,9}.&lt;br /&gt;
&lt;br /&gt;
The simplest way is to loop over the entire array and do array[i]=array[i]+1. However this will run in  a single thread.&lt;br /&gt;
&lt;br /&gt;
But what if we can take advantage of multi-core CPUs, i.e. break the array into two halves and give it to two threads. So that the first thread operates on the left-half (thereby modifying the array entries to {1,2,3,4,5,......}) of the array whereas the second thread operates on the second-half of the array (thereby modifying the array entries to {......,6,7,8,9,10}).&lt;br /&gt;
&lt;br /&gt;
The ....s means that the corresponding thread doesn&#39;t know what is there. It doesn&#39;t have to care. It is not part of its job!&lt;br /&gt;
&lt;br /&gt;
This is where the JDK7&#39;s ForkJoinTask comes into the play. We give a complex task to be executed.  Along with that we also have to specify a threshold. If the task&#39;s size is greater than the threshold then the task divides itself and fork()s them and wait for them to finish by join()ing. Hence the name ForkJoinTask. There are two implementation of ForkJoinTask - RecursiveAction and RecursiveTask.&lt;br /&gt;
&lt;br /&gt;
Here is an example. The applyAlgorithm() is the CPU intensive method where each element in the array is modified. When the array is bigger than 5000 (threshold), then the array is divided into two and the two new arrays are handled in parallel by the threads available in the ForkJoinPool.&lt;br /&gt;
&lt;br /&gt;
Following are the results from 2 different machines. One on a 16 core machine and another on a dual core machine. In both the cases the parallel execution is well ahead the single threaded numbers.&lt;br /&gt;
&lt;br /&gt;
You can download the java code &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/ForkJoinAlgoritmicTask.java&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
1. On a 16 core machine&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;myServer $ java -cp test/jsr166y.jar:. ForkJoinAlgoritmicTask
Number of processor available: 16
Array size: 10000000
Treshhold: 5000
Number of runs: 5
 
Parallel processing time: 198
Parallel processing time: 69
Parallel processing time: 64
Parallel processing time: 61
Parallel processing time: 59

Number of steals: 579

Sequential processing time: 438
Sequential processing time: 437
Sequential processing time: 436
Sequential processing time: 436
Sequential processing time: 437
&lt;/pre&gt;&lt;br /&gt;
2. On a 2 core machine&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;muruga-Study$java -cp jsr166y.jar:. -Xms1G -Xmx1G ForkJoinAlgoritmicTask
Number of processor available: 2
Array size: 10000000
Treshhold: 5000
Number of runs: 5

Parallel processing time: 227
Parallel processing time: 206
Parallel processing time: 226
Parallel processing time: 203
Parallel processing time: 208
Number of steals: 12

Sequential processing time: 385
Sequential processing time: 385
Sequential processing time: 385
Sequential processing time: 385
Sequential processing time: 385
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/9097300824530506427/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/03/peek-into-jdk7-forkjointask.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/9097300824530506427'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/9097300824530506427'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/03/peek-into-jdk7-forkjointask.html' title='A peek into JDK7 - ForkJoinTask example (RecursiveAction example, Forkjoinpool example)'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-5576968307377000449</id><published>2010-03-10T08:59:00.000-08:00</published><updated>2010-03-10T09:00:00.008-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Netbeans"/><title type='text'>Adapting netbeans default license template.</title><content type='html'>After participating in the &lt;a href=&quot;http://www.javapassion.com/javaee6/&quot;&gt;JavaEE6 codecamp&lt;/a&gt;, somehow I got in love with Netbeans.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But whenever I create a new java file, the editor kept on adding the following default license template to the files.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/&lt;/pre&gt;&lt;br /&gt;
If you would like to avoid this then go to &quot;Tools-&amp;gt;Templates&quot;. The &quot;Template Manager&quot; window will pop up and there expand the &quot;Licenses&quot; folder. Select &quot;Default License&quot; and click &quot;Open in Editor&quot; button (at the bottom).&lt;br /&gt;
&lt;br /&gt;
There you can customize the license text or you can just delete the contents.&lt;br /&gt;
&lt;br /&gt;
Hope this helps someone.</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/5576968307377000449/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/03/adapting-netbeans-default-license.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/5576968307377000449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/5576968307377000449'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/03/adapting-netbeans-default-license.html' title='Adapting netbeans default license template.'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-7097032363468083104</id><published>2010-02-25T09:20:00.000-08:00</published><updated>2010-08-12T13:36:48.794-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Atomicity"/><category scheme="http://www.blogger.com/atom/ns#" term="CAS"/><category scheme="http://www.blogger.com/atom/ns#" term="concurrency"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="thread-safe"/><title type='text'>Optimistic CAS Vs Pessimistic synchronized...</title><content type='html'>As &lt;a href=&quot;http://www.ibm.com/developerworks/java/library/j-jtp04186/index.html&quot;&gt;Brian Goetz&lt;/a&gt; says, be optimistic and not pessimistic.&lt;br /&gt;
&lt;br /&gt;
Exclusive (synchronized) locking is pessimistic and &lt;a href=&quot;http://en.wikipedia.org/wiki/Compare-and-swap&quot;&gt;CAS&lt;/a&gt; (compare-and-swap/set) used by AtomicInteger, AtomicLongs etcs are optimistic.&lt;br /&gt;
&lt;br /&gt;
Synchronized (locking) is pessimistic in the sense that we fear that something can go wrong, so lock our stuff and do our work.&lt;br /&gt;
&lt;br /&gt;
CAS is optimistic in the sense that we do some work optimistically and then try to commit our work. But if another guy has did what we did, we re-try to do our work once again.&lt;br /&gt;
&lt;br /&gt;
More here in DeveloperWorks article on &lt;a href=&quot;https://www.ibm.com/developerworks/java/library/j-jtp11234/#2.0&quot;&gt;Going Atomic&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I wanted to see how CAS outperforms synchronized stuffs and the following code was the outcome.&lt;br /&gt;
&lt;br /&gt;
Here we increment two variables with two different tasks. &lt;br /&gt;
&lt;br /&gt;
The first task &lt;span style=&quot;font-style: italic;&quot;&gt;PlainIncrementTask&lt;/span&gt; increments the plain int of the &lt;span style=&quot;font-style: italic;&quot;&gt;Holder &lt;/span&gt;class using the custom-built synchronized &lt;span style=&quot;font-style: italic;&quot;&gt;getPlain() &lt;/span&gt;and &lt;span style=&quot;font-style: italic;&quot;&gt;incrementPlain() &lt;/span&gt;methods.&lt;br /&gt;
&lt;br /&gt;
The second task AtomicIncrementTask increments the AtomicInteger of the &lt;span style=&quot;font-style: italic;&quot;&gt;Holder  &lt;/span&gt;class using the &lt;span style=&quot;font-style: italic;&quot;&gt;AtomicInteger&lt;/span&gt;&#39;s &lt;span style=&quot;font-style: italic;&quot;&gt;incrementAndGet()&lt;/span&gt; and &lt;span style=&quot;font-style: italic;&quot;&gt;get()&lt;/span&gt; methods.&lt;br /&gt;
&lt;br /&gt;
You can download the code &lt;a href=&quot;http://dl.dropbox.com/u/2736804/blogFiles/Holder.java&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;......
public int incrementAtomic() {
return atomicInteger.incrementAndGet();
}

public int getAtomic() {
return atomicInteger.get();
}

synchronized public int incrementPlain() {
return ++plainInteger;
}

synchronized public int getPlain() {
return plainInteger;
}
.....

&lt;/pre&gt;And the results (of course) favour AtomicInteger.  &lt;br /&gt;
&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;mbp $ java Holder
Time taken for PLAIN: 1010 ms
Time taken for ATOMIC: 171 ms
mbp $ java Holder
Time taken for PLAIN: 1045 ms
Time taken for ATOMIC: 169 ms
mbp $ java Holder
Time taken for PLAIN: 1043 ms
Time taken for ATOMIC: 172 ms
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
So it is always better to use Atomic.* for counters and so on.&lt;br /&gt;
&lt;br /&gt;
And never try to outperform the Java Gurus. ;)</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/7097032363468083104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2010/02/optimistic-cas-vs-pessimistic.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/7097032363468083104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/7097032363468083104'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2010/02/optimistic-cas-vs-pessimistic.html' title='Optimistic CAS Vs Pessimistic synchronized...'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-9074504485812952737</id><published>2009-12-08T12:09:00.000-08:00</published><updated>2010-03-12T02:46:39.648-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><category scheme="http://www.blogger.com/atom/ns#" term="StringBuilder"/><title type='text'>Java String concatenation Vs StringBuilder (using Dtrace object allocation probe)</title><content type='html'>&quot;Item 51: Beware the performance of string concatenation&quot; - Effective Java by Joshua Bloch.&lt;br /&gt;
&lt;br /&gt;
Would you like to see how evil String concatenation in java is? continue reading...&lt;br /&gt;
&lt;br /&gt;
Just to avoid these overheads later versions of java introduced StringBuffer(synchronized) and then StringBuilder(unsynchronized) classes.&lt;br /&gt;
&lt;br /&gt;
Never ever use plain string concatenation in any production code. To know why run the following code on your machine...&lt;br /&gt;
&lt;br /&gt;
&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;public class StringSpeed {

public static void main(String[] args) {

try {
Thread.sleep(10000);
}
catch ( InterruptedException e ) {
e.printStackTrace();
}

int N = 100000;

String temp = &quot;&quot;;
long start = System.currentTimeMillis();

for ( int i = 0; i &lt; N; i++ ) {
temp = temp + &quot;*&quot;;
}

long stop = System.currentTimeMillis();
System.out.println(stop - start);

StringBuilder tempBuilder = new StringBuilder();
start = System.currentTimeMillis();

for ( int i = 0; i &lt; N; i++ ) {
tempBuilder.append(&quot;*&quot;);
}

stop = System.currentTimeMillis();
System.out.println(stop - start);
}
}
&lt;/pre&gt;


Since the first loop used plain string concatenation it took quite long... whereas the second loop crossed the finish line much quicker.

output(on my machine):
======================
&lt;pre name=&quot;code&quot; class=&quot;console&quot;&gt;13328
8
&lt;/pre&gt;

Isn&#39;t the difference worth enough ;) ?

The reason for the difference is that the concatenation using &quot;+&quot; has to create so many temp String/StringBuffer objects.

To look how many we can use the following dtrace script.

&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;:::object-alloc {
self-&gt;str_ptr = (char*) copyin(arg1, arg2+1);
self-&gt;str_ptr[arg2] = &#39;\0&#39;;
self-&gt;classname = (string) self-&gt;str_ptr;
@allocs_count[self-&gt;classname] = count();
}
&lt;/pre&gt;

Output for first loop (yes!!! 90000+ Strings and 84000+ StringBuilders)
&lt;pre name=&quot;code&quot; class=&quot;console&quot;&gt;[Ljava/lang/Runnable; 1
java/lang/Shutdown$Lock 1
java/lang/Thread 1
java/security/AccessControlContext 1
[B 3
[[I 3
[S 6
[I 8
java/lang/StringBuilder 84414
java/lang/String 90416
[C 400008
&lt;/pre&gt;

For the second loop it is just...
&lt;pre name=&quot;code&quot; class=&quot;console&quot;&gt;[Ljava/lang/Runnable; 1
java/lang/Shutdown$Lock 1
java/lang/StringBuilder 1
java/lang/Thread 1
java/security/AccessControlContext 1
[B 2
[[I 2
[S 4
[I 7
java/lang/String 14
[C 62
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/9074504485812952737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/12/java-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/9074504485812952737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/9074504485812952737'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/12/java-string.html' title='Java String concatenation Vs StringBuilder (using Dtrace object allocation probe)'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-854948359306298259</id><published>2009-10-29T06:27:00.000-07:00</published><updated>2009-12-10T06:32:46.655-08:00</updated><title type='text'>Extracting/Unzipping tar.gz file under Solaris</title><content type='html'>Even though it is a one line command sometimes it is bit hard to remember the syntax...&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;console&quot;&gt;&lt;br /&gt;gzip -dc zippedFile.tar.gz | tar xf -&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/854948359306298259/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/10/extracting-targz-file-under-solaris.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/854948359306298259'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/854948359306298259'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/10/extracting-targz-file-under-solaris.html' title='Extracting/Unzipping tar.gz file under Solaris'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-4042655621156606687</id><published>2009-09-11T06:40:00.000-07:00</published><updated>2010-05-17T06:52:40.166-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="folder"/><category scheme="http://www.blogger.com/atom/ns#" term="mount"/><category scheme="http://www.blogger.com/atom/ns#" term="opensolaris"/><category scheme="http://www.blogger.com/atom/ns#" term="share"/><category scheme="http://www.blogger.com/atom/ns#" term="windows xp"/><title type='text'>Opensolaris Virtualbox folder share (mount)</title><content type='html'>&lt;div style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDrlp422hXyNl4HrjjisxOB6WXeRByX9CkH0lIJ0WSzEHTzCZj8j2WF-AZ1kTVV0FG3cvxurUxGAd_i4duaDgIDCqFP_W3LOHWrlyygbmEAPNf7ywdhPsjadfb5vrLWzr4X_Eyh2Zw9mJd/s1600-h/VirtualBoxError.PNG&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;br /&gt;
&lt;/a&gt;&lt;br /&gt;
I was trying to share some folders from my windows xp HOST to opensolaris GUEST. But always i got &quot;&lt;span style=&quot;color: #000099;&quot;&gt;Operation not applicable to FSType vboxsf&lt;/span&gt;&quot;.&lt;br /&gt;
&lt;br /&gt;
even after 2 hours of  googling i couldn&#39;t get the correct answer... And then came my (actually Sun&#39;s) mistake to sunlight... because in the virtual box&#39;s context help, it is given (look at the image below) that we have to issue &quot;&lt;span style=&quot;color: #000099;&quot;&gt;mount -t vbox&lt;span style=&quot;color: red;&quot;&gt;sf &lt;/span&gt;share mount_point&lt;/span&gt;&quot; to mount the shared folder.&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQmylD-nHhU2Fp1_UOAkORughZAjKEZBOPLqF0gfxYeX4mM3dZYnuC7G2Szl0WxcdGPizUwTSBLUZMcmC9Saa6VcUhanCbRZxybiQRQsHWsqfl8tdoLlJoC5g6sVpl5BPe_VwmpkVyi8fl/s1600/virtualBoxError.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;278&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQmylD-nHhU2Fp1_UOAkORughZAjKEZBOPLqF0gfxYeX4mM3dZYnuC7G2Szl0WxcdGPizUwTSBLUZMcmC9Saa6VcUhanCbRZxybiQRQsHWsqfl8tdoLlJoC5g6sVpl5BPe_VwmpkVyi8fl/s320/virtualBoxError.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
But it is should NOT be&lt;br /&gt;
&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;mount -t vboxsf share mount_point&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;(Operation not applicable to FSType &lt;span style=&quot;color: #000099;&quot;&gt;vbox&lt;span style=&quot;color: red;&quot;&gt;sf &lt;/span&gt;&lt;/span&gt; because there is nothing like that but &lt;span style=&quot;color: #000099;&quot;&gt;vbox&lt;span style=&quot;color: red;&quot;&gt;fs&lt;/span&gt;&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
but it should be&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;console&quot; name=&quot;code&quot;&gt;mount -F vboxfs share mount_point&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;It does make sense &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3333ff;&quot;&gt;vboxfs&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #3333ff;&quot;&gt; &lt;/span&gt;--&amp;gt; virtualbox filesystem&lt;br /&gt;
&lt;br /&gt;
Atlast happy to have my files accessible in open solaris.&lt;br /&gt;
&lt;br /&gt;
hope someone will land here to get helped.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/4042655621156606687/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/09/opensolaris-virtualbox-folder-share.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4042655621156606687'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/4042655621156606687'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/09/opensolaris-virtualbox-folder-share.html' title='Opensolaris Virtualbox folder share (mount)'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQmylD-nHhU2Fp1_UOAkORughZAjKEZBOPLqF0gfxYeX4mM3dZYnuC7G2Szl0WxcdGPizUwTSBLUZMcmC9Saa6VcUhanCbRZxybiQRQsHWsqfl8tdoLlJoC5g6sVpl5BPe_VwmpkVyi8fl/s72-c/virtualBoxError.PNG" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-1109052461498223135</id><published>2009-08-29T14:30:00.000-07:00</published><updated>2010-03-11T02:15:04.366-08:00</updated><title type='text'>Stockholm == cyclist city</title><content type='html'>Last week I have been there at Stockholm. Nice and green city. Since i am living in Frankfurt for the past 2 years, it was  a different experience for me. In Germany (I have lived in 3 to 4 cities in Germany), you cannot see that many cyclists in Stockholm. They really have a broader lane, even in busy routes, just for cyclists.&lt;br /&gt;
&lt;br /&gt;
There were plenty of cyclists in Stockholm. They help to keep the air fresh and themselves active. Just for comparison in Frankfurt the car:cycle ratio would be 500:1 if not 1000:1.&lt;br /&gt;
&lt;br /&gt;
Only after coming back and after doing a little research i came to know that these days European countries are taking effort to cut down emission by encouraging people to use cycles.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://news.bbc.co.uk/2/hi/europe/8224141.stm&quot;&gt;BBC News&lt;/a&gt; tells you more.</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/1109052461498223135/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/08/stockholm-cyclist-city.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/1109052461498223135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/1109052461498223135'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/08/stockholm-cyclist-city.html' title='Stockholm == cyclist city'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-5388640393869845556</id><published>2009-06-02T01:17:00.000-07:00</published><updated>2009-06-02T02:32:24.465-07:00</updated><title type='text'>Java nio non blocking server &amp; client</title><content type='html'>Java supports nonblocking io since java 1.4.&lt;br /&gt;&lt;br /&gt;But just now i am really using it in one of my projects.&lt;br /&gt;&lt;br /&gt;The difference is that the server socket&#39;s accept() or a normal socket&#39;s read() or write() method need not be blocking (in a single thread) any more.&lt;br /&gt;E.g. with the normal io, if a server is executing the accept() method then it cannot do anything with the previously accepted sockets.&lt;br /&gt;&lt;br /&gt;It has changed with nio in the sense that a server can handle (within a single thread)&lt;br /&gt;1. several existing connections and &lt;br /&gt;2. new incoming connections&lt;br /&gt;&lt;br /&gt;Let us see an see an example.&lt;br /&gt;&lt;br /&gt;The server (Server.java) listens on port 9999 for any incoming connections.&lt;br /&gt;$&gt;javac Server.java&lt;br /&gt;$&gt;java Server&lt;br /&gt;&lt;br /&gt;The client (Client.java) sends the text &quot;I am Client : clientXXX&quot; to the server. XXX-&gt;is the command line argument; it is just an identifier to distinguish (on the server&#39;s console) different clients.&lt;br /&gt;$&gt;javac Client.java&lt;br /&gt;$&gt;java Client 354&lt;br /&gt;$&gt;java Client dfdsfsd&lt;br /&gt;&lt;br /&gt;Sample Output:&lt;br /&gt;Sample Output:&lt;br /&gt;serverSocketChannel&#39;s registered key is : sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:9999]&lt;br /&gt;&lt;br /&gt;Server is listening on: 127.0.0.1:9999&lt;br /&gt;Key ready to perform accept() : sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:9999]&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2633]&lt;br /&gt;I am Client : 354&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2633]&lt;br /&gt;I am Client : 354&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2633]&lt;br /&gt;I am Client : 354&lt;br /&gt;Key ready to perform accept() : sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:9999]&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2636]&lt;br /&gt;I am Client : dfdsfsd&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2633]&lt;br /&gt;I am Client : 354&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2636]&lt;br /&gt;I am Client : dfdsfsd&lt;br /&gt;Key ready to perform read() : java.nio.channels.SocketChannel[connected local=/127.0.0.1:9999 remote=/127.0.0.1:2633]&lt;br /&gt;I am Client : 354&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Server.java&lt;br /&gt;&lt;table width=&quot;100%&quot; bgcolor=&quot;#c0c0c0&quot; border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.net.InetAddress;&lt;br /&gt;import java.net.InetSocketAddress;&lt;br /&gt;import java.nio.ByteBuffer;&lt;br /&gt;import java.nio.channels.SelectionKey;&lt;br /&gt;import java.nio.channels.Selector;&lt;br /&gt;import java.nio.channels.ServerSocketChannel;&lt;br /&gt;import java.nio.channels.SocketChannel;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.Set;&lt;br /&gt;&lt;br /&gt;public class Server {&lt;br /&gt;&lt;br /&gt; // this is equivalent to the server socket in the non nio world&lt;br /&gt; ServerSocketChannel serverSocketChannel;&lt;br /&gt;&lt;br /&gt; // this is the multiplexer which multiplexes the messages received from different clients&lt;br /&gt; Selector selector;&lt;br /&gt;&lt;br /&gt; public Server() {&lt;br /&gt;  try {&lt;br /&gt;&lt;br /&gt;   // get a selector&lt;br /&gt;   selector = Selector.open();&lt;br /&gt;&lt;br /&gt;   // get a server socket channel&lt;br /&gt;   serverSocketChannel = ServerSocketChannel.open();&lt;br /&gt;&lt;br /&gt;   // we force the socket to be Non-blocking.&lt;br /&gt;   // if it is set to &quot;true&quot; then this socket acts as a normal (blocking) server socket&lt;br /&gt;   serverSocketChannel.configureBlocking(false);&lt;br /&gt;&lt;br /&gt;   // port and ip address where the server listens for connections&lt;br /&gt;   InetSocketAddress add = new InetSocketAddress(InetAddress.getLocalHost(), 9999);&lt;br /&gt;&lt;br /&gt;   // bind the server socket to the ip/port&lt;br /&gt;   serverSocketChannel.socket().bind(add);&lt;br /&gt;&lt;br /&gt;   // register the serverSocketChannel (for incoming connection events) to the selector.&lt;br /&gt;   // The &quot;SelectionKey.OP_ACCEPT&quot; parameter tells the selector that this serverSocketChannel registers&lt;br /&gt;   // itself for incoming (acceptable) connections&lt;br /&gt;   SelectionKey key = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);&lt;br /&gt;   System.out.println(&quot;serverSocketChannel&#39;s registered key is : &quot; + key.channel().toString());&lt;br /&gt;&lt;br /&gt;   System.out.println();&lt;br /&gt;  } catch (IOException e) {&lt;br /&gt;   e.printStackTrace();&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;  Server server = new Server();&lt;br /&gt;  server.startListening();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private void startListening() {&lt;br /&gt;&lt;br /&gt;  System.out.println(&quot;Server is listening on: &quot;&lt;br /&gt;    + serverSocketChannel.socket().getInetAddress().getHostAddress() + &quot;:&quot;&lt;br /&gt;    + serverSocketChannel.socket().getLocalPort());&lt;br /&gt;&lt;br /&gt;  while (true) {&lt;br /&gt;   try {&lt;br /&gt;&lt;br /&gt;    // this line blocks until some events has occurred in the underlying socket&lt;br /&gt;    selector.select();&lt;br /&gt;&lt;br /&gt;    // get the selected keys set&lt;br /&gt;    Set&lt;SelectionKey&gt; selectedKeys = selector.selectedKeys();&lt;br /&gt;&lt;br /&gt;    Iterator&lt;SelectionKey&gt; iterator = selectedKeys.iterator();&lt;br /&gt;&lt;br /&gt;    while (iterator.hasNext()) {&lt;br /&gt;&lt;br /&gt;     SelectionKey key = (SelectionKey) iterator.next();&lt;br /&gt;&lt;br /&gt;     iterator.remove();&lt;br /&gt;&lt;br /&gt;     // a client has asked for a new connection&lt;br /&gt;     if (key.isAcceptable()) {&lt;br /&gt;      // only ServerSocketsChannels registered for OP_ACCEPT are excepted to receive an&lt;br /&gt;      // &quot;acceptable&quot; key&lt;br /&gt;&lt;br /&gt;      System.out.println(&quot;Key ready to perform accept() : &quot; + key.channel().toString());&lt;br /&gt;&lt;br /&gt;      // as usual the accept returns the plain socket towards the client&lt;br /&gt;      SocketChannel client = serverSocketChannel.accept();&lt;br /&gt;&lt;br /&gt;      // set the client socket to be non blocking&lt;br /&gt;      client.configureBlocking(false);&lt;br /&gt;&lt;br /&gt;      // register the client socket with the same selector to which we have registered the&lt;br /&gt;      // serverSocketChannel&lt;br /&gt;      client.register(selector, SelectionKey.OP_READ);&lt;br /&gt;      continue;&lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;     // the client has sent something to be read by this server&lt;br /&gt;     if (key.isReadable()) {&lt;br /&gt;&lt;br /&gt;      System.out.println(&quot;Key ready to perform read() : &quot; + key.channel().toString());&lt;br /&gt;&lt;br /&gt;      // get the underlying socket&lt;br /&gt;      SocketChannel client = (SocketChannel) key.channel();&lt;br /&gt;      ByteBuffer bb = ByteBuffer.allocate(1024);&lt;br /&gt;&lt;br /&gt;      // read the msg sent by the client&lt;br /&gt;      client.read(bb);&lt;br /&gt;&lt;br /&gt;      // display the message&lt;br /&gt;      bb.flip();&lt;br /&gt;      byte[] array = new byte[bb.limit()];&lt;br /&gt;      bb.get(array);&lt;br /&gt;      System.out.println(new String(array));&lt;br /&gt;      continue;&lt;br /&gt;     }&lt;br /&gt;    }&lt;br /&gt;   } catch (IOException e) {&lt;br /&gt;    e.printStackTrace();&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;Client.java&lt;br /&gt;&lt;table width=&quot;100%&quot; bgcolor=&quot;#c0c0c0&quot; border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot;&gt;&lt;br /&gt;&lt;tr&gt;&lt;br /&gt;&lt;td&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.net.InetAddress;&lt;br /&gt;import java.net.InetSocketAddress;&lt;br /&gt;import java.nio.ByteBuffer;&lt;br /&gt;import java.nio.channels.SelectionKey;&lt;br /&gt;import java.nio.channels.Selector;&lt;br /&gt;import java.nio.channels.SocketChannel;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;import java.util.Set;&lt;br /&gt;&lt;br /&gt;public class Client {&lt;br /&gt;&lt;br /&gt; String myIdentity;&lt;br /&gt;&lt;br /&gt; public Client(String pIdentity) {&lt;br /&gt;  myIdentity = pIdentity;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; void talkToServer() {&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;&lt;br /&gt;   SocketChannel mySocket = SocketChannel.open();&lt;br /&gt;&lt;br /&gt;   // non blocking&lt;br /&gt;   mySocket.configureBlocking(false);&lt;br /&gt;&lt;br /&gt;   // connect to a running server&lt;br /&gt;   mySocket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9999));&lt;br /&gt;&lt;br /&gt;   // get a selector&lt;br /&gt;   Selector selector = Selector.open();&lt;br /&gt;&lt;br /&gt;   // register the client socket with &quot;connect operation&quot; to the selector&lt;br /&gt;   mySocket.register(selector, SelectionKey.OP_CONNECT);&lt;br /&gt;&lt;br /&gt;   // select() blocks until something happens on the underlying socket&lt;br /&gt;   while (selector.select() &gt; 0) {&lt;br /&gt;&lt;br /&gt;    Set&lt;SelectionKey&gt; keys = selector.selectedKeys();&lt;br /&gt;    Iterator&lt;SelectionKey&gt; it = keys.iterator();&lt;br /&gt;&lt;br /&gt;    while (it.hasNext()) {&lt;br /&gt;&lt;br /&gt;     SelectionKey key = it.next();&lt;br /&gt;&lt;br /&gt;     SocketChannel myChannel = (SocketChannel) key.channel();&lt;br /&gt;&lt;br /&gt;     it.remove();&lt;br /&gt;&lt;br /&gt;     if (key.isConnectable()) {&lt;br /&gt;      if (myChannel.isConnectionPending()) {&lt;br /&gt;       myChannel.finishConnect();&lt;br /&gt;       System.out.println(&quot;Connection was pending but now is finiehed connecting.&quot;);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      ByteBuffer bb = null;&lt;br /&gt;&lt;br /&gt;      while (true) {&lt;br /&gt;       bb = ByteBuffer.wrap(new String(&quot;I am Client : &quot; + myIdentity).getBytes());&lt;br /&gt;       myChannel.write(bb);&lt;br /&gt;       bb.clear();&lt;br /&gt;       synchronized (this) {&lt;br /&gt;        wait(3000);&lt;br /&gt;       }&lt;br /&gt;      }&lt;br /&gt;     }&lt;br /&gt;    }&lt;br /&gt;   }&lt;br /&gt;  } catch (IOException e) {&lt;br /&gt;      e.printStackTrace();&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;  Client client = new Client(args[0]);&lt;br /&gt;  client.talkToServer();&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/td&gt;&lt;br /&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/5388640393869845556/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/06/java-supports-nonblocking-io-since-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/5388640393869845556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/5388640393869845556'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/06/java-supports-nonblocking-io-since-java.html' title='Java nio non blocking server &amp; client'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7784832589817032133.post-6304811041462104566</id><published>2009-05-22T00:16:00.000-07:00</published><updated>2009-05-22T00:47:10.427-07:00</updated><title type='text'>SCTPing in Java</title><content type='html'>&lt;a href=&quot;http://blogs.sun.com/chegar/entry/sctp_in_java&quot;&gt;blog from Chris Hegarty - the lead for SCTP integration in JDK7&lt;/a&gt;&lt;a href=&quot;http://blogs.sun.com/chegar/entry/sctp_in_java&quot;&gt;&lt;br /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://kramasundar.blogspot.com/feeds/6304811041462104566/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kramasundar.blogspot.com/2009/05/test.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6304811041462104566'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7784832589817032133/posts/default/6304811041462104566'/><link rel='alternate' type='text/html' href='http://kramasundar.blogspot.com/2009/05/test.html' title='SCTPing in Java'/><author><name>Ramsundar Kandasamy</name><uri>http://www.blogger.com/profile/06770624793338489640</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='http://4.bp.blogspot.com/_XeQ0YseQaCw/S7SbuhnvSNI/AAAAAAAACBM/net-uMHJv-A/S220/Think+Green.jpg'/></author><thr:total>0</thr:total></entry></feed>