<?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; signal</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/tag/signal/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Thu, 20 Jan 2011 04:03:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</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>
	</channel>
</rss>

