<?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>Mon, 18 Jul 2011 06:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Antlr tutorial: Hello Antlr</title>
		<link>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/</link>
		<comments>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 14:10:01 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[antlr]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=997</guid>
		<description><![CDATA[An introduction / overview to ANTLR, the parser generator. We take a look at grammar that allows a computer to determine whether number A is bigger than number B. Not exactly the most complex operation in the world, but it makes for an easy intro to ANTLR.]]></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%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_3.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_3.png" alt="" title="antlr_3" width="332" height="213" class="aligncenter size-full wp-image-1007" /></a></p>
<p>Domain specific languages (DSL) are great tools to communicate with non-programmers. Normally this group includes business users that would like to configure a system / rule using a fluent language (as in &#8211; a natural language). It also includes those like my 8 year old neighbor that knows absolutely nothing about programming. He would love to tell the computer how to perform a small series of operations, without delving into the specifics. Coincidentally, I have been reading up on methodologies to approach DSLs and was introduced to <a href="http://www.antlr.org/" target="_blank">ANTLR</a>.</p>
<h2>Enter ANTLR:</h2>
<p>What my neighbor needs is an English like grammar. This grammar needs to be parsed into something meaningful at runtime. Every time the grammar changes, the parser would need to change too. ANTLR, is a &#8216;parser generator&#8217;. Once a grammar is defined, ANTLR can code-gen a lexer and a parser for this grammar. The lexer identifies tokens in any input that adheres to the grammar and the parser makes sense of these tokens.</p>
<p>A GUI by the name ANTLRWorks helps you generate the grammar and its corresponding lexer+parser code. Lets take a look at an example that asks the computer if a number A is &#8216;bigger than&#8217; or &#8216;smaller than&#8217; number B.</p>
<p>Before you delve into the code, it might be a good idea to download the <a href="http://www.antlr.org/" target="_blank">ANTLR</a> library and the related ANTLRWorks <a href="http://martinfowler.com/articles/languageWorkbench.html" target="_blank">language workbench</a>.</p>
<h2>The grammar:</h2>
<p>Here is the grammar presented in full. Lets analyze it one piece at a time. (Some parts of the grammar are based on <a href="http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3" target="_blank">examples</a> from ANTLR. You will find it easier to delve into documentation that way.  )</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">grammar SimpleCalc<span style="color: #339933;">;</span>
&nbsp;
@header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
@members <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setAns<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> num1, <span style="color: #003399;">String</span> num2<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> a <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Integer</span> b <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&gt;</span>b<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;no&quot;</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;
 @lexer<span style="color: #339933;">::</span>header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
is    returns <span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span> expr<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span>    <span style="color: #0000ff;">'IS'</span> 
                <span style="color: #009900;">&#40;</span>
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'BIGGERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$a.<span style="color: #006633;">text</span>,$b.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
                <span style="color: #339933;">|</span> 
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'SMALLERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$b.<span style="color: #006633;">text</span>,$a.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>$expr<span style="color: #339933;">=</span>answer<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
NUMBER    <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>DIGIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #339933;">;</span>
&nbsp;
WHITESPACE <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\u</span>000C'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>     <span style="color: #009900;">&#123;</span> $channel <span style="color: #339933;">=</span> HIDDEN<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">;</span>
&nbsp;
fragment DIGIT    <span style="color: #339933;">:</span> <span style="color: #0000ff;">'0'</span>..<span style="color: #0000ff;">'9'</span> <span style="color: #339933;">;</span></pre></div></div>

<p><strong>Grammar definition:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">grammar SimpleCalc<span style="color: #339933;">;</span>
&nbsp;
@header <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.antlr</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This defines the name of the grammar and the header under which the code needs to be generated. Lexer and parser code can be generated under different packages.</p>
<p><strong>Custom java code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@members <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setAns<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> num1, <span style="color: #003399;">String</span> num2<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> a <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Integer</span> b <span style="color: #339933;">=</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>num2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">&gt;</span>b<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yes&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #009900;">&#123;</span>
            answer <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;no&quot;</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></div></div>

<p>This is a piece of java code. It is embedded into the parser and can be called when a rule is executed. We maintain a private String by the name &#8216;answer&#8217;. When the rule that compares two numbers is invoked, the setAns() method can be called with the numbers in order to compare them.</p>
<p><strong>Lexer rules:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
NUMBER    <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>DIGIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #339933;">;</span>
&nbsp;
WHITESPACE <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\r</span>'</span> <span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">|</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\u</span>000C'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>     <span style="color: #009900;">&#123;</span> $channel <span style="color: #339933;">=</span> HIDDEN<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #339933;">;</span>
&nbsp;
fragment DIGIT    <span style="color: #339933;">:</span> <span style="color: #0000ff;">'0'</span>..<span style="color: #0000ff;">'9'</span> <span style="color: #339933;">;</span></pre></div></div>

<p>In the lexer section, we identify the bits of the input grammar that the parser can make sense of. A DIGIT ranges from 0-9. When the parser encounters one or more DIGITs, it represents a NUMBER. We also tell ANTLR that any whitespace needs to ignored.</p>
<p><strong>Parser rules:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/</span>
&nbsp;
is    returns <span style="color: #009900;">&#91;</span><span style="color: #003399;">String</span> expr<span style="color: #009900;">&#93;</span><span style="color: #339933;">:</span>    <span style="color: #0000ff;">'IS'</span> 
                <span style="color: #009900;">&#40;</span>
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'BIGGERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$a.<span style="color: #006633;">text</span>,$b.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
                <span style="color: #339933;">|</span> 
                a<span style="color: #339933;">=</span>NUMBER <span style="color: #0000ff;">'SMALLERTHAN'</span> b<span style="color: #339933;">=</span>NUMBER <span style="color: #009900;">&#123;</span>setAns<span style="color: #009900;">&#40;</span>$b.<span style="color: #006633;">text</span>,$a.<span style="color: #006633;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>$expr<span style="color: #339933;">=</span>answer<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The parser section of the grammar, defines a &#8216;is&#8217; operation that returns a String. The pattern to match is &#8216;IS NUMBER &#8216;BIGGERTHAN&#8217; | &#8216;SMALLERTHAN&#8217; NUMBER&#8217;. The numbers are stored in variables a and b. The code inside the {} brackets is embedded into the parser, just like the code inside @members. As such this code can call the methods in @members and is executed when an appropriate match is made.</p>
<p>The match &#8216;IS NUMBER BIGGERTHAN NUMBER&#8217; results in the following call setAns($a.text,$b.text);. When the match is for the &#8216;SMALLERTHAN&#8217; input, simply reverse the numbers in the method call.</p>
<h2>Running the pieces:</h2>
<p>Now that the grammar is in place, it can be debugged in ANTLRWorks or code-gened and ported to a java project. Debugging in ANTLRWorks reveals interesting internal details. Once can visualize how ANTLR goes about generating a parse tree for our grammar or what the syntax tree for the &#8216;is&#8217; rule looks like.</p>
<p><strong>ANTLRWorks:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_1.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_1.png" alt="" title="antlr_1" width="735" height="356" class="aligncenter size-full wp-image-1004" /></a></p>
<p><strong>Syntax tree:</strong><br />
<a href="http://www.certpal.com/blogs/wp-content/uploads/antlr_2.png"><img src="http://www.certpal.com/blogs/wp-content/uploads/antlr_2.png" alt="" title="antlr_2" width="634" height="101" class="aligncenter size-full wp-image-1005" /></a></p>
<p>The code-gen route allows one to execute the parser for a user defined input.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.antlr.runtime.ANTLRStringStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.antlr.runtime.CommonTokenStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CalcTest
<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: #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> CalcTest<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;">try</span>
        <span style="color: #009900;">&#123;</span>
            SimpleCalcLexer lex <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCalcLexer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ANTLRStringStream<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;IS 19 SMALLERTHAN 20&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            CommonTokenStream tokens <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CommonTokenStream<span style="color: #009900;">&#40;</span>lex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            SimpleCalcParser parser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleCalcParser<span style="color: #009900;">&#40;</span>tokens<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> eval <span style="color: #339933;">=</span> parser.<span style="color: #006633;">is</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>eval<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;">Exception</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></pre></div></div>

<p>Modifying bits of the program above would allow the input to be obtained from the console. So my neighbor types in &#8216;IS 20 BIGGERTHAN 19&#8242; and is happy with the result. Now he wants me to make a language that will print multiplication tables. His homework is due this week <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><script type="text/javascript">var dzone_url = 'http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/ ‎';</script><br />
<script type="text/javascript">var dzone_title = 'Hello Antlr';</script><br />
<script type="text/javascript">var dzone_blurb = 'An introduction / overview to ANTLR, the parser generator. We take a look at grammar that allows a computer to determine whether number A is bigger than number B. Not exactly the most complex operation in the world, but it makes for an easy intro to ANTLR.';</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>
<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%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2011%2F01%2Fantlr-tutorial-hello-antlr%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</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>
<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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fgoogle-wave-java-robot-overview%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/10/google-wave-java-robot-overview/feed/</wfw:commentRss>
		<slash:comments>7</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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</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"><div 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></div></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"><div 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></div></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"><div class="code"><pre class="java" style="font-family:monospace;">Waiting
Inerrupted</pre></div></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"><div 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></div></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.
<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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F10%2Fjava-thread-tutorial%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/10/java-thread-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

