<?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-674557581614690902</id><updated>2026-10-09T01:51:48.331-07:00</updated><category term="Java.util.Concurrent"/><category term="Threads in JAVA"/><category term="Generics in Java"/><category term="Generics Practice Questions"/><category term="COM PORT"/><category term="COM PORT Communicaiton using java"/><category term="Encapsulation"/><category term="Encapsulation in Java"/><category term="waiting"/><category term="waiting and yielding in threads"/><category term="waiting in threads"/><category term="yielding"/><category term="yielding in threads"/><category term="ASCII in JAVA"/><category term="ASCII to Hex"/><category term="Callable"/><category term="Classes"/><category term="Condition"/><category term="DDD"/><category term="Data Hiding in Java"/><category term="Deadly Diamond of Death in JAVA"/><category term="Deadly diamond of Death"/><category term="Future"/><category term="GC"/><category term="Garbage Collection"/><category term="Hex in Java"/><category term="Hex to ASCII"/><category term="Inheritance"/><category term="Inheritance breaks Encapsulation"/><category term="Initialization in JAVA"/><category term="Interface Concept"/><category term="Interface in JAVA"/><category term="JAVA 5"/><category term="Locale in Java"/><category term="Lock"/><category term="Locks"/><category term="Object Initialization in JAVA"/><category term="Runnable"/><category term="Runnable Vs Callable"/><category term="Serialization"/><category term="Serialization in JAVA"/><category term="Time and Locale"/><category term="Time as per Locale"/><category term="Time in JAVA"/><category term="broadening"/><category term="broadening in java"/><category term="casting in java"/><category term="executor Service"/><category term="finalize"/><category term="widening"/><category term="widening in java"/><title type='text'>Jovial Java</title><subtitle type='html'>Sharpen your Java concept with real life examples...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>53</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2851597637832159123</id><published>2011-10-08T21:28:00.000-07:00</published><updated>2011-10-08T21:28:38.466-07:00</updated><title type='text'>Part 2: Apache Velocity - Setting up the classpath</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
In this article we will learn to set up the class path for Apache velocity. This will help us to understand the code example in the upcoming articles. Basically Apache velocity comes into various flavors but we will cover only velocity engine. Only 2 external jars are required to set up the required class path. These jars are&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
1) velocity-1.6.2.jar &lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
2) velocity-1.6.2-dep.jar&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
You can download the latest jar from this location-&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
http: //velocity.apache.org/download.cgi &lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;Setting up the class path&amp;nbsp; &lt;span style=&quot;background-color: white;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;(Assuming that one is setting class path in Eclipse)&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Step 1:&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Make a new folder name &quot;classpath&quot; under your project node.&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/li&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;li&gt;&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Copy the jars in the folder.&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;background-color: white;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;/b&gt;&lt;/ul&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: white; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/b&gt;&lt;/div&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;div style=&quot;background-color: white; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;background-color: white; color: red; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt; &lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;background-color: yellow;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&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/AVvXsEibEc1Fc5-EvZ-j6m6SSuKMcxjFRS3XSJ_MXbWndutO5uoNCXDmTdoHwh2liK-Xhgd5xcdcUkK-E03iylxaZ3VUdAkXc1bP8ZG0b6Zsu4Ke04I714zgbmBPKQ17ST_qtk4Bgur65Lwqn4MY/s1600/Jars.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;285&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibEc1Fc5-EvZ-j6m6SSuKMcxjFRS3XSJ_MXbWndutO5uoNCXDmTdoHwh2liK-Xhgd5xcdcUkK-E03iylxaZ3VUdAkXc1bP8ZG0b6Zsu4Ke04I714zgbmBPKQ17ST_qtk4Bgur65Lwqn4MY/s400/Jars.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Step 2: &lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Add the jar files in your class path. &lt;/b&gt;&lt;/li&gt;
&lt;/ul&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/AVvXsEiuRJRxNcM8QlnYlHjvDDH_5GTbe6ZHF55JZpzZI92yqAwAElcZxmNk-B_uCvYQnODO7RMD4TU_3mcIHswuf6JLdPhBTQt6wj5x-YiPHjO1rcNwVQCpF_pzZBKNeDJTBwpda9_IWqLDLNGR/s1600/classpath.JPG&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;82&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuRJRxNcM8QlnYlHjvDDH_5GTbe6ZHF55JZpzZI92yqAwAElcZxmNk-B_uCvYQnODO7RMD4TU_3mcIHswuf6JLdPhBTQt6wj5x-YiPHjO1rcNwVQCpF_pzZBKNeDJTBwpda9_IWqLDLNGR/s400/classpath.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;That&#39;s all. We are ready to experience some coding example. &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/b&gt;&lt;/div&gt;
&lt;b style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;/b&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2851597637832159123/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2851597637832159123' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2851597637832159123'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2851597637832159123'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/10/part-2-apache-velocity-setting-up.html' title='Part 2: Apache Velocity - Setting up the classpath'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibEc1Fc5-EvZ-j6m6SSuKMcxjFRS3XSJ_MXbWndutO5uoNCXDmTdoHwh2liK-Xhgd5xcdcUkK-E03iylxaZ3VUdAkXc1bP8ZG0b6Zsu4Ke04I714zgbmBPKQ17ST_qtk4Bgur65Lwqn4MY/s72-c/Jars.JPG" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2008110609832564165</id><published>2011-10-08T05:38:00.000-07:00</published><updated>2011-10-08T21:29:16.941-07:00</updated><title type='text'>Part 1- Apache Velocity -  ( Introduction )</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;background-color: yellow; color: red; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b&gt;&amp;nbsp;Introduction&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
In simple terms – Apache velocity is open source software that can be used to generate dynamic content for email, sms etc. For example –One wants to write an application which will send the “password” or “account activation link”.  Normally the content of such email is fixed only certain sections such as “Password value”, “Salvation – Mr. or Ms.” change for each email. Using apache velocity, one can use a normal text file, to define the fixed content, and dynamic data, password value, to generate the email content. The Integration of text file and dynamic data is quite fast. On top of that one can define rules within the text file.  So overall one can code with in a text file.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Example of template –&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Hi  $data.get(“UserName”)&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Your password is $data.get(“password”)&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Thanks,&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Jovialjava team
&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: red; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;background-color: yellow; color: red;&quot;&gt;This is the text from Wiki&lt;/span&gt;-&lt;/b&gt;
Apache Velocity (formerly known as Jakarta Velocity) is an open source software project directed by the Apache Software Foundation. Velocity is a Java-based template engine that provides a simple yet powerful template language to reference objects defined in Java code. Its aim is to ensure clean separation between the presentation tier and business tiers in a Web application (the model–view–controller design pattern).&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2008110609832564165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2008110609832564165' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2008110609832564165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2008110609832564165'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/10/apache-velocity-part-1-introduction.html' title='Part 1- Apache Velocity -  ( Introduction )'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-586196869122129202</id><published>2011-01-31T07:41:00.000-08:00</published><updated>2011-02-03T06:30:17.803-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 13: java.util.concurrent - Atomic Variables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;In this article we will look into Atomic Variables which can help us to write lock free and wait free algorithms which were not possible prior to Java 5.0.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Two main points about Atomic Variables are&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; 1.&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 10.75pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&lt;b&gt;Help to write lock free and wait free algorithm&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt; –&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-left: .5in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-left: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Under high contention ( lots of thread are fighting for lock ), JVM spends more time with scheduling of threads, managing contention, queues of waiting threads and less time in doing the real work.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-left: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This dramatically reduces the throughput of the process.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Problem with locking:&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;1) Thread in block state cannot do anything else.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;2) If the blocked thread is high priority, then its a big disaster.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3) Can cause Dead lock&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4) Managing a Block thread is a heavy weight process, so throughput decreases.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Soon we will see how can we write lock free algorithms using atomic variables&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; margin-left: 0.25in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;2.&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;Implement very light weight process like CAS –&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;CAS (compares and swap):&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Let’s take an example to understand the concept of CAS. Suppose we have once variable “i” and we are doing some calculation over “I” and storing the result back into “i”. In a nutshell-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i = someComplicateComputation( i )&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;for “i” = 1,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; someComplicatedComputation(i) &lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;è&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt; 1234&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;In CAS Process following happens-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A memory location V will be defined.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A local variable A will be defined.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A local variable B will be defined.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;V will hold the initial value of “i”. So&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; V = i =1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;A = V = 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;B = result of that computation = 1234&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;compare ( V , A )&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;if &lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;both values are same --&amp;gt; replace V with B&#39;s value.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;else&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this means in the mean while someone has changed the value of V, so repeat the whole process again. Lets someone changes the value of “i”, hence V to 2.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; V = 2;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A = V = 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; B = result = 3246;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; compare ( V , A )&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and so on...!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This is very light weight process. This CAS technique is implemented by atomic package classes.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;Example &lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;– Lets write a simple program which first increase the number by 1, then decrease the number by 1, and then increase again by 1. So overall effect is increase the number by 1. Lets run 4 threads concurrently access the method and compare the performance of AtomicInteger Vs Integer.&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: 0in; margin-top: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre class=&quot;brush: java&quot;&gt;package com.jovialjava.blog.threads;

import java.util.concurrent.atomic.*;

public class AtomicVariableExample implements Runnable {
 AtomicInteger atomic_int_1 = new AtomicInteger();
 AtomicInteger atomic_int_2 = new AtomicInteger();
 int int_1;
 int int_2;
 private static int count = 0;

 
 public static void main(String[] args) {
  AtomicVariableExample pr = new AtomicVariableExample();
  new Thread(pr).start();// 1 0 1
  new Thread(pr).start();// 2 1 2
  new Thread(pr).start(); // 3 2 3
  new Thread(pr).start(); // 4 3 4
  while (true) {
   if (count == 4) {
    System.out.println(pr.atomic_int_1.get());
    System.out.println(pr.int_1);
    break;
   }
  }

 }

 public void run() {
  System.out.println(&quot;Inside run method...&quot;);
  doCalc();

 }

 private void doCalc() {
  try {
   atomic_int_2 = atomic_int_1;
   int_2 = int_1;
   atomic_int_2.incrementAndGet();
   int_2 = int_2 + 1;
   Thread.sleep(1000);
   atomic_int_2.decrementAndGet();
   int_2 = int_2-1;
   Thread.sleep(1000);
   atomic_int_2.incrementAndGet();
   int_2 = int_2 + 1;
   Thread.sleep(1000);
   atomic_int_1 = atomic_int_2;
   int_1 = int_2;
   synchronized (this) {
    count++;
   }
  } catch (InterruptedException e) {   
   e.printStackTrace();
  }
 }

}

&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/586196869122129202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/586196869122129202' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/586196869122129202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/586196869122129202'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-13-javautilconcurrent-atomic.html' title='Part 13: java.util.concurrent - Atomic Variables'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-537034843445754769</id><published>2011-01-28T22:31:00.000-08:00</published><updated>2011-01-28T22:31:30.604-08:00</updated><title type='text'>Part 12: java.util.concurrent : SingleThreadPool Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This article will discuss about Thread pool that uses single thread to execute tasks. From Java 5.0+ one can get such pool from Executors using following method –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;newSingleThreadExecutor()&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Example-&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Suppose we have 100 properties files in an application. We have one thread that can read properties file and return a map value. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Pseudo code –&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;READER THREAD&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Config Reader implements Callable&amp;lt;Map&amp;lt;String, String&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;try{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get the file name in the constructor of thread&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Check if File exists&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Read the file and retrun the map object&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//release all the resource&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //return null&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Main THREAD-&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get a Single thread pool from Executors&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;try{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get the list of all properties file in the directory&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a READER THREAD by passing the name of file&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store the READER thread in the a list&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//release all the thread in one go and get the Map objects&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//release all the resources&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // print the stack trace&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}finally{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//shutdown the thread pool&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 7.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre class=&quot;brush :java&quot;&gt;
package com.jovialjava.blog.threads;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


class SingleReader implements Callable  &lt;  Properties  &gt; {
   
  private String name = null; 
  public SingleReader(String name){
   this.name = name;  
  } 
  public Properties call(){
   try{
    File f = new File(name); 
    Properties prop = new Properties();
          if(f.exists() &amp;&amp; f.canRead() &amp;&amp; f.isFile()){
           FileInputStream in = new FileInputStream(f);         
           prop.load(in);          
           return prop;
          }else{
           System.err.println(&quot;Please check about this file:[&quot;+ f.getAbsolutePath()+&quot;]&quot;);
           return null;
          }   
   }catch(Exception e){
     e.printStackTrace();
     return null;
   }
  }
 }
