<?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>Grayger &#187; Java</title>
	<atom:link href="http://www.grayger.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.grayger.com</link>
	<description>In the pursuit of effectiveness</description>
	<lastBuildDate>Sat, 02 Jul 2011 16:38:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hidden Features of Java</title>
		<link>http://www.grayger.com/java-eng/hidden-features-of-java/</link>
		<comments>http://www.grayger.com/java-eng/hidden-features-of-java/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 15:58:31 +0000</pubDate>
		<dc:creator>grayger</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.grayger.com/wp/?p=51</guid>
		<description><![CDATA[A few days ago, I registered stackoverflow.com and found an interesting post: what are hidden features of Java?
The stuffs that I didn&#8217;t know before are

Double brace initialization


	Map map=new HashMap&#60;String,String&#62;() {
		{
			put(&#34;key1&#34;,&#34;value1&#34;);
			put(&#34;key2&#34;,&#34;value2&#34;);
		}
	};

Instance initializer


public class Initializer {
	static {
		System.out.println(&#34;static&#34;);
	}
	{
		System.out.println(&#34;instance&#34;);
	}
	public Initializer() {
		System.out.println(&#34;constructor&#34;);
	}
}

Covariant return type


	abstract class Base {
		abstract List getList();
	}

	class Sub extends Base {
		@Override
		ArrayList getList() {
			return null;
		}
	}


Double brace initialization uses instance [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I registered stackoverflow.com and found an interesting post: <a href="http://stackoverflow.com/questions/15496/hidden-features-of-java">what are hidden features of Java?</a></p>
<p>The stuffs that I didn&#8217;t know before are</p>
<ul>
<li>Double brace initialization</li>
<pre class="brush: java; ">

	Map map=new HashMap&lt;String,String&gt;() {
		{
			put(&quot;key1&quot;,&quot;value1&quot;);
			put(&quot;key2&quot;,&quot;value2&quot;);
		}
	};
</pre>
<li>Instance initializer</li>
<pre class="brush: java; ">

public class Initializer {
	static {
		System.out.println(&quot;static&quot;);
	}
	{
		System.out.println(&quot;instance&quot;);
	}
	public Initializer() {
		System.out.println(&quot;constructor&quot;);
	}
}
</pre>
<li>Covariant return type</li>
<pre class="brush: java; ">

	abstract class Base {
		abstract List getList();
	}

	class Sub extends Base {
		@Override
		ArrayList getList() {
			return null;
		}
	}
</pre>
</ul>
<p>Double brace initialization uses instance initializer because first brace is for declairing annomymous inner class and second brace is for instance initializer block.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.grayger.com/java-eng/hidden-features-of-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java VM Optimization</title>
		<link>http://www.grayger.com/java-eng/32/</link>
		<comments>http://www.grayger.com/java-eng/32/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 14:36:54 +0000</pubDate>
		<dc:creator>grayger</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.grayger.com/wp/?p=32</guid>
		<description><![CDATA[I saw a presentation on Java VM optimization via InfoQ. Java is originally slow, but HotSpot compiler is getting smarter enough to free Java from the shame.
http://www.infoq.com/presentations/pampuch-vm-optimizations-language-designers
]]></description>
			<content:encoded><![CDATA[<p>I saw a presentation on Java VM optimization via InfoQ. Java is originally slow, but HotSpot compiler is getting smarter enough to free Java from the shame.</p>
<p><a href="http://www.infoq.com/presentations/pampuch-vm-optimizations-language-designers">http://www.infoq.com/presentations/pampuch-vm-optimizations-language-designers</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.grayger.com/java-eng/32/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java deadlock detection</title>
		<link>http://www.grayger.com/java-eng/java-deadlock-detection/</link>
		<comments>http://www.grayger.com/java-eng/java-deadlock-detection/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 16:27:44 +0000</pubDate>
		<dc:creator>grayger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Deadlock]]></category>
		<category><![CDATA[Profiler]]></category>

		<guid isPermaLink="false">http://www.grayger.com/wp/?p=23</guid>
		<description><![CDATA[Detecting deadlock is one of common features of Java profiling tools but I haven&#8217;t experienced the situation where those tools detect deadlock from code I wrote. In fact, deadlock is hazardous because it is difficult to predict and reproduce.
I wondered under what conditions do profilers detect deadlock and then report it, so I wrote code [...]]]></description>
			<content:encoded><![CDATA[<p>Detecting deadlock is one of common features of Java profiling tools but I haven&#8217;t experienced the situation where those tools detect deadlock from code I wrote. In fact, deadlock is hazardous because it is difficult to predict and reproduce.</p>
<p>I wondered under what conditions do profilers detect deadlock and then report it, so I wrote code that reaches deadlock.</p>
<pre class="brush: java; ">

package com.grayger.deadlock;

public class SimpleDeadlocker {
	private final static Object OBJ1=new Object();
	private final static Object OBJ2=new Object();

	private final static int LOOP=100;

	public void obj1First() {
		synchronized(OBJ1) {
			printInfo(&quot;Hold OBJ1&quot;);
			synchronized(OBJ2) {
				printInfo(&quot;Hole OBJ2&quot;);
			}
			printInfo(&quot;Release OBJ2&quot;);
		}
		printInfo(&quot;Release OBJ1&quot;);
	}

	public void obj2First() {
		synchronized(OBJ2) {
			printInfo(&quot;Hold OBJ2&quot;);
			synchronized(OBJ1) {
				printInfo(&quot;Hole OBJ1&quot;);
			}
			printInfo(&quot;Release OBJ1&quot;);
		}
		printInfo(&quot;Release OBJ2&quot;);
	}

	private void printInfo(String info) {
		System.out.println(Thread.currentThread().getName()+&quot; &quot;+info);
	}

	public static void main(String args[]) {
		final SimpleDeadlocker d=new SimpleDeadlocker();
		Thread t1=new Thread(new Runnable() {
			public void run() {
				for(int i=0;i&lt;loop;i++)&gt;
					d.obj1First();
				}
				d.printInfo(&quot;Terminated&quot;);
			}
		}, &quot;OBJ1_First&quot;);

		Thread t2=new Thread(new Runnable() {
			public void run() {
				for(int i=0;i&lt;loop;i++)&gt;
					d.obj2First();
				}
				d.printInfo(&quot;Terminated&quot;);
			}
		}, &quot;OBJ2_First&quot;);

		t1.start();
		t2.start();
	}
}
</pre>
<p>Above example can be refactored as follows:</p>
<pre class="brush: java; ">

package com.grayger.deadlock;

public class Deadlocker {
	private final static int LOOP=100;

	static class Deadlock1 {
		private final static Object OBJ1=new Object();
		private final static Object OBJ2=new Object();		

		public void obj1First() {
			synchronized(OBJ1) {
				printInfo(&quot;Hold OBJ1&quot;);
				synchronized(OBJ2) {
					printInfo(&quot;Hole OBJ2&quot;);
				}
				printInfo(&quot;Release OBJ2&quot;);
			}
			printInfo(&quot;Release OBJ1&quot;);
		}
	}

	static class Deadlock2 {
		private final static Object OBJ1=new Object();
		private final static Object OBJ2=new Object();

		public void obj2First() {
			synchronized(OBJ2) {
				printInfo(&quot;Hold OBJ2&quot;);
				synchronized(OBJ1) {
					printInfo(&quot;Hole OBJ1&quot;);
				}
				printInfo(&quot;Release OBJ1&quot;);
			}
			printInfo(&quot;Release OBJ2&quot;);
		}
	}

	public static void printInfo(String info) {
		System.out.println(Thread.currentThread().getName()+&quot; &quot;+info);
	}

	public static void main(String args[]) {
		final Deadlock1 d1=new Deadlock1();
		final Deadlock2 d2=new Deadlock2();
		Thread t1=new Thread(new Runnable() {
			public void run() {
				for(int i=0;i&lt;loop;i++)&gt;
					d1.obj1First();
				}
				Deadlocker.printInfo(&quot;Terminated&quot;);
			}
		}, &quot;OBJ1_First&quot;);

		Thread t2=new Thread(new Runnable() {
			public void run() {
				for(int i=0;i&lt;loop;i++)&gt;
					d2.obj2First();
				}
				Deadlocker.printInfo(&quot;Terminated&quot;);
			}
		}, &quot;OBJ2_First&quot;);

		t1.start();
		t2.start();
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.grayger.com/java-eng/java-deadlock-detection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>@Override annotation in Java6</title>
		<link>http://www.grayger.com/java-eng/override-annotation-in-java6/</link>
		<comments>http://www.grayger.com/java-eng/override-annotation-in-java6/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 16:10:32 +0000</pubDate>
		<dc:creator>grayger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Annotation]]></category>

		<guid isPermaLink="false">http://www.grayger.com/wp/?p=31</guid>
		<description><![CDATA[The @Override annotation added in Java5 is very useful. Recently I found out the annotation of Java6 differs from that of Java5. Java6 allows annotating a method that implements a method declared in interface with @Override whereas Java5 doesn&#8217;t allow it.
Unfortunately I couldn&#8217;t find any document on the change from java.sun.com site. Missing this issue [...]]]></description>
			<content:encoded><![CDATA[<p>The @Override annotation added in Java5 is very useful. Recently I found out the annotation of Java6 differs from that of Java5. Java6 allows annotating a method that implements a method declared in interface with @Override whereas Java5 doesn&#8217;t allow it.</p>
<p>Unfortunately I couldn&#8217;t find any document on the change from java.sun.com site. Missing this issue from the release note of Java6 seems to be a mistake as <a title="http://blogs.sun.com/ahe/entry/override_snafu" href="http://blogs.sun.com/ahe/entry/override_snafu">http://blogs.sun.com/ahe/entry/override_snafu</a> says.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.grayger.com/java-eng/override-annotation-in-java6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

