<?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>CertPal &#187; tutorial</title>
	<atom:link href="http://www.certpal.com/blogs/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Tue, 24 Aug 2010 17:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Google wave java robot overview</title>
		<link>http://www.certpal.com/blogs/2009/10/google-wave-java-robot-overview/</link>
		<comments>http://www.certpal.com/blogs/2009/10/google-wave-java-robot-overview/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 15:17:53 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wave]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=408</guid>
		<description><![CDATA[An intro to writing your own google wave java robot. This article links to other useful articles that allow you to get started writing your own robot. Use your robot wisely and make a nice one.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F" height="61" width="51" /></a></div><p><img class="alignright size-full wp-image-416" title="appspot" src="http://www.certpal.com/blogs/wp-content/uploads/appspot.PNG" alt="appspot" width="114" height="71" />As most of you are aware by now, developers can write java robots that can aid a conversation that happens in google wave. A conversation is a wavelet and each reply in this wavelet is called a blip. There are some &#8216;getting started&#8217; tutorials available out there that are of great help. These links should help you</p>
<p><a href="http://code.google.com/apis/wave/extensions/robots/java-tutorial.html" target="_blank">Official google wave guide</a><br />
<a href="http://www.vogella.de/articles/GoogleWave/article.html" target="_blank"> Google wave getting started</a> &#8211; Sort of an abridged version of the official guide written by Vogella.</p>
<p>Grasping the overall picture of a java robot is a little difficult. This is because there are no flow or architecture diagrams (at least none that I know of) that show you the sequence of events. Given below is a diagram that does that. Assume that you wrote a java robot that is meant to edit blips in a wavelet. The robot should provide a profanity filter service which will delete objectionable words from the wave. This is how the series of events happen.</p>
<p><strong>Google wave robot sequence of events:</strong></p>
<p><img class="aligncenter size-full wp-image-412" title="google-wave-java-robot-arch" src="http://www.certpal.com/blogs/wp-content/uploads/google-wave-java-robot-arch.PNG" alt="google-wave-java-robot-arch" width="550" height="684" /></p>
<p>1. User logs in</p>
<p>2. Replies to a blip.</p>
<p>3. Our java robot is already deployed under a .appspot.com address. Its capability allows it to trigger an event that signals that a blip was created.</p>
<p>4. The triggered event is actually a HTTP request that goes out to the java robot servlet from google wave. The servlet then contacts wave using the wave client library API.</p>
<p>5. The blips in the wave are accessed using the client API. An event object return all the blips that were created.</p>
<p>6. The blip in question is identified and edited if needed.</p>
<p>7. Optionally you can include a &#8220;gadget&#8221; into your wave.  The robot can append a blip and add a gadget to it. A gadget is an XML file that houses, at times, javascript and HTML related content. The gadget can be hosted on any site. This is unlike the java robot, which at the time of this writing is only allowed to be hosted in google app engine (GAE). There are plans to allow developers to host it elsewhere on their own servers.</p>
<p><strong>More on gadgets:</strong></p>
<p>To get a feel for what a gadget is and how it can be used, check out the <a href="http://answers.oreilly.com/topic/374-reacting-to-wave-conversations-and-inserting-wave-gadgets-with-a-wave-robot/" target="_blank">ISBN gadget</a> and the gadget help files. Gadgets that are integrated with wave have a sense of state. This state is just a saved variable that is accessible by all users on that wave. This is like having a variable exposed to several threads at the same time. If you modify the state variables in the gadget, do so carefully since it is equivalent to accessing a non thread safe variable.</p>
<p>So there you go. The wave is still a developer&#8217;s playground and less of a lay man user&#8217;s tool. It will soon mature. If you happen to have a wave account, give the robots a try. Let others know what robot you made.<br />
<script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2009/10/google-wave-java-robot-overview/';</script><br />
<script type="text/javascript">var dzone_title = 'Google wave java robot overview';</script><br />
<script type="text/javascript">var dzone_blurb = 'An intro to writing your own google wave java robot. This article links to other useful articles that allow you to get started writing your own robot. Use your robot wisely and make a nice one.';</script><br />
<script type="text/javascript">var dzone_style = '2';</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script> </p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/10/google-wave-java-robot-overview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java thread tutorial</title>
		<link>http://www.certpal.com/blogs/2009/10/java-thread-tutorial/</link>
		<comments>http://www.certpal.com/blogs/2009/10/java-thread-tutorial/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 10:51:00 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[Java certifications]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=389</guid>
		<description><![CDATA[Java thread programming can be a little tricky to understand. This post provides a tutorial that SCJP candidates can use. The examples illustrate various scenarios that one encounters when using threads.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F" height="61" width="51" /></a></div><p><img class="alignright size-full wp-image-393" title="j_thread_locks" src="http://www.certpal.com/blogs/wp-content/uploads/j_thread_locks.PNG" alt="j_thread_locks" width="140" height="115" />If you are a newbie to working with java threads, this post will help you. Certifications like the SCJP require you to understand java threads to a fair degree. Threads behavior can be difficult to understand even for experienced programmers, so I will try to present some examples which will help candidates identify how threads wait / lock and synchronize.</p>
<p>Lets cut the chit chat and jump into a problem. A program increments a counter in a for loop as shown below.</p>
<p><strong>Synchronizing:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</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;">class</span> StaticSync
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> s_lock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</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;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> s_counter<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</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>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> StaticSync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</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: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<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;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> iCounter<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> iCounter<span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span> iCounter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span> thread <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><span style="color: #000000; font-weight: bold;">new</span> Incrementer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StaticSync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            thread.<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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Incrementer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
    StaticSync m_staticSync<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Incrementer<span style="color: #009900;">&#40;</span>StaticSync staticSync_INP<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        m_staticSync <span style="color: #339933;">=</span> staticSync_INP<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: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>m_staticSync.<span style="color: #006633;">s_lock</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            m_staticSync.<span style="color: #006633;">s_counter</span><span style="color: #339933;">++;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>m_staticSync.<span style="color: #006633;">s_counter</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This program is pretty simple. This is the sort of code that you can expect on the SCJP exam. Advanced versions of the code above can also appear by adding notify() wait() sleep() etc into the picture. Let us concentrate on the program for now. What can be guaranteed about the output of this program ?</p>
<p>Can we say that Numbers 1 through 50 will be printed for sure ? What can be said about the order in which they are printed. Is there a guarantee about that ?</p>
<p>This program prints numbers 1 through 50 and does it consistently. What that means is that we can guarantee that this program will print 1-&gt;50 in ascending order. Why ?</p>
<p><strong>Instances of a thread object lock on the same static object:</strong></p>
<p><img class="aligncenter size-full wp-image-394" title="j_thread_static_object_lock_1" src="http://www.certpal.com/blogs/wp-content/uploads/j_thread_static_object_lock_1.PNG" alt="j_thread_static_object_lock_1" width="294" height="310" /><strong></strong></p>
<p><strong>One thread manages to obtain a lock. The others wait:</strong></p>
<p><img class="aligncenter size-full wp-image-395" title="j_thread_static_object_lock_2" src="http://www.certpal.com/blogs/wp-content/uploads/j_thread_static_object_lock_2.PNG" alt="j_thread_static_object_lock_2" width="308" height="339" /></p>
<p>We are creating a new Incrementer instance while starting each thread. We are also passing it a new StaticSync instance for every thread. However when the code tries to update the counter, it synchronizes over a static object. The static object has only one instance, so when one of the 50 thread instances holds this lock, the other thread will have to wait until the lock is released.</p>
<p>Once the lock is released the next thread will update the counter variable. Since only one thread can obtain the lock at any given time, the operation of incrementing the counter and printing its value is atomic. What that means is that the ++ operation and the System.out.println() operation will happen without any other thread intefering in the middle. It happens as one single unit. Note that m_staticSync.s_lock is actually referring to the public static variable through a non static reference. This is not a recommended practice but it was used anyway to help readers understand the concept. The code also throws encapsulation out the window. Our focus is on the thread behavior of the code alone.</p>
<p>Now let us replace <strong>synchronized (m_staticSync.s_lock)</strong> with <strong>synchronized (this)</strong>. What do you think will change (if there is any) and why ? What does &#8220;this&#8221; refer to here and how is the locking different from the locking we did in the first example ? If the program is modified so that we synchronize over a static method, which Object&#8217;s lock do we obtain ?</p>
<p>I will leave these questions to you as an exercise. The images below should provide a hint.</p>
<p><strong>Threads locking on distinct object instances:</strong></p>
<p><img class="aligncenter size-full wp-image-399" title="j_thread_nonstatic_object_lock_1" src="http://www.certpal.com/blogs/wp-content/uploads/j_thread_nonstatic_object_lock_1.PNG" alt="j_thread_nonstatic_object_lock_1" width="227" height="324" /></p>
<p><strong>Everyone is a winner:</strong></p>
<p><img class="aligncenter size-full wp-image-400" title="j_thread_nonstatic_object_lock_2" src="http://www.certpal.com/blogs/wp-content/uploads/j_thread_nonstatic_object_lock_2.PNG" alt="j_thread_nonstatic_object_lock_2" width="223" height="322" /></p>
<p>Moving on to thread interruptions</p>
<p><strong>Interrupting:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</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;">class</span> ThreadInterrupt
<span style="color: #009900;">&#123;</span>
&nbsp;
    <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>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> ThreadInterrupt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</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: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        InterruptTest interruptTest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InterruptTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Thread</span> thread <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>interruptTest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        thread.<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: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        thread.<span style="color: #006633;">interrupt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> InterruptTest <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
    <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: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Waiting&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                wait<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30000</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: #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: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Inerrupted&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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This class shows you how interruption of threads work. When a thread waits or sleeps, it goes into a blocking state. Such threads can be interrupted to come out of the blocking state. When such a thread gets &#8220;interrupted&#8221; from its blocking state, it throws an InterrutpedException. The output of the program above is</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Waiting
Inerrupted</pre></td></tr></table></div>

<p>I have never used the interrupt() method at work. But it is useful to know that this can be done. Just remember that only threads that are blocking can be interrupted. Trying to interrupt threads in the running state will not work.</p>
<p><strong>Notification:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</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;">class</span> NotificationExample
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Object</span> lock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</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;">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>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> NotificationExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">go</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: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> go<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;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> iCounter<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> iCounter<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">100</span> <span style="color: #339933;">;</span> iCounter<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Thread</span> thread <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><span style="color: #000000; font-weight: bold;">new</span> ThreadWorker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            thread.<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>
        <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><span style="color: #cc66cc;">2000</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: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>lock<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            lock.<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>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ThreadWorker <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span>
<span style="color: #009900;">&#123;</span>
    <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: #000000; font-weight: bold;">synchronized</span> <span style="color: #009900;">&#40;</span>NotificationExample.<span style="color: #006633;">lock</span><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>
                NotificationExample.<span style="color: #006633;">lock</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: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span>.<span style="color: #006633;">currentThread</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; finished waiting&quot;</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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The SCJP exam expects you to understand how waiting and notifications happen in Threads. In this example, several threads synchronize over the same lock and then wait. A few seconds later a notifyAll() call is made to notify all the threads that were waiting on this Object&#8217;s lock. As an exercise, try to change the notifyAll() call to notify(). Observe what change that does to the execution of this program.</p>
<p>Some of the code displayed above uses the sleep() method to better explain and understand Thread behaviour when you run the examples. However making a Thread sleep does not guarantee any given result. You need to keep that in mind. The scenarios may also play out differently when you remove the calls to sleep(). What would happen for example if the notify() method is called before the threads actually begin waiting ?</p>
<p>You can toy around with those three examples to get a better grasp on threads.</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/10/java-thread-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