public class SingleThreadPoolExample {
 public static String directory = &quot;config&quot;;
   private static ExecutorService executorPool=null;
       
   public static void main(String...args){
    try{
     File dir = new File(directory);
     if(dir.isDirectory()){
       List &lt; Callable &lt; Properties &gt;  &gt;  fileList = new ArrayList &lt; Callable &lt; Properties &gt;  &gt; ();
       String[] files = dir.list();
       /**
        * Optimization - Single thread executor.
        */
       executorPool = Executors.newSingleThreadExecutor();
       
      for(String file : files){
        Callable &lt; Properties &gt;  reader = new SingleReader(dir.getAbsolutePath()+ File.separator+ file);
        fileList.add(reader);      
      }
      List &lt; Future &lt; Properties &gt;  &gt;  results = executorPool.invokeAll(fileList);
      /**
       * Check how many success and how many failure
       */
      int success =0, failure = 0;
      for(Future &lt; Properties &gt;  result : results){
       if(result.get() == null){
        failure++;
       }else{
        success++;
       }
      }
      System.out.println(&quot;Total number of files [&quot;+ fileList.size()+&quot;]&quot;);
      System.out.println(&quot;Success Count [&quot;+ success+&quot;]&quot;);
      System.out.println(&quot;Failure Count [&quot;+ failure+&quot;]&quot;);
     }else{
       throw new IllegalArgumentException(&quot;There is no such directory name -&quot;+ directory);
     }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
     if(executorPool!=null){
       executorPool.shutdown();
     }
     }
   }

}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/537034843445754769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/537034843445754769' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/537034843445754769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/537034843445754769'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-12-javautilconcurrent.html' title='Part 12: java.util.concurrent : SingleThreadPool Example'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-5947620149331369735</id><published>2011-01-28T21:15:00.000-08:00</published><updated>2011-02-03T06:30:17.804-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 11: java.util.concurrent - CachedThreadPool Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This article will discuss about Thread pool that can reuse previously constructed threads when they are available. From Java 5.0+ one can get such pool from Executors using following method –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;newCachedThreadPool()&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Example-&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Suppose we have 100 properties files in an application. We have one thread that can read properties file and return a map value. We want to optimize the time to read all 100 properties file by using concurrent reading. Here optimize means – we need a perfect balance between CPU Utilization and total time consumed by reading process.&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Pseudo code –&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;READER THREAD&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Config Reader implements Callable&amp;lt;Map&amp;lt;String, String&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;try{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get the file name in the constructor of thread&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Check if File exists&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Read the file and retrun the map object&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//release all the resource&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //return null&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Main THREAD-&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get a Cached thread pool from Executors&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;try{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;// Get the list of all properties file in the directory&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a READER THREAD by passing the name of file&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store the READER thread in the a list&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//release all the thread in one go and get the Map objects&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//release all the resources&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // print the stack trace&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}finally{&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;b&gt;&lt;span style=&quot;color: #38761d; font-family: &#39;Courier New&#39;;&quot;&gt;//shutdown the thread pool&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 7.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre class=&quot;brush : java&quot;&gt;package com.jovialjava.blog.threads;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


class CachedReader implements Callable  &lt;  Properties  &gt; {
   
  private String name = null; 
  public CachedReader(String name){
   this.name = name;  
  } 
  public Properties call(){
   try{
    File f = new File(name); 
    Properties prop = new Properties();
          if(f.exists() &amp;&amp; f.canRead() &amp;&amp; f.isFile()){
           FileInputStream in = new FileInputStream(f);         
           prop.load(in);          
           return prop;
          }else{
           System.err.println(&quot;Please check about this file:[&quot;+ f.getAbsolutePath()+&quot;]&quot;);
           return null;
          }   
   }catch(Exception e){
     e.printStackTrace();
     return null;
   }
  }
 }
public class CachedThreadPoolExample {
 public static String directory = &quot;config&quot;;
   private static ExecutorService executorPool=null;
   private static int MAX_THREADS = 20;
    
   public static void main(String...args){
    try{
     File dir = new File(directory);
     if(dir.isDirectory()){
       List &lt; Callable &lt; Properties &gt;  &gt;  fileList = new ArrayList &lt; Callable &lt; Properties &gt;  &gt; ();
       String[] files = dir.list();
       /**
        * Optimization - Reuse the the threads.
        */
       executorPool = Executors.newCachedThreadPool();
       
      for(String file : files){
        Callable &lt; Properties &gt;  reader = new CachedReader(dir.getAbsolutePath()+ File.separator+ file);
        fileList.add(reader);      
      }
      List &lt; Future &lt; Properties &gt;  &gt;  results = executorPool.invokeAll(fileList);
      /**
       * Check how many success and how many failure
       */
      int success =0, failure = 0;
      for(Future &lt; Properties &gt;  result : results){
       if(result.get() == null){
        failure++;
       }else{
        success++;
       }
      }
      System.out.println(&quot;Total number of files [&quot;+ fileList.size()+&quot;]&quot;);
      System.out.println(&quot;Success Count [&quot;+ success+&quot;]&quot;);
      System.out.println(&quot;Failure Count [&quot;+ failure+&quot;]&quot;);
     }else{
       throw new IllegalArgumentException(&quot;There is no such directory name -&quot;+ directory);
     }
    }catch(Exception e){
      e.printStackTrace();
    }finally{
     if(executorPool!=null){
       executorPool.shutdown();
     }
     }
   }
}

&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/5947620149331369735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/5947620149331369735' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/5947620149331369735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/5947620149331369735'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-11-javautilconcurrent.html' title='Part 11: java.util.concurrent - CachedThreadPool Example'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2690811085170105498</id><published>2011-01-28T08:35:00.000-08:00</published><updated>2011-02-03T06:30:17.805-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 10: java.util.concurrent - ScheduledThreadPool Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This article will discuss about Thread pool that can schedule threads to run after a specified interval of time. From Java 5.0+ one can get such pool from Executors using following method –&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 18pt;&quot;&gt;public static ScheduledExecutorService&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;; font-size: 18pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 7.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;; font-size: 18pt;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; newScheduledThreadPool(int corePoolSize)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 7.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;The return type of this method (return type of thread pool) is ScheduledExecutorService.Some of the salient features of ScheduledExecutorService are –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; text-autospace: none; text-indent: -.25in;&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Schedule a Callable or Runnable to run once with a fixed delay after submission&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Schedule a Runnable to run periodically at a fixed rate&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: &#39;Times New Roman&#39;; font-weight: normal;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;S&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;chedule a Runnable to run periodically with a fixed delay between executions&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Submission returns a ScheduledFutureTask handle which can be used to cancel the task&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&amp;nbsp;.Like Timer, but supports pooling&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;Example&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;Suppose we are building a Bank check processing system. Here is the process –&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;&lt;span style=&quot;font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;Every local branch collect cheques and create a txt file contain cheque info.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;&lt;span style=&quot;font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;A service runs which copy the cheque txt file from local branch to main server.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;&lt;span style=&quot;font-family: Wingdings;&quot;&gt;&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;A Local service runs on server which check if any file has received and notify the cheque&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;Clearing process.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;We will try to make “Local Service”, which check the file reception and “Copying process” Which copy file from client machine to Server.&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;Pseudo Code-&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//Define Local &amp;amp; Copying service running interval time in Seconds&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//Make an ScheduledThreadPool with pool size 2&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;// Make Local Service thread&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Make Copying process thread&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Scheduled Both thread to run at regular interval&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//release all resources&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;LocalService Thread-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//Check if directory exists&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Check if any file exists in directory&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //return status&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//Print necessary exception&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Copying Process Thread-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;// Check if File existin on remote server&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Copy the file to main server&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//Print necessary exception&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-layout-grid-align: none; text-autospace: none;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;pre class=&quot;brush : java&quot;&gt;package com.jovialjava.blog.threads;

import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


//LOCAL SERVICE THREAD
class LocalService implements Runnable{
 
 private String DIRECTORY = null;
 
