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

<channel>
	<title>Kandy Software, Inc. &#187; Java</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/category/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Dec 2008 06:47:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Implicit &amp; Explicit Locks</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks#comments</comments>
		<pubDate>Sun, 23 Nov 2008 18:52:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[await]]></category>
		<category><![CDATA[condition]]></category>
		<category><![CDATA[explicit]]></category>
		<category><![CDATA[finally]]></category>
		<category><![CDATA[implicit]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[notify]]></category>
		<category><![CDATA[signal]]></category>
		<category><![CDATA[synchronized]]></category>
		<category><![CDATA[tr]]></category>
		<category><![CDATA[wait]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=128</guid>
		<description><![CDATA[In Computer Science, <i>synchronization</i> refers to relationships among events. For example:
<ul>
<li>Serialization: Event A must <i>happen before</i> Event B.
<li>Mutual Exclusion: Event A and Event B <i>must not happen at the same time</i>. 
</ul>
Prior to JDK 5, Java's support for synchronization was based on implicit locks by the use of <code>synchronized</code> keyword. This article introduces explicit locks introduced in JDK 5.]]></description>
			<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/locks.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/locks.png" alt="" title="locks" class="alignright size-full wp-image-131" /></a>In Computer Science, <i>synchronization</i> refers to relationships among events. For example:</p>
<ul>
<li>Serialization: Event A must <i>happen before</i> Event B.
<li>Mutual Exclusion: Event A and Event B <i>must not happen at the same time</i>.
</ul>
<p>Java&#8217;s support for synchronization is based on implicit locks by the use of <code>synchronized</code> keyword. Every object has an intrinsic lock associated with it. When a thread invokes a <b>synchronized non-static method</b>, it acquires the intrinsic lock for that method&#8217;s object. The lock is returned when the method returns (even due to an uncaught exception). Similarly, every class has an intrinsic lock associated with it. A thread acquires this class lock to invoke a <b>synchronized static</b> method and releases when the method returns. These locks are acquired and released automatically.</p>
<p>In addition to intrinsic locks, each instance also has a <i>wait set</i> associated with it. When a thread invokes <code>wait()</code> (or any overloaded <code>wait(...)</code> methods) it is placed in this wait set. The <code>wait(...)</code> methods must be called from a synchronized block. This is used in scenarios like when a thread obtains the lock but is not ready to proceed code in the critical section. </p>
<h3><strong>Implicit locks demo</strong></h3>
<p>I will demonstrate implicit locks by implementing a solution to producer-consumer problem. Source code is attached as a link at the end of the article. The producer-consumer problem can be described as follows. There are M threads producing items and N threads consuming items. They share a common buffer for transfering these items. The problem is to make sure that producers won&#8217;t add items when the buffer is full and consumers won&#8217;t remove items from an empty buffer. In <code>ImplicitLocksDemo</code> class I have 3 nested classes &#8211; <code>Producer</code>, <code>Consumer</code> and <code>Drop</code> (shared buffer). <code>Producer</code> and <code>Consumer</code> are implemented as threads. <code>Producer</code> creates a <code>String</code> object and places in the <code>drop</code> object which is shared between producers and consumers as shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Drop drop <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Drop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To make the problem a bit more complicated, the shared buffer is made to hold only one item. The <code>Drop</code> class has one member variable (<code>shared</code>) which will be set by a producer. Additionally, the <code>Drop</code> class has a variable called <code>empty</code> which tells if the buffer is full or empty. Producers can only set <code>shared</code> with an item when <code>empty</code> is true. Similarly, consumers can only read <code>shared</code> when <code>empty</code> is false. The <code>Drop</code> class has two methods called <code>get()</code> which the consumers call to read an item and a method called <code>set(String)</code> which is called by producers to set an item. Both <code>get()</code> and <code>set()</code> are made synchronized so only one thread can invoke <code>get()</code> or <code>set()</code> method at any time.</p>
<p>Now, what if a producer acquired a lock and wants to invoke <code>set()</code> method and the buffer is not empty? If a consumer had not read the item produced previously, the producer would be overwriting the item. To use the locks effectively, we use wait/notify mechanism provided by Java. <code>wait()</code> and <code>notify</code> are methods defined in <code>Object</code> class, hence, they are available to any class. Invoking <code>wait()</code> makes the current thread go into a waiting state or into the wait set associated with the object on which <code>wait()</code> was invoked. When a consumer reads the item it invokes <code>notify()</code> on <code>drop</code> object which sends a signal to all threads waiting in the wait set of <code>drop</code> object. That is to say that the shared object is now empty and a producer can safely fill the shared storage. So with the help of wait/notify, threads can effectively communicate with each other and share the critical sections in a safe manner. Complete solution is given below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @(#)ImplicitLocksDemo.java	Feb 26, 2008
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">lock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Demonstrates Producer and Consumer with a
 * 
 * @author $Author: vijaykandy $
 * @version $Revision: 1.4 $
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImplicitLocksDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Main.
	 * 
	 * @param s
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Drop drop <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Drop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Shared class
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Drop <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> shared<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Takes a message when a signal is received.
		 * 
		 * @return
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #003399;">String</span> get<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>empty<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> shared<span style="color: #339933;">;</span>
			empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> message<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Sets a message when a signal is received.
		 * 
		 * @param s
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> set<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>empty<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			shared <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span>
			empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Produces a message
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Producer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> Drop drop<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Random</span> random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> messages <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#123;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DONE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, Drop drop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">drop</span> <span style="color: #339933;">=</span> drop<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Random</span> random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				drop.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s produced: %s%n&quot;</span>, name, msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Consumes a message
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Consumer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> Drop drop<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, Drop drop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">drop</span> <span style="color: #339933;">=</span> drop<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Random</span> random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> drop.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s received: %s%n&quot;</span>, name, message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DONE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>synchronized</code> keyword can also be used with a block. In this case, threads are required to acquire the lock associated with the object in the <code>synchronized</code> statement as shown here:.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">Object</span> anObject <span style="color: #339933;">=</span> ...
	<span style="color: #666666; font-style: italic;">// ...</span>
	<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span>anObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// threads must acquire anObject's lock</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Longest Increasing Subsequence</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/lis</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/lis#comments</comments>
		<pubDate>Fri, 15 Feb 2008 00:50:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[increasing]]></category>
		<category><![CDATA[lis]]></category>
		<category><![CDATA[subsequence]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=85</guid>
		<description><![CDATA[A classic computer science problem. The problem is finding the longest increasing subset of numbers within a given set. This article presents a solution in Java. ]]></description>
			<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png" alt="" title="dna" class="alignleft size-medium wp-image-86" /></a>Finding the <a href="http://en.wikipedia.org/wiki/Longest_increasing_subsequence" target="_blank">maximum or longest increasing subsequence</a> (LIS) within a given a set of numbers is a classic Computer Science problem. The algorithm is central to many tools and is used in various disciplines related to mathematics, physics etc. In the field of Bioinformatics, LIS is used to find DNA subsequences matching a certain criteria. By recognizing certain patterns in DNA sequences scientists can determine diseases, genetic disorders etc.</p>
<h3><strong>Problem</strong></h3>
<p>Given a sequence of N integers, find the longest <i>strictly</i> increasing subsequence. For example in the sequence 56, 21, 33, 22, 34, 78, 11, 35, 46, the longest increasing subsequence is 21, 33, 34, 35, 46. In this context we will consider a NXN matrix of integers and find a sequence which is simply a series of adjacent squares. A square may not be used more than once. In the following 10 X 10 grid:</p>
<pre>
  | 0  1  2  3  4  5  6  7  8  9
--+------------------------------
0 | 95 47 30 36 60 31 57 66 12 55
1 | 35 57 41 13 82 80 71 93 31 62
2 | 89 36 98 75 91 24 95 53 37 99
3 | 25 45 26 17 15 84 80 73 96 21
4 | 75 22 43 <font color="red">96</font> 96 36 64 31 45 86
5 | 45 99 <font color="red">68</font> <font color="red">74</font> 54 14 93 17 14 55
6 | 14 42 <font color="red">52</font> 54 34 50 22 84 32 41
7 | 90 <font color="red">44</font> 73 10 71 84 20 12 55 52
8 | 95 <font color="red">33</font> <font color="red">25</font> 31 76 45 44 84 90 52
9 | 94 64 95 <font color="red">24</font> 41 63 87 93 79 12
</pre>
<p>the longest increasing subsequence is of length 8 consisting of entries 24, 25, 33, 44, 52, 68, 74, 96 as highlighted in red.</p>
<h3><strong>Solution</strong></h3>
<p>Each number and its position in the sequence are stored in an array. In the first pass, neighbors for each item in the array are found and are stored along with the item. Items are then sorted by their descending value. In the second and final pass, weights are assigned to the neighbors. The neighbor with the least value is assigned weight 1, the next highest neighbor is assigned 2 and so on. To get the longest increasing subsequence, find the element with the highest weight and print its neighbors in the descending order of weights.</p>
<p>Unzip the attachment and <code>cd</code> into the newly created folder. Compile the classes run as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\MaximumIncreasingSubsequence&gt;javac -d bin src\*.java
&nbsp;
C:\MaximumIncreasingSubsequence&gt;java -classpath bin MaximumIncreasingSubsequence small-grid.txt
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">129</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">138</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">167</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">236</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">338</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">438</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">28</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">469</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">596</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">709</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">723</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">778</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">28</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">866</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">939</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">953</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
Program took: <span style="color: #cc66cc;">380</span> milliseconds</pre></div></div>

<h3><strong>Attachments</strong></h3>
<p><a href='http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/maximumincreasingsubsequence.zip' title='Source code'>Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/lis/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boggle</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/boggle</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/boggle#comments</comments>
		<pubDate>Sat, 09 Feb 2008 00:19:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Boggle]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=77</guid>
		<description><![CDATA[A Java implementation of the popular word game.]]></description>
			<content:encoded><![CDATA[<h3><strong>The Game</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/boggle.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/boggle.png" alt="" title="boggle" class="alignleft size-medium wp-image-80" /></a><a href="http://en.wikipedia.org/wiki/Boggle" target="_blank">Boggle</a> is a word game designed by Allan Turoff. The objective of the game is to construct as many different words as you can from a grid of letters. You can use letters (in positions) that are next to each other &#8211; above, below, left, right, diagonal but you can not use the same letter more than once. Each word must be defined in a dictionary. Points are assigned according to the length of the word.</p>
<p>The source code attached here is an implementation of this game in Java. It demonstrates <a href="http://en.wikipedia.org/wiki/Recursion" target="_blank">recursion</a> to solve problems like this game. The program attempts to construct words as defined in <code>dic.txt</code> from <code>puzzle.txt</code>. Points are assigned according to the length of the word.</p>
<h3><strong>Instructions</strong></h3>
<p>To run this program you need JDK 5 or better installed. You can get JDK <a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">from here</a>.</p>
<ol>
<li>Unzip the file. This creates a folder called <code>Boggle</code></li>
<li>Compile the code: <code>javac src\*.java  -d bin</code></li>
<li>Run the program: <code>java -classpath bin Boggle</code></li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\Boggle&gt;javac src\*.java  -d bin
&nbsp;
C:\Boggle&gt;java -classpath bin Boggle
Enter boggle file:
puzzle.txt
Opened puzzle.txt
Enter dictionary name:
dic.txt
Opened dic.txt
Reading files...
Solving...
Word            | Points | Path
----------------+--------+-----------------------
            APR |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      APRTOCFED |     <span style="color: #cc66cc;">15</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            COT |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            FED |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<h3><strong>Attachments</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/boggle.zip">Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/boggle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