 public LocalService(String DIRECTORY){
  this.DIRECTORY = DIRECTORY;  
 } 
 public void run(){
  try{
   File dir = new File(this.DIRECTORY);
   if(dir.isDirectory()){
    if(dir.list().length &amp;gt; 0){
     System.out.println(&quot;FILE EXISTS&quot;);
    }
   }else{
    System.err.println(&quot;NO SUCH DIRECTORY [&quot;+dir.getAbsolutePath()+&quot;] exists&quot;);    
   }      
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

//COPYING SERVICE THREAD
class CopyService implements Runnable{
 
 private String REMOTE_DIR = null;
 private String LOCAL_DIR = null;
 
 public CopyService(String remoteDir, String localDir){
  this.REMOTE_DIR = remoteDir;
  this.LOCAL_DIR = localDir;
 } 
 public void run(){
  try{
   File remote = new File(this.REMOTE_DIR);
   File local = new File(this.LOCAL_DIR);
   if(remote.isDirectory() &amp;amp;&amp;amp; local.isDirectory()){
    if(remote.list().length &amp;gt; 0){
     System.out.println(&quot;REMOTE FILE FOUND, COPYING&quot;);
     //--- Call the file copying method.     
    }else{
     System.out.println(&quot;NO REMOTE FILE FOUND&quot;);     
    }    
   }else{
    System.err.println(&quot;PLEASE CHECK DIRECTORY [&quot;+remote.getAbsolutePath()+&quot; OR/AND&quot;
      +local.getAbsolutePath()+&quot;] existence&quot;);    
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
}

public class ScheduledExample {

 private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
 private static final int LOCAL_INTERVAL = 5, COPY_INTERVAL= 2;
 private static final String REMOTE_DIR=&quot;REMOTE&quot;, LOCAL_DIR= &quot;LOCAL&quot;;
 
 public static void main(String... args){
  Runnable localService = new LocalService(LOCAL_DIR);
  Runnable remoteService = new CopyService(REMOTE_DIR, LOCAL_DIR);
  try{
   executor.scheduleWithFixedDelay(localService, 0, LOCAL_INTERVAL, TimeUnit.SECONDS);
   executor.scheduleWithFixedDelay(remoteService,0, COPY_INTERVAL, TimeUnit.SECONDS);   
  }catch(Exception e){
   e.printStackTrace();
  }
 } 
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2690811085170105498/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2690811085170105498' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2690811085170105498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2690811085170105498'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-10-javautilconcurrent.html' title='Part 10: java.util.concurrent - ScheduledThreadPool Example'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-6299488281181865971</id><published>2011-01-27T07:38:00.000-08:00</published><updated>2011-01-27T07:38:17.178-08:00</updated><title type='text'>Part 9: java.util.concurrent : FixedThreadPool Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;This article will discuss about Thread pool with fixed number of thread. From Java 5.0+ one can get such pool from Executors using following method –&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Courier New&amp;quot;; font-size: 18.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;/b&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;; font-size: 18.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 7.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in; mso-line-height-alt: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;; font-size: 18.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; newFixedThreadPool(int nThreads)&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 7.5pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&lt;b&gt;Example-&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Suppose we have 100 properties files in an application. We have one thread that can read properties file and return a map value. We want to optimize the time to read all 10 properties file by using concurrent reading. Here optimize means – we need a perfect balance between CPU Utilization and total time consumed by reading process.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Pseudo code –&amp;nbsp;&lt;/span&gt;READER THREAD&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Config Reader implements Callable&amp;lt;Map&amp;lt;String, String&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt; &amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;// Get the file name in the constructor of thread&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Check if File exists&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Read the file and retrun the map object&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//release all the resource &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //return null&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Main THREAD-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;// Get a fixed thread pool from Executors&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;// Get the list of all properties file in the directory&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a READER THREAD by passing the name of file&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // store the READER thread in the a list&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;//release all the thread in one go and get the Map objects&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}catch(Exception e){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//release all the resources&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // print the stack trace&lt;/span&gt;&lt;/b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}finally{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;//shutdown the thread pool&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: xx-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;color: #333333; line-height: 10.75pt; margin-bottom: 0.0001pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;pre class=&quot;brush: java&quot;&gt;package com.jovialjava.blog.threads;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


class Reader implements Callable  &lt;  Properties  &gt; {
 
 private String name = null; 
 public Reader(String name){
  this.name = name;  
 } 
 public Properties call(){
  try{
   File f = new File(name); 
   Properties prop = new Properties();
         if(f.exists() &amp;&amp; f.canRead() &amp;&amp; f.isFile()){
         FileInputStream in = new FileInputStream(f);         
            prop.load(in);          
            return prop;
         }else{
          System.err.println(&quot;Please check about this file:[&quot;+ f.getAbsolutePath()+&quot;]&quot;);
          return null;
         }   
  }catch(Exception e){
   e.printStackTrace();
   return null;
  }
 }
}

public class FixedThreadPoolExample {
  public static String directory = &quot;config&quot;;
  private static ExecutorService executorPool=null;
  
  public static void main(String...args){
   try{
    File dir = new File(directory);
    if(dir.isDirectory()){
     List &lt; Callable &lt; Properties &gt;  &gt;  fileList = new ArrayList &lt; Callable &lt; Properties &gt;  &gt; ();
     String[] files = dir.list();
     /**
      * Optimization - just 20% of number of files.
      */
     executorPool = Executors.newFixedThreadPool(files.length/5);
     
     for(String file : files){
      Callable &lt; Properties &gt;  reader = new Reader(dir.getAbsolutePath()+ File.separator+ file);
      fileList.add(reader);      
     }
     List &lt; Future &lt; Properties &gt;  &gt;  results = executorPool.invokeAll(fileList);
     /**
      * Check how many success and how many failure
      */
     int success =0, failure = 0;
     for(Future &lt; Properties &gt;  result : results){
      if(result.get() == null){
       failure++;
      }else{
       success++;
      }
     }
     System.out.println(&quot;Total number of files [&quot;+ fileList.size()+&quot;]&quot;);
     System.out.println(&quot;Success Count [&quot;+ success+&quot;]&quot;);
     System.out.println(&quot;Failure Count [&quot;+ failure+&quot;]&quot;);
    }else{
     throw new IllegalArgumentException(&quot;There is no such directory name -&quot;+ directory);
    }
   }catch(Exception e){
    e.printStackTrace();
   }finally{
    if(executorPool!=null){
     executorPool.shutdown();
    }
   }
  } 
}

&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/6299488281181865971/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/6299488281181865971' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/6299488281181865971'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/6299488281181865971'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-9-javautilconcurrent.html' title='Part 9: java.util.concurrent : FixedThreadPool Example'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-654041970851586998</id><published>2011-01-26T00:05:00.000-08:00</published><updated>2011-02-03T06:30:17.807-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 8: java.util.concurrent - &quot;Executors&quot; Factory Threads Pool</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;Today in this article we will see some of threads pool available in &lt;i&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599; color: #cc0000; font-size: large;&quot;&gt;&lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html&quot;&gt;“Executors”&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/i&gt; Factory -&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; newFixedThreadPool(int nThreads)&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;public static ScheduledExecutorService&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newScheduledThreadPool(int corePoolSize)&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt; newCachedThreadPool()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt; newSingleThreadExecutor()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;public static ExecutorService&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt; newSingleThreadExecutor()&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;script src=&quot;http://connect.facebook.net/en_US/all.js#xfbml=1&quot;&gt;
&lt;/script&gt;&lt;fb:like href=&quot;http://jovialjava.blogspot.com/2011/01/part-8-javautilconcurrent-executors.html&quot; show_faces=&quot;false&quot; width=&quot;450&quot;&gt;&lt;/fb:like&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/654041970851586998/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/654041970851586998' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/654041970851586998'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/654041970851586998'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-8-javautilconcurrent-executors.html' title='Part 8: java.util.concurrent - &quot;Executors&quot; Factory Threads Pool'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2523220161988592870</id><published>2011-01-25T00:07:00.000-08:00</published><updated>2011-02-03T06:30:17.808-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 7: java.util.concurrent - invokeAll via ExecutorService</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-align: center;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;; font-size: large;&quot;&gt;Submit Collection of Threads via ExecutorService&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 10pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;script src=&quot;http://connect.facebook.net/en_US/all.js#xfbml=1&quot;&gt;
&lt;/script&gt;&lt;fb:like href=&quot;http://jovialjava.blogspot.com/2011/01/part-7-javautilconcurrent-invokeall-via.html&quot; show_faces=&quot;false&quot; width=&quot;450&quot;&gt;&lt;/fb:like&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Today in next part of the series we will talk about How we can release multiple threads via Executor Interface.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;As per JAVA 6.0 docs –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;lt;T&amp;gt; &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/List.html&quot; title=&quot;interface in java.util&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;List&lt;/span&gt;&lt;/a&gt;&amp;lt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-2-javautilconcurrent-future.html&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;Future&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/a&gt;&amp;gt; &lt;b&gt;invokeAll&lt;/b&gt;(&lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Collection.html&quot; title=&quot;interface in java.util&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;Collection&lt;/span&gt;&lt;/a&gt;&amp;lt;? extends &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Callable.html&quot; title=&quot;interface in java.util.concurrent&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;Callable&lt;/span&gt;&lt;/a&gt;&amp;lt;T&amp;gt;&amp;gt;&amp;nbsp;tasks)&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throws &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/lang/InterruptedException.html&quot; title=&quot;class in java.lang&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;InterruptedException&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #00b050; font-family: &#39;Courier New&#39;;&quot;&gt;In traditional Java&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;– If we have to release multiple threads- we have to create Thread objects and call &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Start method one by one.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: #00b050; font-family: &#39;Courier New&#39;;&quot;&gt;In Java 5.0 and above&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt; – If we can put all callable objects in a collection object and pass the collection objects to ExecutorService to release.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;For example – If we have 1000 thread object to be released, we can create an array list and an ExecutorService object with thread pool size of 20.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create an ExecutorService object with thread pool size = 20&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create an array List with 1000 callable objects.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Release all threads by ExecutorService.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;u&gt;&lt;b&gt;Some important points to note here are &lt;/b&gt;&lt;/u&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-list: l0 level1 lfo1; text-indent: -.25in;&quot;&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;As ExecutorService has thread pool of 20, it will internally manage queuing and releasing of 1000 threads.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;i&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;invokeAll&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt; is a blocking method. It means – JVM won’t proceed to next line until all the 1000 threads are complete.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;All the 1000 threads must have same Return Type &amp;lt; T &amp;gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;i&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html#isDone()&quot;&gt;&lt;span style=&quot;color: #333333; text-decoration: none;&quot;&gt;Future.isDone()&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;is&amp;nbsp;true&amp;nbsp;for each element of the returned list&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;A&amp;nbsp;&lt;i&gt;completed&lt;/i&gt;&amp;nbsp;thread could have terminated either normally or by throwing an exception. In both the case return type of Future.isDone( ) is true.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;The results of this method are undefined if the given collection is modified while this operation is in progress&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #333333; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;It can throw following exceptions –&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #0033cc; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;color: #0033cc; text-decoration: none;&quot;&gt;InterruptedException&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;- if interrupted while waiting, in which case unfinished tasks are cancelled.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #0033cc; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;color: #0033cc; text-decoration: none;&quot;&gt;NullPointerException&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;- if tasks or any of its elements are&amp;nbsp;null.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #0033cc; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;span style=&quot;color: #0033cc; text-decoration: none;&quot;&gt;RejectedExecutionException&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;line-height: normal;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;- if any task cannot be scheduled for execution.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;EXAMPLE –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Imagine a scenario, one needs to write create a txn file of each request coming to system. Following are the requirements-&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; mso-add-space: auto; mso-list: l0 level1 lfo1; text-indent: -.25in;&quot;&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;color: #cc0000; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;File name will be the client name.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #cc0000; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;At a time, almost 1000 clients could be connected at a time, so system must be capable of creating 1000 files in one go.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #cc0000; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Process of creating one file should be less than 5 ms.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #cc0000; font-family: Wingdings;&quot;&gt;Ø&lt;span style=&quot;font: normal normal normal 7pt/normal &#39;Times New Roman&#39;;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Total time of creating all the files should be less than 300 ms.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;;&quot;&gt;Let’s write the pseudo code for this case –&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-align: center;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;File creation Thread – implements CALLABLE&amp;lt;Boolean&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// GET the File name and Raw Data in Constructor&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;Try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Create a File Object&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Create a print Writer Object&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Prepare the data and Write in the File&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Close the print Writer and File Object&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Return TRUE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;} catch Exception {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;return FALSE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt; text-align: center;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: 0.0001pt; text-align: center;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&lt;o:p&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: black; font-family: &#39;Times New Roman&#39;;&quot;&gt;&lt;b&gt;&lt;span style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;;&quot;&gt;Main Parent Thread&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;// -- In the Loop for whole Client list ---&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;try{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create a file creator thread – Pass filename and raw Data&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// add the thread object into an Array List&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Pass the array list to Executor Service interface and invokeAll.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// &lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-2-javautilconcurrent-future.html&quot;&gt;Loop in the Future&amp;lt;Boolean&amp;gt;&lt;/a&gt; and check how many threads completed successfully.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp;}catch Exception{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Take necessary action&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;}finally{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: 10.75pt; margin-bottom: .0001pt; margin-bottom: 0in; text-indent: .5in;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;// Shut down the ExecutorService&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: Arial, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Times New Roman&#39;, serif;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 7.5pt; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 7.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 7.5pt; line-height: 115%;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;color: #333333; font-family: &#39;Courier New&#39;; font-size: 7.5pt; line-height: 115%;&quot;&gt;&lt;br style=&quot;mso-special-character: line-break;&quot; /&gt; &lt;br style=&quot;mso-special-character: line-break;&quot; /&gt; &lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush : java&quot;&gt;package com.jovialjava.blog.threads;

import java.io.File;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.*;


// FILE CREATOR THREAD
class FileTask implements Callable&amp;lt; Boolean &amp;gt;{
 
 private String name = null;
 private String data = null;
 public FileTask(String name, String data){
  this.name = name;
  this.data = data;
 }
 
 public Boolean call(){
  try{
   File file = new File(this.name);
   PrintWriter writer = new PrintWriter(file);
   writer.write(this.data);
   writer.close();
   return true;   
  }catch(Exception e){
   return false;
  }
 }
}

// MAIN THREAD
public class InvokeAllExample {
 
 private static final ExecutorService executorPool=Executors.newFixedThreadPool(20);
 private static final int NO_OF_CLIENT = 1000;
 private static final String FILE_EXT = &quot;.txt&quot;;
 private static String TXN_DATA = &quot;SOME RANDOM TXN DATA FOR CLIENT --&amp;gt; &quot;;
 private static String DIRECTORY = &quot;EXAMPLE&quot; + File.separator;
 
 static{
  if(!new File(DIRECTORY).isDirectory()){
   new File(DIRECTORY).mkdir();
  }
 }
 
 public static void main(String[] args) {
  int success = 0;
  int failure = 0;
  /**
   * Lets assume we have 1000 clients connected and sending request at a time.
   */
  Collection&lt;filetask&gt; collection = new ArrayList&lt;filetask&gt;( );
  for(int i=0; i&amp;lt; NO_OF_CLIENT; i++){
   FileTask task = new FileTask(DIRECTORY+ Integer.toString(i)+ FILE_EXT, TXN_DATA+ i);
   collection.add(task);
  }
  long startTime = new Date().getTime();
  try {   
   List&amp;lt; Future&amp;lt; Boolean &amp;gt; &amp;gt; list = executorPool.invokeAll(collection);   
    for(Future&amp;lt; Boolean &amp;gt; fut : list){
     int ignore = fut.get()? success++ : failure++;
    }
   
   } catch (Exception e) {
     e.printStackTrace();
   }finally{
     System.out.println(&quot;TOTAL SUCCESS - &quot;+ success);
     System.out.println(&quot;TOTAL FAILURE - &quot;+ failure);
     System.out.println(&quot;Total time - &quot; + (new Date().getTime() - startTime) + &quot; ms&quot;);
     executorPool.shutdown();
   }
 } // End of Main

}
&lt;/filetask&gt;&lt;/filetask&gt;&lt;/pre&gt;&lt;/div&gt;&lt;script src=&quot;http://connect.facebook.net/en_US/all.js#xfbml=1&quot;&gt;
&lt;/script&gt;&lt;fb:like href=&quot;http://jovialjava.blogspot.com/2011/01/part-7-javautilconcurrent-invokeall-via.html&quot; show_faces=&quot;false&quot; width=&quot;450&quot;&gt;&lt;/fb:like&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2523220161988592870/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2523220161988592870' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2523220161988592870'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2523220161988592870'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-7-javautilconcurrent-invokeall-via.html' title='Part 7: java.util.concurrent - invokeAll via ExecutorService'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-4216668977594688574</id><published>2011-01-22T10:21:00.000-08:00</published><updated>2011-02-03T06:43:59.988-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Condition"/><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Lock"/><category scheme="http://www.blogger.com/atom/ns#" term="Locks"/><title type='text'>Part 6: java.util.concurrent - Lock and Condition Object</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;script src=&quot;http://connect.facebook.net/en_US/all.js#xfbml=1&quot;&gt;
&lt;/script&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Today in next part of the series we will talk about How to communicate among threads using Lock and Condition Objects.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;In traditional Java&lt;/b&gt; - If we need to communicate among threads, we syncronize code and use &quot;wait&quot; and &quot;notify&quot; methods &amp;nbsp;of Object class.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;From Java 5.0 and above&lt;/b&gt; - &lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;Lock interface provides an easier implmentation for synchronization and Condition class can be used to wait and notify threads.&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Lock has several important methods such as &quot;lock&quot;, &quot;tryLock&quot;, &quot;lockInterruptibly&quot; etc, whereas Condition has &quot;await&quot;, &quot;signal&quot;, &#39;signalAll&quot; etc. In this article we will demonstrate the usage of 3 methods - &quot;lock&quot; (from Lock interface), &quot;await&quot;, &quot;signal&quot; (from Condition Class).&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Lets try to visualize a scenario here - Assume we have 2 process &quot;Reader&quot; and &quot;Writer&quot;. Writer writes on a file and Reader reads from a file. we want to add a listener to writer object so that whenever writer writes anything on a file, &quot;Reader&quot; will be called and it will read the same data. Before we look into codes lets look at the some important points to use Lock and Condition-&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;1) Lock is an &lt;b&gt;Interface&lt;/b&gt;, the most common implementation class is &lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;ReentrantLock&lt;/span&gt;&lt;/b&gt;. Others two are - ReentrantReadWriteLock.ReadLock, ReentrantReadWriteLock.WriteLock&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;2) &lt;b&gt;Condition Object is always retrieved from Lock object&lt;/b&gt;. For example - Condition condition = lock.newCondition( );&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;3) One of the best practice to use Lock is-&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;Lock lockObj = new ReentrantLock( );&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lockObj.lock( );&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;.... code..&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }finally{&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lockObj.unlock( );&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;4) condition.await, condition.signal, condition.signalAll methods should only be called once you have acquired the lock by - lockObj.lock( ).&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;In our example, the design of the class will be something like this -&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;gt; One lock Object == fileLock&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;gt; One condition Object = condition = fileLock.newCondition&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;Pseudo code&amp;nbsp;for Writer Thread&lt;/span&gt;&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;// GET the LOCK&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;try{&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;// -- In &amp;nbsp;the Loop untill EXIT---&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; //Write on the file&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; // Signal the READER&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; //If EXIT signal then exit else &quot;WAIT for READER to SIGNAL&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;}finally{&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;// RELEASE the LOCK&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000;&quot;&gt;pseudo code for Reader Thread&lt;/span&gt;&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;// GET the LOCK&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;try{&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;// -- In &amp;nbsp;the Loop untill EXIT---&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; //Read from the file&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; // Signal the WRITER&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; // If EXIT signal - then exit else &quot;WAIT for WRITER to SIGNAL&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;}finally{&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;// RELEASE the LOCK&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
&lt;pre class=&quot;brush : java&quot;&gt;package com.jovialjava.blog.threads;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.security.SecureRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockExample {
 
 private static final String fileName = &quot;LockExample.txt&quot;;
 private static final String EXIT_FLAG = &quot;BYE&quot;;
 private static final int NO_OF_LINES = 10;
 private static final Lock fileLock = new ReentrantLock();
 private static final Condition condition = fileLock.newCondition();
 private static final ExecutorService executorPool=Executors.newFixedThreadPool(2);
 
 public static void main(String...args){  
  Runnable fileWriter = new FileWrite();
  Runnable fileReader = new FileRead();  
  executorPool.submit(fileReader);
  executorPool.submit(fileWriter); 
  executorPool.shutdown();
 }
 
 /**
  * This thread will write on a file and inform the reader thread to
  * read it. If it has not written the EXIT flag then it will go into 
  * wait stage and will wait for READER to signal that it safe to write
  * now. 
  */
 public static class FileWrite implements Runnable{
  
  public void run( ){   
   try{
    fileLock.lock();
   for(int i=0; i&amp;lt; NO_OF_LINES; i++){    
    PrintWriter writer = new PrintWriter(new File(fileName));    
    if(i != NO_OF_LINES - 1){
     int random = new SecureRandom().nextInt();
     System.out.println(&quot;WRITER WRITING &quot; + random);
     writer.println(random);
     writer.close();
     //signallng to READER that its safe to read now.
     condition.signal();
     System.out.println(&quot;Writer waiting&quot;);
     condition.await();
    }else{
     writer.println(EXIT_FLAG);
     System.out.println(&quot;WRITER EXITING &quot;);
     writer.close();
     //AS it was an exit flag so no need to wait, just signal the reader.
     condition.signal();
    }   
   }   
   }catch(Exception e){
    System.out.println(&quot;!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!&quot;);
    e.printStackTrace();
   }finally{
    fileLock.unlock();
    // Delete the file, require in case if one wants to run demo again.
    File file = new File(fileName);
    file.delete();
    try{
     file.createNewFile();
    }catch(Exception e){}
   }
  }
 } 
 /**
  * This thread will read from the file and inform the writer thread to
  * write again. If it has not read the EXIT flag then it will go into 
  * wait stage and will wait for WRITER to signal that it safe to read
  * now. 
  */
 public static class FileRead implements Runnable{
    
  public void run( ){
   String data = null;   
   fileLock.lock();   
  try{   
   while(true){    
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    data = reader.readLine();
    System.out.println(&quot;READ DATA - &quot; + data);        
    reader.close();
    if(data == null || !data.equals(EXIT_FLAG)){
     condition.signalAll();
     System.out.println(&quot;Reader Waiting&quot;);
     condition.await();
    }else{
     System.out.println(&quot;READER EXITING&quot;);
     condition.signal();
     break;
    }    
   }
   }catch(Exception e){
    System.out.println(&quot;!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!&quot;);
    e.printStackTrace();
   }finally{
    fileLock.unlock();
   }
  }
 }
}
&lt;/pre&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;script src=&quot;http://connect.facebook.net/en_US/all.js#xfbml=1&quot;&gt;
&lt;/script&gt;&lt;fb:like href=&quot;http://jovialjava.blogspot.com/2011/01/part-6-javautilconcurrent-lock-and.html&quot; show_faces=&quot;false&quot; width=&quot;450&quot;&gt;&lt;/fb:like&gt;&amp;gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/4216668977594688574/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/4216668977594688574' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4216668977594688574'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4216668977594688574'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-6-javautilconcurrent-lock-and.html' title='Part 6: java.util.concurrent - Lock and Condition Object'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-1253917619790037652</id><published>2011-01-21T19:49:00.000-08:00</published><updated>2011-02-03T06:44:44.125-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 5: java.util.concurrent - Submit Callable&lt; T &gt; via ExecutorService</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: large;&quot;&gt;&lt;b&gt;Future&amp;lt;T&amp;gt; submit(Callable&amp;lt;T&amp;gt; c)&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Today in next part of the series we will talk about submission of callable task via executor service.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;As per JAVA version 6.0, ExecutorService Interface has following method -&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599;&quot;&gt;Future&amp;lt; T &amp;gt; &amp;nbsp;submit( callable c )&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;Submits a Callable task for execution and returns a Future representing that task computation.&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Future&amp;lt; T &amp;gt; will return T on successful&amp;nbsp;completion&amp;nbsp;of the process, otherwise result will not be T.&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Lets start with Callable task - As we &amp;nbsp;know that Callable&amp;lt;T&amp;gt; interface has following method - &lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;public T call()&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;So when we say - ExecutorService.submit(Callable Task) --&amp;gt; It starts a new stack starting with call method of callable task, as call method can return T so does future object. Due to this reason the signature of submit(Callable c) is Future&amp;lt; T &amp;gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;T --&amp;gt; means anything that extends Object.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;you can &lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot;&gt;&lt;b style=&quot;background-color: #ffe599;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;&amp;gt;&amp;gt;download&amp;lt;&amp;lt;&lt;/span&gt;&lt;/b&gt;&lt;/a&gt; the program from here.&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush : java&quot;&gt;/**
*Copyright (C) 2010  Jovial Java Admin [email: jovialjava.blogspot@gmail.com]
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, version 3 of the License.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program.  If not, see &lt;http: licenses=&quot;&quot; www.gnu.org=&quot;&quot;&gt;.
*@date Jan 22, 2011
*@author JovialJava
*/
package com.jovialjava.blog.threads;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class CallableExample {

 private static final ExecutorService executorPool=Executors.newFixedThreadPool(2);
 
 public static void main(String[] args) {
  CallableTask_1 task_1 = new CallableTask_1();
  CallableTask_2 task_2 = new CallableTask_2();
  
  /**
   * Submit the first task
   */
  Future&lt;boolean&gt; fut_1 = executorPool.submit(task_1);
  Future&lt;boolean&gt; fut_2 = executorPool.submit(task_2);
  
  try{
   if(fut_1.get()){
    System.out.println(&quot;TASK 1 completed SUCCESSFULLY&quot;);
   }
   if(fut_2.get()){
    System.out.println(&quot;TASK 2 completed SUCCESSFULLY&quot;);
   }
  }catch(ExecutionException e){   
   System.out.println(e.getMessage());
  }catch(InterruptedException e){
   System.out.println(e.getMessage());
  }finally{
   /**====VERY IMPORTANT===
    * This is required to stop the executor pool to
    * stop accepting new request.
    */
   executorPool.shutdown();
  }

 }

 
 /**
  * This task will complete successfully
  */
 public static class CallableTask_1 implements Callable&lt;boolean&gt;{
  public Boolean call()throws NullPointerException{
   System.out.println(&quot;Hi, Inside Callable Task 1&quot;);
   return true;
  }  

 }
 
 /**
  * This task will result in error.
  */
 public static class CallableTask_2 implements Callable&lt;boolean&gt;{
  public Boolean call()throws NullPointerException{
   System.out.println(&quot;Hi, Inside Callable Task 2&quot;);
   throw new IllegalStateException(&quot;Callable Task Exception&quot;);
  }  

 }
}
&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/http:&gt;&lt;/pre&gt;&lt;http: licenses=&quot;&quot; www.gnu.org=&quot;&quot;&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;br /&gt;
&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/http:&gt;&lt;http: licenses=&quot;&quot; www.gnu.org=&quot;&quot;&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;boolean&gt;&lt;a href=&quot;http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601?ie=UTF8&amp;amp;tag=widgetsamazon-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Multithreading in JAVA&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;1&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=widgetsamazon-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321349601&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;.&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/boolean&gt;&lt;/http:&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/1253917619790037652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/1253917619790037652' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1253917619790037652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1253917619790037652'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-5-javautilconcurrent-submission-of.html' title='Part 5: java.util.concurrent - Submit Callable&lt; T &gt; via ExecutorService'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-7379815028372552853</id><published>2011-01-21T08:17:00.000-08:00</published><updated>2011-02-03T06:45:03.212-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 4: java.util.concurrent - Submit Runnable via ExecutorService</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Future&amp;lt;?&amp;gt; submit( Runnable r )&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Today in part 4 of the series we will talk about submission of runnable task via executor service. This method can be very&amp;nbsp;beneficial&amp;nbsp;if someone wants to upgrade legacy code from 1.4 to 1.5 onwards.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;As per JAVA version 6.0, ExecutorService Interface has following method -&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Future&amp;lt; ? &amp;gt; &amp;nbsp;submit( Runnable r )&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;nbsp;&lt;b&gt;Submits a Runnable task for execution and returns a Future representing that task.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;gt;&amp;nbsp;This method will return NULL on successful completeion of the process, otherwise result will not be null&lt;/b&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Lets start with Runnable task - As we &amp;nbsp;know that Runnable interface has following method -&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;public void run()&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;So when we say - ExecutorService.submit(Runnable Task) --&amp;gt; It starts a new stack starting with run method of runnable task, but as run method can not return any value&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;so does future object. Due to this reason the signature of submit(Runnable r) is Future&amp;lt; ? &amp;gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;? --&amp;gt; means anything that extends Object, has no lower limit&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;pre class=&quot;brush: java&quot;&gt;/**
*Copyright (C) 2010  Jovial Java Admin [email: jovialjava.blogspot@gmail.com]
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, version 3 of the License.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
*@date Jan 21, 2011
*@author JovialJava
*/
package com.jovialjava.blog.threads;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class RunnableExample {
 
 private static final ExecutorService executorPool=Executors.newFixedThreadPool(2);
 
 public static void main(String[] args) {
  RunnableTask_1 task_1 = new RunnableTask_1();
  RunnableTask_2 task_2 = new RunnableTask_2();
  
  /**
   * Submit the first task
   */
  Future&lt; ? &gt; fut_1 = executorPool.submit(task_1);
  Future&lt; ? &gt; fut_2 = executorPool.submit(task_2);
  
  try{
   if(fut_1.get() == null){
    System.out.println(&quot;TASK 1 completed SUCCESSFULLY&quot;);
     }
   if(fut_2.get() == null){
    System.out.println(&quot;TASK 2 completed SUCCESSFULLY&quot;);
     }
  }catch(ExecutionException e){   
   System.out.println(e.getMessage());
  }catch(InterruptedException e){
   System.out.println(e.getMessage());
  }finally{
   /**====VERY IMPORTANT===
    * This is required to stop the executor pool to
    * stop accepting new request.
    */
   executorPool.shutdown();
  }

 }
 /**
  * This task will complete successfully
  */
 public static class RunnableTask_1 implements Runnable{
  public void run()throws NullPointerException{
   System.out.println(&quot;Hi, Inside Runnable Task 1&quot;);   
  }  

 }
 /**
  * This task will result in error.
  */
 public static class RunnableTask_2 implements Runnable{
  public void run()throws NullPointerException{
   System.out.println(&quot;Hi, Inside Runnable Task 2&quot;);
   throw new IllegalStateException(&quot;Runnable Task Exception&quot;);
  }  

 }
}
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/7379815028372552853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/7379815028372552853' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/7379815028372552853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/7379815028372552853'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-4-javautilconcurrent-submit.html' title='Part 4: java.util.concurrent - Submit Runnable via ExecutorService'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-1020982798059242286</id><published>2011-01-20T07:06:00.000-08:00</published><updated>2011-07-27T05:05:48.455-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="executor Service"/><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><title type='text'>Part 3:java.util.concurrent- ExecutorService Interface</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;xecutorService interface provide us a base to release threads. It maintains everything related to threads.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;Analogy&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;This is very much similar to a small IT organization where a project manager handles all activities related to developers. Activities such as -&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;how many person will be there in each team, if one team has less work then releasing some person from that team and shifting to&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;another team, generate weekly report to submit to company etc.&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;If we see deeply -&lt;b&gt;functions of ExecutorService&lt;/b&gt; are nothing but an exact replica of &lt;b&gt;what a project manager does in a company&lt;/b&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;We create thread task , ExecutorService helps to release a thread and give the result back to us&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;similarly company hires a person and project manager takes out the work from that person and company gets benefited.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;,Courier,monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Threads Pool in ExecutorService Implementation:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-size: large;&quot;&gt;Analogy&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;This is very much similar to big organizations in which a project manager have project leaders working under him to take care of developers. so project leaders are nothing but acting as thread POOL.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;,Courier,monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Fixed Thread POOL:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;,Courier,monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Advantage:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;An important advantage of the fixed thread pool is that applications using it degrade gracefully. &lt;b&gt;To understand this, consider a web server application where each HTTP request is handled by a separate thread. If the application simply creates a new thread for every new HTTP request, and the system receives more requests than it can handle immediately, the application will suddenly stop responding to all requests when the overhead of all those threads exceed the capacity of the system. With a limit on the number of the threads that can be created, the application will not be servicing HTTP requests as quickly as they come in, but it will be servicing them as quickly as the system can sustain.&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;,Courier,monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Example-&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Suppose there are 8 threads in the Thread Pool.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;First request comes in the queue : check if there is any Thread in the pool --&amp;gt; Yes , execute the task.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;9th request comes &amp;nbsp;in : No thread in pool currently --&amp;gt; Hold the task in the queue( Degrading the performance) --&amp;gt; wait until one among the currently running 8 task finishes --&amp;gt; as soon as one finished, run the ninth task ( graceful work)&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffd966; color: #cc0000;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;ExecutorServiceInterface -&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;Important Methods&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;Provide &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;submit(…)&lt;/span&gt; in spite of &lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: white;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;execute(…)&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;submit( Runnable &amp;nbsp; &amp;nbsp;r)&lt;/span&gt; --&amp;gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;return Future&amp;lt; ? &amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;submit( Callable&amp;lt;V&amp;gt; c)&lt;/span&gt; --&amp;gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;return Future&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;invokeAll( Collection&amp;lt; ? extends Callable&amp;lt;T&amp;gt;&amp;gt; task )&lt;/span&gt; --&amp;gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;List&amp;lt;Future&amp;lt;T&amp;gt;&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;shutdownNow( )&lt;/span&gt; --&amp;gt; &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d;&quot;&gt;List&amp;lt;Runnable&amp;gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/1020982798059242286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/1020982798059242286' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1020982798059242286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1020982798059242286'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-3javautilconcurrent.html' title='Part 3:java.util.concurrent- ExecutorService Interface'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-274671402483342444</id><published>2011-01-19T02:56:00.000-08:00</published><updated>2011-02-03T06:30:17.815-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Future"/><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 2: java.util.concurrent - Future &amp;lt;T&amp;gt;</title><content type='html'>&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;, Courier, monospace; font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Future&amp;lt;T&amp;gt;:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;A Future represents the result of an asynchronous computation. Future has been provide with some really powerful methods. For Instance -&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;1) We can check whether a task has been completed or not.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;2) We can cancel a task.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;3) Check whether task was cancelled or complete normally.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #cc0000; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;u&gt;Methods-&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffe599; color: #38761d;&quot;&gt;boolean cancel(boolean mayInterruptIfRunning)&lt;/span&gt;&lt;/b&gt; -&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;This method does following -&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;1) If the process(Thread) has not started, then cancel the thread.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;2) If the process has started, then check if&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;mayInterruptIfRunning = true ==&amp;gt; Inturrept the thread and cancel it&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;mayInterruptIfRunning = false ==&amp;gt; Let it run&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;After this method returns, subsequent calls to isDone() will always return true.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Subsequent calls to isCancelled() will always return true if this method returned true.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b style=&quot;background-color: #ffe599;&quot;&gt;boolean isCancelled()&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Returns true if this task was cancelled before it completed normally&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b style=&quot;background-color: #ffe599;&quot;&gt;V get() throws InterruptedException, ExecutionException&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;This method is a blocking call. It will cause JVM to wait if necessary for the computation to complete, and then retrieves its result.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #38761d; font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;b style=&quot;background-color: #ffe599;&quot;&gt;boolean isDone()&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation - in all of these cases, this method will return true.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Please &lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;background-color: #ffd966; color: #274e13;&quot;&gt;&amp;gt;&amp;gt;download&amp;lt;&amp;lt;&lt;/span&gt;&lt;/b&gt;&lt;/a&gt; the program from here.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush : java&quot;&gt;/**
*Copyright (C) 2010  Jovial Java Admin [email: jovialjava.blogspot@gmail.com]
*
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, version 3 of the License.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program.  If not, see &lt;http: licenses=&quot;&quot; www.gnu.org=&quot;&quot;&gt;.
*@date Jan 19, 2011
*@author JovialJava
*/
package com.jovialjava.blog.threads;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureExample {
 /**
  * Executor pool is a new concept in JAVA 5+, it can help to release 
  * a thread. It maintains a pool of threads, It reuses them, also align them in 
  * a queue. Here we are creating pool of 1 thread.
  */
 private static final ExecutorService executorPool=Executors.newFixedThreadPool(5);
 
 public static void main(String... args){
  CallableTask call = new CallableTask();  
  try{
   /**
    * Future&lt;t&gt; is result of asynchronous computation.
    * It may possible that CallableTask has not started yet
    * but &#39;ExecutorService&#39; gives the result via Future
    * Object.
    */
   Future&lt;string&gt; future = executorPool.submit(call);
   /**
    * We can check if CallableTask has been completed.
    */
   System.out.println(&quot;Status of Callable Task [Is Completed ? &quot;+ future.isDone()+ &quot;]&quot;);
   /**
    * We can get the result of callable Task.
    * Note : future.get() is a blocking call, It will wait until the associated process finishes.
    */
   System.out.println(&quot;Result of callable task [&quot;+ future.get()+&quot;]&quot;);
   /**
    * We can cancel the task.
    */
   System.out.println(&quot;Trying to cancel the task [Is Cancelled ? &quot;+ future.cancel(false)+ &quot;]&quot;);
   /**
    * We can see if the task was canceled.
    * Returns true if this task was canceled before it completed normally
    */
   System.out.println(&quot;Was task canceled before normal complition ? -&quot;+ future.isCancelled());
   
   
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   executorPool.shutdownNow();
  }
  
 }

 
 /*
  *Callable&lt;v&gt; is defined as Callable&lt;string&gt;
  *
  */
 public static class CallableTask implements Callable&lt;string&gt;{
    
  public String call( ){
   System.out.println(&quot;Inside CALLABLE TASK&quot;);
   return &quot;~~JOVIAL JAVA~~&quot;;
  }
 } 
}

&lt;/string&gt;&lt;/string&gt;&lt;/v&gt;&lt;/string&gt;&lt;/t&gt;&lt;/http:&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/274671402483342444/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/274671402483342444' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/274671402483342444'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/274671402483342444'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/part-2-javautilconcurrent-future.html' title='Part 2: java.util.concurrent - Future &amp;lt;T&amp;gt;'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2369022537569381233</id><published>2011-01-17T06:57:00.000-08:00</published><updated>2011-02-03T06:29:51.604-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Callable"/><category scheme="http://www.blogger.com/atom/ns#" term="Java.util.Concurrent"/><category scheme="http://www.blogger.com/atom/ns#" term="Runnable"/><category scheme="http://www.blogger.com/atom/ns#" term="Runnable Vs Callable"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads in JAVA"/><title type='text'>Part 1: java.util.concurrent - Runnable Vs Callable in Java</title><content type='html'>&lt;div style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;About Runnable:&lt;/b&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Runnable interface is implemented by the Thread class as well and it&#39;s a common protocol for all the objects who wish to execute in a different thread. It&#39;s one of the ways of creating threads in Java. The other way to create a thread is by subclassing the Thread class. A class implementing Runnable interface can simply pass itself to create a Thread instance and can run thereafter. This eliminates the need of subclassing the Thread class for the purpose of executing the code in a separate thread.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;As long as we don&#39;t wish to override other methods of the Thread class, it may be a better idea to implement the Runnable interface to enable multithreading capabilities to a class than enabling the same by extending the Thread class.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;About Callable:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;The designers of Java felt a need of extending the capabilities of the Runnable interface, but they didn&#39;t want to affect the uses of the Runnable interface and probably that was the reason why they went for having a separate interface named Callable in Java 1.5 than changing the already existing Runnable interface which has been a part of Java since Java 1.0.&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #cc0000; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;u&gt;&lt;b&gt;Similarities:&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;1) Both threads can be used to cause a separate stack for a thread. &lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;2) Both has only one method : Inside Runnable it is called as &lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public abstract void run( );&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Inside callable the method is being called as &lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;public abstract &lt;v&gt; call() ;&lt;br /&gt;
(Note : V can be any valid object in JAVA)&lt;br /&gt;
&lt;br /&gt;
&lt;u style=&quot;color: #cc0000;&quot;&gt;&lt;b&gt;Differences:&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
1) Return Type -&lt;br /&gt;
public abstract void run()  --&amp;gt; Return type &quot;void&quot;&lt;br /&gt;
public abstract &lt;v&gt; call()  --&amp;gt; Return type &quot;Any valid JAVA Object&quot;&lt;br /&gt;
&lt;br /&gt;
2) Run method can not throw any checked Exception, whereas&lt;br /&gt;
public abstract &lt;v&gt; call() throw checkedException&lt;br /&gt;
Call method can throw any checked Exception.&lt;/v&gt;&lt;/v&gt;&lt;/v&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
Note: You can also &lt;b style=&quot;background-color: #ffe599; color: #38761d;&quot;&gt;&lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot;&gt;&amp;gt;&amp;gt; DOWNLOAD &amp;lt;&amp;lt;&lt;/a&gt;&lt;/b&gt; the program from here.&lt;/div&gt;&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNi8O9k79opsVNyq9fZqTOFBsgJBVKPFRia3huvKqzee_KGYrEcAiKP20Ku9hkomcVTvalxCP4BFQTL5dS4kWNFFoM-39cpPMon_D-M-WgqVJKrLg2ikdHGlHgQx72QR8FVoX9DJxUSirk/s1600/thread1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNi8O9k79opsVNyq9fZqTOFBsgJBVKPFRia3huvKqzee_KGYrEcAiKP20Ku9hkomcVTvalxCP4BFQTL5dS4kWNFFoM-39cpPMon_D-M-WgqVJKrLg2ikdHGlHgQx72QR8FVoX9DJxUSirk/s1600/thread1.JPG&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Copyright (c) 2011 http://www.jovialjava.blogspot.com&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/td&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2369022537569381233/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2369022537569381233' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2369022537569381233'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2369022537569381233'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/runnable-vs-callable-in-java.html' title='Part 1: java.util.concurrent - Runnable Vs Callable in Java'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNi8O9k79opsVNyq9fZqTOFBsgJBVKPFRia3huvKqzee_KGYrEcAiKP20Ku9hkomcVTvalxCP4BFQTL5dS4kWNFFoM-39cpPMon_D-M-WgqVJKrLg2ikdHGlHgQx72QR8FVoX9DJxUSirk/s72-c/thread1.JPG" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-1373265046701808091</id><published>2011-01-16T00:02:00.000-08:00</published><updated>2011-02-07T00:13:39.874-08:00</updated><title type='text'>java.util.concurrent tutorial Index</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Hi Everyone,&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;We are extremely happy to present a tutorial covering most of the important topics in java.util.concurrent package. Below is the index of the tutorial-&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/runnable-vs-callable-in-java.html&quot;&gt;Part 1: java.util.concurrent - Runnable Vs Callable in Java&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-2-javautilconcurrent-future.html&quot;&gt;Part 2: java.util.concurrent - Future &amp;lt;T&amp;gt;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-3javautilconcurrent.html&quot;&gt;Part 3: java.util.concurrent- ExecutorService Interface&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-4-javautilconcurrent-submit.html&quot;&gt;Part 4: java.util.concurrent - Submit Runnable via ExecutorService&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-5-javautilconcurrent-submission-of.html&quot;&gt;Part 5: java.util.concurrent - Submit Callable&amp;lt; T &amp;gt; via ExecutorService&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-6-javautilconcurrent-lock-and.html&quot;&gt;Part 6: java.util.concurrent - Lock and Condition Object&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-7-javautilconcurrent-invokeall-via.html&quot;&gt;Part 7: java.util.concurrent - invokeAll via ExecutorService&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-8-javautilconcurrent-executors.html&quot;&gt;Part 8: java.util.concurrent - &quot;Executors&quot; Factory Threads Pool&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-9-javautilconcurrent.html&quot;&gt;Part 9: java.util.concurrent : FixedThreadPool Example&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-10-javautilconcurrent.html&quot;&gt;Part 10: java.util.concurrent - ScheduledThreadPool Example&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-11-javautilconcurrent.html&quot;&gt;Part 11: java.util.concurrent - CachedThreadPool Example&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-12-javautilconcurrent.html&quot;&gt;Part 12: java.util.concurrent : SingleThreadPool Example&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/part-13-javautilconcurrent-atomic.html&quot;&gt;Part 13: java.util.concurrent - Atomic Variables&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;All valuable comments are welcome. :)&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/1373265046701808091/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/1373265046701808091' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1373265046701808091'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1373265046701808091'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/javautilconcurrent-tutorial-index.html' title='java.util.concurrent tutorial Index'/><author><name>Jovial Java Admin</name><uri>http://www.blogger.com/profile/16192057567889773393</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-631729745733615313</id><published>2011-01-03T07:08:00.000-08:00</published><updated>2011-01-13T06:47:16.820-08:00</updated><title type='text'>CommunicationFlow.java</title><content type='html'>&lt;span style=&quot;font-size: 85%;&quot;&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;     &lt;span style=&quot;color: #009900;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/communicationflowjava.html&quot;&gt;FOR FIRST PART - CLICK HERE { PART 1 }&lt;/a&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;package com.jovialjava.example;

public class Common {
    
    public static String appendSTX_ETX_LRC(String request){
        String LRCString = calculateLRC(request + &quot;03&quot;);
        System.out.println(LRCString);
        return &quot;02&quot; + request + &quot;03&quot; + LRCString ;
    }
    
    public static String calculateLRC(String inputString){
        /*
         * String is Hex String, need to convert in ASCII.
         */
        inputString = convertHexToString(inputString);
        int checksum = 0;
        char[] chars = inputString.toCharArray();
        for (int i = 0; i &amp;lt; chars.length; i++) {
            checksum ^= chars[i];
        }    
        String val = Integer.toHexString(checksum);        
        if(val.length()&amp;lt; 2){
            val = &quot;0&quot;+ val;
        }
        
        return val;       
    }
    public static String convertHexToString(String hex){
         
          StringBuilder sb = new StringBuilder();
          StringBuilder temp = new StringBuilder();             
          //49204c6f7665204a617661 split into two characters 49, 20, 4c...
          for( int i=0; i&amp;lt;hex.length()-1; i+=2 ){
               //grab the hex in pairs
              String output = hex.substring(i, (i + 2));
              //convert hex to decimal
              int decimal = Integer.parseInt(output, 16);                  
                  sb.append((char)decimal);
//              }
              temp.append(decimal);
          }         
     
          return sb.toString();
      }

    public static String asciiToHex(String ascii){
        StringBuilder hex = new StringBuilder();
        
        for (int i=0; i &amp;lt; ascii.length(); i++) {
            hex.append(Integer.toHexString(ascii.charAt(i)));
        }       
        return hex.toString();
    } 
    
    /*
     * This method will check if LRC received is ok.
     */
    public static boolean verify(String response){
        try {
            String newResponse = Common.convertHexToString(response);
            int lrc = getLRCValue(newResponse);
            response = getLRCString(response);    
            System.out.println(response);
            return verifyLrc(response, lrc);
        } catch (Exception e) {
            System.err.println(&quot;!!!ERROR!!! -Exception while calcualting LRC&quot;);
            e.printStackTrace();
            return false;
        }
    }
    private static int getLRCValue(String response){        
        byte[] bytes = response.getBytes();
        int tmp=-1;
        boolean flag = false;        
        for(byte b : bytes){            
            if(b == 3){
                flag = true;                
                continue;
            }
            if(flag){
            tmp = b;
            break;
            }            
        }        
        return tmp; 
    }
    
    private static String getLRCString(String response) throws Exception{
        if(response.length() % 2 != 0){
            System.err.println(&quot;Response MSG doesn&#39;t have even number of chars&quot;);
            throw new IllegalArgumentException();
        }
        boolean exit = false;
        boolean start = false;
        String LRCStr = &quot;&quot;;
        for( int i=0; i&amp;lt;response.length()-1; i+=2 ){
               //grab the hex in pairs
              String output = response.substring(i, (i + 2));
              if(output.equals(&quot;03&quot;)){    
                  LRCStr = LRCStr + output;
                  exit = true;
              }else if(output.equals(&quot;02&quot;)){
                  start = true;
                  continue;
              }
              
              if(!exit){
                  if(start){
                      LRCStr = LRCStr + output;
                  }
              }else{
                  break;
              }
        }
        return LRCStr;
    }
    
    private static boolean verifyLrc(String resp, int lrcVal)
    {
        if(resp == null || resp.length() == 0)
            return false;
            //int lrc_encountered = calculate1byteLRC(resp);
        int lrc_encountered = Integer.parseInt(calculateLRC(resp),16);

            if( lrcVal != lrc_encountered ) {
                System.err.println(&quot;Mismatched LRC (expected [&quot; + lrc_encountered + &quot;], encountered [&quot;
                    + lrcVal + &quot;])&quot;);
                return false;
            }else{    
                System.out.println(&quot;LRC Matched (expected [&quot; + lrc_encountered + &quot;], encountered [&quot;
                    + lrcVal + &quot;])&quot;);
                return true;
            }
        
    }
    

}

&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
===========================================================================&lt;br /&gt;
&lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/detail?name=Common.java&amp;amp;can=2&amp;amp;q=#makechanges&quot;&gt;Download Both Files from Here&lt;/a&gt;&lt;br /&gt;
&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/631729745733615313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/631729745733615313' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/631729745733615313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/631729745733615313'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/communicationflowjava_03.html' title='CommunicationFlow.java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-845518289402948317</id><published>2011-01-03T06:27:00.000-08:00</published><updated>2011-01-13T06:44:15.479-08:00</updated><title type='text'>Part 6 of 6: CommunicationFlow.java</title><content type='html'>&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;================================================================&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;PLEASE DOWNLOAD THE JAVA PROGRAM FROM HERE - &lt;/span&gt;&lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot; style=&quot;color: red; font-weight: bold;&quot;&gt;DOWNLOAD THIS PROGRAM&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;================================================================&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;package com.jovialjava.example;

import com.jovialjava.DataOperation;
import com.jovialjava.DataResult;
import com.jovialjava.RS232Executor;
import com.jovialjava.DTO.Instruction;
import com.jovialjava.DTO.SerialConfiguration;
import com.jovialjava.exceptions.RS232Exception;

public class CommunicationFlow {    
    /*
     * We need two Objects to interact with Serial Device (D)
     */
    private Instruction instruction = null;
    private SerialConfiguration config = null;
    private RS232Executor executor = null;    
    /*
     * This is required to collect response from Serial Device
     */
    private String response = new String(&quot;&quot;);
    /*
     * Universal Values
     */
    public static final Byte ACK = new Byte((byte)6);
    public static final Byte NACK = new Byte((byte)21);    
    /*
     * Default Constructor
     */
    public CommunicationFlow(){
        /*
         * Setting up Serial Device Configuration.
         */
        this.config = new SerialConfiguration();
        this.config.setComPort(&quot;COM1&quot;);
        this.config.setBaud(&quot;9600&quot;);
        this.config.setDataBits(&quot;8&quot;);
        this.config.setFlowControl(&quot;none&quot;);
        this.config.setParity(&quot;none&quot;);
        this.config.setStopBits(&quot;1&quot;);
        /*
         * Preparing Instrcutor by passing this configuration to it.
         */
        this.instruction = new Instruction();
        this.instruction.setConfig(this.config);
        /*
         * Preparing the executor by passing the instructor to it.
         */
        this.executor = new RS232Executor();
        this.executor.init(instruction);        
    }
    
    /**
     * Main method to execute the Sample Example
     */
    public static void main(String... args){
        try {
            String request =&quot;COM PORT COMMUNICATION USING JAVA BY JOVIAL JAVA&quot;;
            CommunicationFlow commFlow = new CommunicationFlow();
            request = Common.asciiToHex(request);
            commFlow.send(Common.appendSTX_ETX_LRC(request));
            System.out.println(&quot;___________________________________&quot;);
            System.out.println(&quot;RESPONSE -[&quot;+ commFlow.response+&quot;]&quot;);
        } catch (RS232Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * This is the main method which implements the flow.
     */
    public void send(String hexRequestData) throws RS232Exception /* this is the only exception thrown by RS232Java */{
        /*
         * STEP 1 : Connect to Serial DEVICE and check if operation was successful
         */
        this.instruction.setOperation(DataOperation.INITIALIZE);
        this.executor.execute();
        this.checkInitResult(this.instruction.getResult());
        /*
         * STEP 2: If we reach here, means Computer (C) is SUCCESSFULLY CONNECTED TO Serial Device (D)
         * Our next target it to send data to serial device and wait for Serial Device to respond with ACK/NAK
         * Maximum Retry Count = 3
         * Waiting time for Serial Device to send ACK = 2000 ms
         * Data to send : request Data
         */
        this.SendRequest(hexRequestData, 2000, 3);
        /*
         * STEP 3: If we reach here, means serial device has SUCCESSFULLY SENT ACK.
         * Our next Target it to wait for SERIAL DEVICE TO SEND RESPONSE.
         * Waiting time for response = 30,000 ms
         * Maximum Retry Count = 3 ( In case of LRC is not correct)
         */
        this.performResponse(30000, 3);
        /*
         * STEP 4: If we reach here means RESPONSE HAS Been Received
         * Our next target it too close the connection with Serial Device.
         */
        this.instruction.setOperation(DataOperation.CLOSE_PORT);
        this.executor.execute();
        /*
         * If one wants, one can check the result of closing of operation by
         * this.instruction.getResult()
         */
        //=============MISSION ACCOMPLISHED=============================    
    }
    /*
     * This method checks the Initialization request
     */
    private void checkInitResult(DataResult result) {
        switch (result) {
        case SUCCESS:
            System.out.println(&quot;SERIAL DEVICE SUCCESSFUL INITIALIZED&quot;);
            break;
        case FAILED:
            System.err.println(&quot;____________________________________________&quot;);
            System.err.println(&quot; SERIAL DEVICE FAILED TO INITALIZE &quot;);
            System.err.println(&quot;____________________________________________&quot;);
            System.exit(1);        
        }
    }    
    /*
     * this method will send the DATA to Serial DEVICE and will wait for ACK/NAK
     */
    private void SendRequest(String requestData, Integer ackWaitTime, int retryCount) throws RS232Exception{
            this.instruction.setOperation(DataOperation.SEND_N_WAIT_FOR_ACK);
            this.instruction.setTimeOutInMilliSeconds(ackWaitTime);
            this.instruction.setRequest(requestData, null);
            executor.execute();            
            int NAKcounter = 0;
            int noDataCounter = 0;
            boolean breakFlag = false;            
while(!breakFlag){
            DataResult result = instruction.getResult();
                switch (result) {
                case ACK:{
                    System.out.println(&quot;SERIAL DEVICE COMMUNICATED SUCCESSFULLY&quot;);
                    breakFlag = true;
                    break;
                }
                case NACK:{
                    if(NAKcounter &amp;lt; retryCount){
                        NAKcounter++;
                        System.err.println(&quot;NACK Received, Retrying count[&quot;+NAKcounter+&quot;]&quot;);
                        executor.execute();                
                    }else{
                        System.err.println(&quot;NACK Recieved more than &quot;+ NAKcounter + &quot; times&quot;);
                        System.exit(1);
                    }
                    break;
                }            
                case NO_DATA:{
                    if(noDataCounter &amp;lt; retryCount){
                        noDataCounter++;
                        System.err.println(&quot;NO Data Received, Retrying count[&quot;+noDataCounter+&quot;]&quot;);
                        executor.execute();                                            
                    }else{
                        System.err.println(&quot;No Data Recieved more than &quot;+ noDataCounter + &quot; times&quot;);
                        System.exit(1);
                    }
                    break;
                }
                case FAILED:{
                    System.err.println(&quot;____________________________________________&quot;);
                    System.err.println(&quot; SERIAL DEVICE FAILED TO COMMUNICATE &quot;);
                    System.err.println(&quot;____________________________________________&quot;);
                    System.exit(1);
                }
                }
            }        
    }
    
    
private void performResponse(Integer responseTimeOut, Integer retryCount) throws RS232Exception{
        instruction.setOperation(DataOperation.WAIT_FOR_ETX_LRC);
        instruction.setTimeOutInMilliSeconds(responseTimeOut);
        executor.execute();        
        int NAKCounter = 0 ;        
        boolean breakFlag = false;
        
    while(!breakFlag){    
        DataResult result = instruction.getResult();
            switch (result) {
                case RESPONSE:{
                     this.response = instruction.getHexResponse();                                                          
                    // CHECK THE LRC value here    
                    if(!this.verifyLRC(this.response)){
                            if(NAKCounter &amp;lt; retryCount){                                
                                instruction.setRequest(null,new byte[]{NACK});
                                instruction.setOperation(DataOperation.CLR_SEND_N_WAIT_FOR_ETX_LRC);                                
                                executor.execute();        
                                NAKCounter++;
                                continue;
                            }else{
                                System.err.println(&quot;!!!ERROR!!! - BAD LRC IN RESPONSE&quot;);
                                System.exit(1);
                            }
                        }else{
                            /*
                             * Means LRC is OK, SEND ACK and RETURN
                             */
                            instruction.setRequest(null,new byte[]{ACK});
                            instruction.setOperation(DataOperation.SEND);
                            executor.execute();    
                            breakFlag = true;
                        }
                    break;                
                }case NO_DATA:{
                    System.err.println(&quot;____________________________________________&quot;);
                    System.err.println(&quot; SERIAL DEVICE FAILED TO SEND RESPONSE &quot;);
                    System.err.println(&quot;____________________________________________&quot;);
                    System.exit(1);                    
                }case FAILED:{
                    System.err.println(&quot;____________________________________________&quot;);
                    System.err.println(&quot; SERIAL DEVICE FAILED TO COMMUNICATE &quot;);
                    System.err.println(&quot;____________________________________________&quot;);
                    System.exit(1);                
                }
            }
    }
        
    }

    private boolean verifyLRC(String hexResponse){
        return Common.verify(response);        
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/communicationflowjava_03.html&quot;&gt;. . . . .&lt;/a&gt;&lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/communicationflowjava_03.html&quot;&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. . . . . &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;. &lt;/span&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt;Another Required File&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-size: 85%;&quot;&gt; &lt;/span&gt;&lt;/blockquote&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/845518289402948317/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/845518289402948317' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/845518289402948317'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/845518289402948317'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/communicationflowjava.html' title='Part 6 of 6: CommunicationFlow.java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-7844632325946532110</id><published>2011-01-03T05:50:00.000-08:00</published><updated>2011-01-13T06:37:20.793-08:00</updated><title type='text'>Part 5 of 6: Java Program to implement the Flow</title><content type='html'>&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw_ZuIrc4EcUSW2u58v44yFikufZ0MP4DeYMaVNwG49x3CXVN9HlykR9njkAa0xGQ2OuRv8UOBQf6A1ygJwFEeEWXHQevi-3g6jLqsDeXvCPsHJ6evbEyyWkmPTEUVHUZIBHK3R-irWhs/s1600/exampleProgram.JPG&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; id=&quot;BLOGGER_PHOTO_ID_5557957492202613394&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw_ZuIrc4EcUSW2u58v44yFikufZ0MP4DeYMaVNwG49x3CXVN9HlykR9njkAa0xGQ2OuRv8UOBQf6A1ygJwFEeEWXHQevi-3g6jLqsDeXvCPsHJ6evbEyyWkmPTEUVHUZIBHK3R-irWhs/s400/exampleProgram.JPG&quot; style=&quot;cursor: pointer; height: 400px; width: 364px;&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;====================================================================&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;                         Click Here - &lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/communicationflowjava.html&quot; style=&quot;font-family: courier new;&quot;&gt;Part 6 Of 6&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;====================================================================&lt;/span&gt;&lt;br /&gt;
&lt;blockquote&gt;&lt;/blockquote&gt;&lt;img alt=&quot;&quot; src=&quot;file:///C:/DOCUME%7E1/sjain/LOCALS%7E1/Temp/moz-screenshot-2.png&quot; /&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/7844632325946532110/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/7844632325946532110' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/7844632325946532110'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/7844632325946532110'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/java-program-to-implement-flow.html' title='Part 5 of 6: Java Program to implement the Flow'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw_ZuIrc4EcUSW2u58v44yFikufZ0MP4DeYMaVNwG49x3CXVN9HlykR9njkAa0xGQ2OuRv8UOBQf6A1ygJwFEeEWXHQevi-3g6jLqsDeXvCPsHJ6evbEyyWkmPTEUVHUZIBHK3R-irWhs/s72-c/exampleProgram.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-4892005998295652264</id><published>2011-01-03T05:43:00.000-08:00</published><updated>2011-02-01T05:27:26.485-08:00</updated><title type='text'>Part 4 of 6: A Programmtic Example</title><content type='html'>&lt;span style=&quot;font-family: georgia;&quot;&gt;=======================================================================&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-family: georgia;&quot;&gt;Click Here - &lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/java-program-to-implement-flow.html&quot; style=&quot;font-family: georgia;&quot;&gt;PART 5 OF 6&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: georgia;&quot;&gt;=======================================================================&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUQqHZDIPwb-_7OxjjMgnaNcPuRy77uEiCzwLm3pvQPJA86Joj8D8qXEYR2SSaM8Ls_lIHHhJI7qCCvDq0E18yFDTCcXX2YKOzAoxn4_KLyjti3gMRHpxYnQCEsKw56gjZLzOpypNE7Ik/s1600/exampleFlow.JPG&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;span style=&quot;font-size: 100%;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Lets Suppose we have following flow to be implemented -&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihZYvKZLCV-FjeErtiekgSh6Qb_086RAmaSvCwftck9o2ZnCuMGWd1CS_1rIs4hqvXRkzVhyphenhypheniqN_DSVhWsGtYh3wOyLK5QpE_XbdkR1iMNDdLXjojmrSEjkE2Kq5EEMVbvRqticT94SxU/s1600/exampleFlow.JPG&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;430&quot; id=&quot;BLOGGER_PHOTO_ID_5557956453863179778&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihZYvKZLCV-FjeErtiekgSh6Qb_086RAmaSvCwftck9o2ZnCuMGWd1CS_1rIs4hqvXRkzVhyphenhypheniqN_DSVhWsGtYh3wOyLK5QpE_XbdkR1iMNDdLXjojmrSEjkE2Kq5EEMVbvRqticT94SxU/s640/exampleFlow.JPG&quot; style=&quot;height: 269px; width: 400px;&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: georgia;&quot;&gt;=======================================================================&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-family: georgia;&quot;&gt;Click Here - &lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/java-program-to-implement-flow.html&quot; style=&quot;font-family: georgia;&quot;&gt;PART 5 OF 6&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: georgia;&quot;&gt;=======================================================================&lt;/span&gt;&lt;br /&gt;
&lt;img alt=&quot;&quot; src=&quot;file:///C:/DOCUME%7E1/sjain/LOCALS%7E1/Temp/moz-screenshot.png&quot; /&gt;&lt;img alt=&quot;&quot; src=&quot;file:///C:/DOCUME%7E1/sjain/LOCALS%7E1/Temp/moz-screenshot-1.png&quot; /&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/4892005998295652264/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/4892005998295652264' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4892005998295652264'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4892005998295652264'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/programmtic-example.html' title='Part 4 of 6: A Programmtic Example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihZYvKZLCV-FjeErtiekgSh6Qb_086RAmaSvCwftck9o2ZnCuMGWd1CS_1rIs4hqvXRkzVhyphenhypheniqN_DSVhWsGtYh3wOyLK5QpE_XbdkR1iMNDdLXjojmrSEjkE2Kq5EEMVbvRqticT94SxU/s72-c/exampleFlow.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-2571337822537802921</id><published>2011-01-02T06:48:00.000-08:00</published><updated>2011-01-13T06:33:10.211-08:00</updated><title type='text'>Part 3 of 6: Setting up CLASSPATH for RS232 Jar</title><content type='html'>&lt;span style=&quot;font-family: courier new;&quot;&gt;=============================================================================&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: courier new; text-align: center;&quot;&gt;Click Here -&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/programmtic-example.html&quot;&gt;PART 4 OF 6&lt;/a&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt; =============================================================================&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #ff6666; font-family: courier new; font-weight: bold;&quot;&gt;Step 1:Please download Rxtxcomm.jar from this location-&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://rxtx.qbang.org/wiki/index.php/Download&quot; style=&quot;font-family: courier new;&quot;&gt;Download RxTx Jar&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;There will be 3 files-&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;1) RXTXcomm.jar&lt;/span&gt;  : Please keep this jar in your classpath&lt;/span&gt;.&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;2) rxtxParallel.dll&lt;/span&gt; : Please copy this path also in your classpath or else one can copy this file in&lt;br /&gt;
{java install directory}/bin/&lt;span style=&quot;font-family: courier new;&quot;&gt; &lt;/span&gt;directory.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;for example- on my system- these files are at this location -&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;C:\Program Files\Java\jre6\bin&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;some of other files available in this folder are - java.exe, rmid.exe etc.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;3) rxtxSerial.dll&lt;/span&gt; - Same as rxtxParallel.dl&lt;/span&gt;l.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #ff6666; font-family: courier new; font-weight: bold;&quot;&gt;Step 2 – Please download the RS232java1.0.0.jar and java docs from this location&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot; style=&quot;font-family: courier new;&quot;&gt;Download RS232Java-1.0.0.jar&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;Please copy this file in your classpath.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&quot; style=&quot;font-family: courier new;&quot;&gt;Download RS232Java-1.0.0docs.zip&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;=============================================================================&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: courier new; text-align: center;&quot;&gt;Click Here -&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/programmtic-example.html&quot;&gt;PART 4 OF 6&lt;/a&gt;&lt;span style=&quot;text-decoration: underline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt; =============================================================================&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/2571337822537802921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/2571337822537802921' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2571337822537802921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/2571337822537802921'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/blog-post.html' title='Part 3 of 6: Setting up CLASSPATH for RS232 Jar'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-1606403638771801983</id><published>2011-01-02T06:41:00.000-08:00</published><updated>2011-01-13T06:29:56.463-08:00</updated><title type='text'>Part 2 of 6: Architecture of RS232Java</title><content type='html'>&lt;span style=&quot;font-family: courier new;&quot;&gt;===================================================================&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;                       Click Here - &lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/blog-post.html&quot; style=&quot;font-family: courier new;&quot;&gt;PART 3 OF 6&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;===================================================================&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;First of all there are only 2 known APIs are available in the market for Serial Communication in JAVA.&lt;/span&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;JavaComm -&lt;/span&gt; It is provided by Sun Microsystems and in 2005 Sun silently withdrew Java Comm support for Windows.&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;RxTx -&lt;/span&gt; This is a very good API which provide support for windows as well as Linux. But in my opinion in order to properly use this API, you need intermediate level of knowledge of Serial communication. Rx-Tx - if not used as a Java Comm driver - provides a richer interface, but one which is not standardized.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;RS232Java&lt;/span&gt; -This API &lt;/span&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;has been built on top of RXTXcomm to provide a richer and better interface. It can support both 32 and 64 bit machine. In Serial programming flows are complex but similar most of time. This API has lots of in built flows, so user can decide which flow to execute. Everything will be handled by this API.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight: bold;&quot;&gt;Architecture:&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;br /&gt;
This API reflects replica of Instructor-Executor based relationship, in which user of the API will act as Instructor and API will be executor. You decide which flow to be executed and executor will do that.&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF0HWu4Xob8cqcgX9FZp2MnOQcQBe6nTtQPPI-x0Ma4DFzpwvnLHsYoOGdMQ7a7UHh7oedVDq9EXkIfu_CTZRXAggW1mNadbiCikg-QYngCxw7JLlRpbwjq1h-fLnrV491dKhbN5gJdQQ/s1600/RS232_Architecture.jpg&quot; onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; id=&quot;BLOGGER_PHOTO_ID_5557599784942156418&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF0HWu4Xob8cqcgX9FZp2MnOQcQBe6nTtQPPI-x0Ma4DFzpwvnLHsYoOGdMQ7a7UHh7oedVDq9EXkIfu_CTZRXAggW1mNadbiCikg-QYngCxw7JLlRpbwjq1h-fLnrV491dKhbN5gJdQQ/s320/RS232_Architecture.jpg&quot; style=&quot;cursor: pointer; height: 175px; width: 320px;&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;===================================================================&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt;       Click Here - &lt;/span&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/blog-post.html&quot; style=&quot;font-family: courier new;&quot;&gt;PART 3 OF 6&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: courier new;&quot;&gt; ===================================================================&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: courier new;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/1606403638771801983/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/1606403638771801983' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1606403638771801983'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/1606403638771801983'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2011/01/architecture-of-rs232java.html' title='Part 2 of 6: Architecture of RS232Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgF0HWu4Xob8cqcgX9FZp2MnOQcQBe6nTtQPPI-x0Ma4DFzpwvnLHsYoOGdMQ7a7UHh7oedVDq9EXkIfu_CTZRXAggW1mNadbiCikg-QYngCxw7JLlRpbwjq1h-fLnrV491dKhbN5gJdQQ/s72-c/RS232_Architecture.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-6135609156936593322</id><published>2010-12-06T19:31:00.001-08:00</published><updated>2011-02-03T06:29:51.606-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="COM PORT"/><category scheme="http://www.blogger.com/atom/ns#" term="COM PORT Communicaiton using java"/><title type='text'>Part 1 of 6: RS232 Communication Using JAVA</title><content type='html'>&lt;blockquote&gt;&lt;/blockquote&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;===============================================================&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/architecture-of-rs232java.html&quot;&gt;&lt;br /&gt;
&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms; text-align: center;&quot;&gt;&lt;span style=&quot;color: red; font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: courier new; text-decoration: underline;&quot;&gt;Click HERE -&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-family: courier new; font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&lt;a href=&quot;http://jovialjava.blogspot.com/2011/01/architecture-of-rs232java.html&quot;&gt;PART 2 of 6&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;span style=&quot;color: red; font-family: courier new; font-weight: bold; text-decoration: underline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;===============================================================&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;If you are not sure about – &lt;/span&gt;&lt;/div&gt;&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;Ø &lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;What is the difference between DCE and DTE?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;What is the significance of RTS, DTS, CS, ACK, NACK, STX, ETX etc in Serial Communication?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Whether to implement event based or time based communication?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Whether to transmit data in Hex, ASCII or Hex dumps?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Which combination of Baud rate, data bits, parity bit, hardware flow, and stop bit one should use?&lt;/span&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote style=&quot;font-family: courier new;&quot;&gt;&lt;/blockquote&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; font-weight: bold; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;And&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraph&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&amp;gt;&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;If you don’t know that native JAVA doesn’t have unsigned bytes and serial communication is less error prone if you transmit data in unsigned bytes.&lt;/span&gt;&lt;span style=&quot;font-size: 100%; font-weight: bold; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;And&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;blockquote style=&quot;font-family: courier new; font-weight: bold;&quot;&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;If you are looking for a very simple solution to implement the complex flow of serial communication?&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;If you are looking for a JAVA based solution of serial communication?&lt;/span&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;If answers to most of questions are YES, then you are at right page. Here we will go through a small tutorial to make you master of serial communication in JAVA.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;font-family: trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Note: &lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;This tutorial assumes that you have eclipse install on your system.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;Ø&lt;span style=&quot;font-size-adjust: none; font-stretch: normal; font-style: normal; font-variant: normal; font-weight: normal; line-height: normal;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: 100%; line-height: 115%;&quot;&gt;You have basic knowledge of JAVA.&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;font-family: trebuchet ms; text-indent: -0.25in;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/6135609156936593322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/6135609156936593322' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/6135609156936593322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/6135609156936593322'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2010/12/rs232-communication-using-java.html' title='Part 1 of 6: RS232 Communication Using JAVA'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-3275854786718415300</id><published>2010-06-24T01:12:00.000-07:00</published><updated>2010-06-24T01:24:01.789-07:00</updated><title type='text'>XML to HashMap in JAVA</title><content type='html'>Hi Guys,&lt;br /&gt;&lt;br /&gt;This program will convert any XML document to a HashMap&lt;string,&gt; in Java.&lt;br /&gt;Download - Please download it from here-&lt;br /&gt;http://code.google.com/a/eclipselabs.org/p/java-utility/downloads/list&lt;br /&gt;&lt;br /&gt;1) Program 1:  XMLToMap.java&lt;br /&gt;   This is the main java program. It has public static void main method.&lt;br /&gt;2) Program 2:  ValueHandler.java&lt;br /&gt;    This is the program which will actually do the conversion.&lt;br /&gt;3) Prgram 3: StringToSteam.java&lt;br /&gt;  This is just a small util which will convert String to InputStream.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;How to use?&lt;/span&gt;&lt;br /&gt; 1) Please unzip the zip file.&lt;br /&gt; 2) Copy all the 3 files in same folder&lt;br /&gt; 3) Run XMLToMap.java&lt;br /&gt;&lt;br /&gt;The program will run on Java 1.5 and higher.</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/3275854786718415300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/3275854786718415300' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/3275854786718415300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/3275854786718415300'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2010/06/xml-to-hashmap-in-java.html' title='XML to HashMap in JAVA'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-674557581614690902.post-4915752169337213143</id><published>2010-06-10T04:54:00.000-07:00</published><updated>2011-02-01T05:26:46.701-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="COM PORT"/><category scheme="http://www.blogger.com/atom/ns#" term="COM PORT Communicaiton using java"/><title type='text'>com port  communication using java</title><content type='html'>&lt;span style=&quot;font-style: italic; font-weight: bold;&quot;&gt;NOTE: For step by step tutorial please click on this link -&lt;/span&gt;&lt;br /&gt;
&lt;a href=&quot;http://jovialjava.blogspot.com/2010/12/rs232-communication-using-java.html&quot; style=&quot;color: red; font-weight: bold;&quot;&gt;COM PORT COMMUNICATION USING JAVA (RS232JAVA-1.0.0 API)&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;import gnu.io.*;
import java.io.*;

public class ComPortTest
{

/*
 * This program will connect to COM PORT Device and send TEST MESSAGE to it.
 * After sleeping for 500ms , It will read the response from SAME DEVICE.
 * copyright (c) 2011 http://www.jovialjava.blogspot.com
 * For detailed Tutorial please check it out - 
 * http://jovialjava.blogspot.com/2010/12/rs232-communication-using-java.html
 */
public boolean testComPort(String comPort, String testMessage){
    CommPortIdentifier portIdentifier = null;
    try{
        portIdentifier = CommPortIdentifier.getPortIdentifier(comPort);
        if (portIdentifier.isCurrentlyOwned())
        {
            System.out.println(&quot;Port in already in use, owner [&quot;+ portIdentifier.getCurrentOwner()+&quot;]&quot;);
            return false;
        }else{
            SerialPort serialPort = (SerialPort) portIdentifier.open(&quot;ListPortClass&quot;, 300);
            System.out.println(&quot;COM PORT NAME -[&quot;+portIdentifier.getName()+&quot;] BAUD RATE [&quot;+serialPort.getBaudRate()+&quot;]&quot; );
            serialPort.setSerialPortParams(300, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            OutputStream mOutputToPort = serialPort.getOutputStream();
            InputStream mInputFromPort = serialPort.getInputStream();
            mOutputToPort.write(testMessage.getBytes());
            System.out.println(&quot;Byte Sent&quot;);
            mOutputToPort.flush();
            System.out.println(&quot;Waiting for response&quot;);
            Thread.sleep(500);
            byte mBytesIn [] = new byte[20];
            mInputFromPort.read(mBytesIn);
            String value = new String(mBytesIn);
            System.out.println(&quot;Response from Serial Device: &quot;+value);
            mOutputToPort.close();
            mInputFromPort.close();
            serialPort.close();
            System.out.println(&quot;!!! ======================== COM PORT TEST - PASS =============================!!!&quot;);
            return true;
        }
    }catch(NoSuchPortException nspe){
            System.out.println(&quot;No such port [&quot;+comPort+&quot;] found on system &quot;+ nspe.getMessage());
    }catch(PortInUseException piue){
            System.out.println(&quot;port [&quot;+comPort+&quot;] is already in use, owner[&quot;+ portIdentifier.getCurrentOwner()+&quot;] &quot;+piue.getMessage());
    }catch(UnsupportedCommOperationException ucoe){
            System.out.println(&quot;Setting Communciation Operations - 300, DataBits -&quot;+ SerialPort.DATABITS_8+&quot;, StopBits &quot;+ SerialPort.STOPBITS_1+&quot;, Parity &quot;+ SerialPort.PARITY_NONE);
            System.out.println(&quot;Communication Operations are not supported on port [&quot;+comPort+&quot;] &quot;+ ucoe.getMessage());            
    }catch(IOException ioe){
            System.out.println(&quot;IOExceptin while trying to communicate with COM PORT device &quot;+ ioe.getMessage());
    }catch(InterruptedException ie){
            System.out.println(&quot;Interruption while waiting for response from COM Device &quot;+ ie.getMessage());
    }
    return false;
}

    public static void main(String... args){
            new ComPortTest().testComPort(&quot;COM1&quot;, &quot;JOVIAL JAVA&quot;);    
    }
} 
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://jovialjava.blogspot.com/feeds/4915752169337213143/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/674557581614690902/4915752169337213143' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4915752169337213143'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/674557581614690902/posts/default/4915752169337213143'/><link rel='alternate' type='text/html' href='http://jovialjava.blogspot.com/2010/06/com-port-communication-using-java.html' title='com port  communication using java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>