<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mete Atamel</title>
	<atom:link href="http://meteatamel.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://meteatamel.wordpress.com</link>
	<description>A blog about software development</description>
	<lastBuildDate>Fri, 25 May 2012 09:53:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='meteatamel.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mete Atamel</title>
		<link>http://meteatamel.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://meteatamel.wordpress.com/osd.xml" title="Mete Atamel" />
	<atom:link rel='hub' href='http://meteatamel.wordpress.com/?pushpress=hub'/>
		<item>
		<title>When a daemon thread is not so daemon</title>
		<link>http://meteatamel.wordpress.com/2012/05/22/when-a-daemon-thread-is-not-so-daemon/</link>
		<comments>http://meteatamel.wordpress.com/2012/05/22/when-a-daemon-thread-is-not-so-daemon/#comments</comments>
		<pubDate>Tue, 22 May 2012 16:38:54 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=851</guid>
		<description><![CDATA[As you know, threads in Java can be marked as daemon via Thread#setDaemon. The main difference between a normal vs. daemon thread is that the JVM exits when the only threads running are all daemon threads. Daemon threads are usually used as service providers for normal threads running in your application as they don&#8217;t affect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=851&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As you know, threads in Java can be marked as daemon via <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#setDaemon%28boolean%29" target="_blank">Thread#setDaemon</a>. The main difference between a normal vs. daemon thread is that the JVM exits when the only threads running are all daemon threads. Daemon threads are usually used as service providers for normal threads running in your application as they don&#8217;t affect the shutdown of your application like a normal thread might do.</p>
<p>So, you might be tempted to mark a thread as daemon and blindly assume that it will never block the shutdown of your application. While this is true in most cases, as always, there are exceptions. Consider the following piece of code:<br />
<pre class="brush: java; gutter: false;">
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    System.out.println(i++);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.setDaemon(true);
        t.start();

        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(&quot;end&quot;);
    }
</pre></p>
<p>A daemon thread is created which simply prints a counter every second. Then, there&#8217;s a call to <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29" target="_blank">Thread#join</a> to wait for the daemon thread to finish and then the main thread prints &#8220;end&#8221;. Well, &#8220;end&#8221; will never be printed and the application will not exit because the daemon thread will continue printing counter and <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29" target="_blank">Thread#join</a> will block forever. This might be surprising at first glance. Isn&#8217;t this a daemon thread? Isn&#8217;t it supposed to not block JVM? Yes, but calling Thread#join on a daemon thread effectively turns the daemon thread into a normal thread.</p>
<p>The lesson here is that marking a thread as daemon is only part of the story. How a daemon thread is used throughout your application is also as important and you need to keep that in mind. Another lesson is that don&#8217;t use things that can block forever such as <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29" target="_blank">Thread#join</a>. I know you designed your application so perfect that you think it will never block but if something can block in theory, it will block in production sooner or later. That can be quite annoying to customers and support people, so those people before blindly implementing blocking code paths. Instead use non-blocking alternatives like <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28long%29" target="_blank">Thread#join(long)</a> and provide a timeout.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/java/'>Java</a>, <a href='http://meteatamel.wordpress.com/category/threading/'>Threading</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/851/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/851/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/851/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=851&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/05/22/when-a-daemon-thread-is-not-so-daemon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>Origin null is not allowed by Access-Control-Allow-Origin</title>
		<link>http://meteatamel.wordpress.com/2012/05/15/origin-null-is-not-allowed-by-access-control-allow-origin/</link>
		<comments>http://meteatamel.wordpress.com/2012/05/15/origin-null-is-not-allowed-by-access-control-allow-origin/#comments</comments>
		<pubDate>Tue, 15 May 2012 14:59:35 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Chrome]]></category>
		<category><![CDATA[HTTP]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=853</guid>
		<description><![CDATA[I got this error in Chrome while testing out a JQuery Mobile application. The problem was that JQuery Mobile app&#8217;s index.html was trying to open another local HTML file using &#8220;&#60;a href=&#34;foo.html &#8230;&#34;. Opening local files is not allowed by default in Chrome (but works in Firefox and Safari). This is the kind of error [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=853&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I got this error in Chrome while testing out a JQuery Mobile application. The problem was that JQuery Mobile app&#8217;s index.html was trying to open another local HTML file using &#8220;&lt;a href=&quot;foo.html &#8230;&quot;. Opening local files is not allowed by default in Chrome (but works in Firefox and Safari). This is the kind of error you&#039;d get in Chrome:</p>
<p><pre class="brush: plain; gutter: false;">
XMLHttpRequest cannot load file://.../foo.html. 
Origin null is not allowed by Access-Control-Allow-Origin.
</pre></p>
<p>To get rid of the error, you can enable local files in Chrome by adding &quot;&#8211;allow-file-access-from-files&quot; option (<a href="http://stackoverflow.com/questions/4208530/xmlhttprequest-origin-null-is-not-allowed-access-control-access-allow-for-file" target="_blank">as explained here</a>). </p>
<p>On Mac, I do this but running Chrome using the following command from terminal:<br />
<pre class="brush: plain; gutter: false;">
open /Applications/Google\ Chrome.app --args --allow-file-access-from-files
</pre></p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/chrome/'>Chrome</a>, <a href='http://meteatamel.wordpress.com/category/http/'>HTTP</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/853/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/853/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/853/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=853&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/05/15/origin-null-is-not-allowed-by-access-control-allow-origin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>Maintaining Legacy Systems</title>
		<link>http://meteatamel.wordpress.com/2012/04/25/maintaining-legacy-systems/</link>
		<comments>http://meteatamel.wordpress.com/2012/04/25/maintaining-legacy-systems/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 15:49:43 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=805</guid>
		<description><![CDATA[There are a lot of books, blog posts, articles on programming languages, frameworks, design patterns for writing brand new software but not so much on debugging and maintaining legacy systems. This is surprising because a big part of software development is about maintaining legacy systems. It&#8217;s understandable though. Legacy is not exciting but new is, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=805&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://meteatamel.files.wordpress.com/2012/04/imag0117.jpg"><img class="aligncenter size-medium wp-image-847" title="Sagrada Familia" src="http://meteatamel.files.wordpress.com/2012/04/imag0117.jpg?w=179&h=300" alt="" width="179" height="300" /></a></p>
<p>There are a lot of books, blog posts, articles on programming languages, frameworks, design patterns for writing brand <strong>new</strong> software but not so much on debugging and maintaining legacy systems. This is surprising because a big part of software development is about maintaining legacy systems.</p>
<p>It&#8217;s understandable though. Legacy is not exciting but new is, so everybody wants to talk about new things. That&#8217;s great but there are a lot of developers in trenches like myself who spend a lot of time working on non-exciting yet essential work like maintaining legacy systems and I feel like we could use some knowledge sharing.</p>
<p><strong>What&#8217;s a legacy system?</strong><br />
By legacy systems, I don&#8217;t mean some ancient code written in 1980s that you have to maintain (hopefully you don&#8217;t!) but rather I mean code that was designed and implemented before your time in your current role. I call these legacy systems from your perspective because you had no input into their design and implementation. You were basically late to the party where all cool design and implementation choices were made and now you are stuck with understanding and maintaining someone else&#8217;s thought and implementation flaws. If that person is still around, you&#8217;re somewhat lucky. At least, there&#8217;s someone to ask questions but most of the time, you&#8217;re left on your own to understand what&#8217;s going on.</p>
<p><strong>Debugging</strong><br />
Debugging is a prerequisite for understanding and maintaining a legacy system. No matter how simple or complicated a system might be, you will need to step through code to get a general feel for the system. My ex-colleague Jeff Vroom recently had <a href="http://blog.jvroom.com/2012/02/08/debugging-hard-problems" target="_blank">a great post on debugging</a> that covers everything I&#8217;d say on that topic.</p>
<p><strong>Maintenance Guidelines</strong><br />
Once debugging is done and you have a good understanding of the system, you need to fix/maintain the system. These are some guidelines I try to remind myself as I&#8217;m working with legacy systems.</p>
<p><span style="text-decoration:underline;">Have the right mindset</span><br />
As programmers, we are very particular about how we do things. So quite often, we find someone else&#8217;s code ugly, stupid, convoluted, hard to maintain, etc. See code as code only and focus on the problem you&#8217;re trying to solve. Nobody cares about how beautiful or ugly the code is according to you but a lot of customers would benefit from the bug fix or feature you&#8217;re working on, so make sure you remember that.</p>
<p><span style="text-decoration:underline;">Keep bug fixes simple</span><br />
When fixing a bug in a legacy system, try to keep it as simple as possible. The smaller the fix, the easier it&#8217;ll be for others to understand it and the easier it&#8217;ll be to merge to other branches. Even though the fix might not be theoretically perfect or up to your standards in terms of style, that&#8217;s fine as long as it covers the use case you&#8217;re trying to fix. In my experience, the benefits of easily understandable and contained bug fixes outweigh any style or theoretical considerations most of the time.</p>
<p><span style="text-decoration:underline;">Add new functionality as a cohesive unit</span><br />
When you&#8217;re working on a feature in a legacy system, try to add the new functionality as a cohesive unit. Don&#8217;t scatter your new feature throughout the system. If possible, create new methods within the class you&#8217;re editing, or new classes within the system. The more you isolate what you add, the easier time you and others in your team will have later. You&#8217;ll be able to test what you added specifically, you&#8217;ll be able to take out your code and see if a particular bug still happens and so on.</p>
<p><span style="text-decoration:underline;">Resist the urge to refactor</span><br />
I know if you were there for the initial design and implementation, everything would be better and I know that the code you&#8217;re looking at now is not what you want to look at. But despite all that, resist the urge to refactor unless it&#8217;s really needed. I&#8217;m not saying you shouldn&#8217;t change code at all. You should always follow &#8220;Leave the code better than you found it&#8221; principle but do it incrementally, so people can track what you&#8217;re doing. If you refactor bunch of classes in a major way all at the same time, nobody will be able to grasp what you just did. If you&#8217;re lucky, there are good tests to catch any errors you introduced but most of the time, that&#8217;s not the case, so it&#8217;s a big risk to refactor a legacy system in a major way at the same time because you will most certainly introduce errors. Instead accept the system for what it is and do incremental changes. If done consistently, small incremental changes over a period of time do wonders to your legacy code base.</p>
<p><span style="text-decoration:underline;">Ask for help but strive for independence</span><br />
Don&#8217;t spend hours and hours trying to understand the logic behind some piece of code, when the original programmer could explain everything in 10 minutes. I know it could be time-consuming and somewhat annoying for the original programmer to spend time explaining his code, but hey, you&#8217;re doing them a favor by maintaining their code, so the least they can do is to spend some time in explaining their code. Having said that, your goal is to gradually reduce your dependency on the original programmer. If you sincerely put in the effort to understand the system, it&#8217;s just a matter of time.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/programming/'>Programming</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/805/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/805/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/805/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=805&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/04/25/maintaining-legacy-systems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>

		<media:content url="http://meteatamel.files.wordpress.com/2012/04/imag0117.jpg?w=179" medium="image">
			<media:title type="html">Sagrada Familia</media:title>
		</media:content>
	</item>
		<item>
		<title>Thread.sleep vs. Object.wait</title>
		<link>http://meteatamel.wordpress.com/2012/04/16/thread-sleep-vs-object-wait/</link>
		<comments>http://meteatamel.wordpress.com/2012/04/16/thread-sleep-vs-object-wait/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 09:00:24 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=817</guid>
		<description><![CDATA[In my last 2 posts (here and here), I talked about some simple but overlooked topics in Java threading. I want to wrap up this series by pointing out yet another easily overlooked topic related to threading: the subtle difference between Thread.sleep and Object.wait. In Java, both Thread.sleep and Object.wait make the current thread wait [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=817&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my last 2 posts (<a href="http://meteatamel.wordpress.com/2012/04/09/block-deadlock/" title="Block != Deadlock" target="_blank">here</a> and <a href="http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/" title="Deadlock Detection in Java" target="_blank">here</a>), I talked about some simple but overlooked topics in Java threading. I want to wrap up this series by pointing out yet another easily overlooked topic related to threading: the subtle difference between Thread.sleep and Object.wait.</p>
<p>In Java, both Thread.sleep and Object.wait make the current thread wait for a specified amount of time. This is useful when the current thread needs to wait for some other thread before it can proceed. I sometimes see developers use these interchangeably in close proximity in code but this can be problematic as the two methods are quite different.</p>
<p>Consider the example from my previous <a href="http://meteatamel.wordpress.com/2012/04/09/block-deadlock/" title="Block != Deadlock" target="_blank">post</a>. In that example, thread1 acquired a lock, slept for a minute while thread2 was blocked waiting for the same lock. It used TimeUnit.MINUTES.sleep(1) which uses Thread.sleep to make thread1 wait. Let&#8217;s change it to use Object.wait and see what happens:</p>
<p><pre class="brush: java; gutter: false;">
private static void test() {
    final Object lock = new Object();

    Thread thread1 = new Thread(new Runnable() {
        @Override public void run() {
            synchronized (lock) {
                System.out.println(&quot;Thread1 acquired lock&quot;);
                try {
                    lock.wait(1 * 60 * 1000);
                    //TimeUnit.MINUTES.sleep(1);
                } catch (InterruptedException ignore) {}
            }
        }

    });
    thread1.start();

    Thread thread2 = new Thread(new Runnable() {
        @Override public void run() {
            synchronized (lock) {
                System.out.println(&quot;Thread2 acquired lock&quot;);
            }
        }
    });
    thread2.start();
}
</pre><br />
Once you run this sample, you&#8217;ll see the following right away:<br />
<pre class="brush: plain; gutter: false;">
Thread1 acquired lock
Thread2 acquired lock
</pre> </p>
<p>So what happened? When thread1 went to sleep, it released the lock and thread2 acquired it right away. Unlike Thread.sleep (where locks are not released) in Object.wait, locks are released as the thread goes to sleep. In some situations, this might be appropriate, in others, maybe not, so this is an important thing to keep in mind when deciding which sleep method to use.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/java/'>Java</a>, <a href='http://meteatamel.wordpress.com/category/threading/'>Threading</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/817/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=817&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/04/16/thread-sleep-vs-object-wait/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>Block != Deadlock</title>
		<link>http://meteatamel.wordpress.com/2012/04/09/block-deadlock/</link>
		<comments>http://meteatamel.wordpress.com/2012/04/09/block-deadlock/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 09:56:59 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=809</guid>
		<description><![CDATA[I sometimes hear programmers use the terms &#8220;threads are blocked&#8221; and &#8220;threads are deadlocked&#8221; interchangeably even though block and deadlock are two very different notions. So, in this post, I want to briefly highlight the difference between blocks and deadlocks to set the record straight. In my previous post, I talked about thread deadlocks. Thread [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=809&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I sometimes hear programmers use the terms &#8220;threads are blocked&#8221; and &#8220;threads are deadlocked&#8221; interchangeably even though block and deadlock are two very different notions. So, in this post, I want to briefly highlight the difference between blocks and deadlocks to set the record straight.</p>
<p><a href="http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/" title="Deadlock Detection in Java" target="_blank">In my previous post</a>, I talked about thread deadlocks. Thread blocks, on the other hand, happen when one thread acquires a lock and holds onto it while the second thread waits for the same lock. In this case, the second thread is blocked until the first thread releases the lock. Here&#8217;s a quick example:<br />
<pre class="brush: java; gutter: false;">
    private static void test() {
        final Object lock = new Object();

        Thread thread1 = new Thread(new Runnable() {
            @Override public void run() {
                synchronized (lock) {
                    System.out.println(&quot;Thread1 acquired lock&quot;);
                    try {
                        TimeUnit.MINUTES.sleep(1);
                    } catch (InterruptedException ignore) {}
                }
            }

        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override public void run() {
                synchronized (lock) {
                    System.out.println(&quot;Thread2 acquired lock&quot;);
                }
            }
        });
        thread2.start();
    }
</pre></p>
<p>Notice how Thread1 acquires the lock and does not release it for a minute. Once you run this sample, you&#8217;ll see &#8220;Thread1 acquired lock&#8221; but Thread2 does not acquire the lock for a whole minute. In that 1 minute, you can see that Thread2 is blocked in the JVM thread dump:<br />
<pre class="brush: java; gutter: false;">
&quot;Thread-2&quot; prio=5 tid=101978000 nid=0x10a704000 waiting for monitor entry [10a703000]
   java.lang.Thread.State: BLOCKED (on object monitor)
	at BlockTest$2.run(BlockTest.java:32)
	- waiting to lock &lt;7f3112a18&gt; (a java.lang.Object)
	at java.lang.Thread.run(Thread.java:680)
</pre></p>
<p>Thread2 will eventually be unblocked, in this case, after 1 minute. So, unlike deadlocks, there&#8217;s some hope in blocks but to the end user deadlocks and blocks basically manifest themselves as unresponsive application, especially if the blocks are too frequent and long. </p>
<p>However, there is no excuse for programmers to confuse blocks and deadlocks as they are 2 completely different notions <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/java/'>Java</a>, <a href='http://meteatamel.wordpress.com/category/threading/'>Threading</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/809/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/809/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/809/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=809&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/04/09/block-deadlock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>Deadlock Detection in Java</title>
		<link>http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/</link>
		<comments>http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 16:58:48 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=742</guid>
		<description><![CDATA[In this post, I want to talk about deadlock detection in Java and some pitfalls you might run into. What is a deadlock? Every programmer knows what a deadlock is but for completeness sake, I&#8217;ll give a brief description. Imagine you have two threads, thread1 and thread2. Thread1 acquires lock1 and is about to acquire [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=742&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post, I want to talk about deadlock detection in Java and some pitfalls you might run into.</p>
<p><strong>What is a deadlock?</strong></p>
<p>Every programmer knows what a deadlock is but for completeness sake, I&#8217;ll give a brief description. Imagine you have two threads, thread1 and thread2. Thread1 acquires lock1 and is about to acquire lock2 while thread2 acquires lock2 and is about to acquire lock1. In this case, threads are deadlocked because they are each waiting for the other thread to release the other lock and your application just hangs. Deadlocks usually occur when the order of locks is not consistent throughout the application.</p>
<p><strong>Deadlock sample with regular sync blocks</strong></p>
<p>Here is a a quick sample that shows the deadlock situation explained above with good old sync blocks.<br />
<pre class="brush: java; gutter: false;">
    private static void test1() {
        final Object lock1 = new Object();
        final Object lock2 = new Object();

        Thread thread1 = new Thread(new Runnable() {
            @Override public void run() {
                synchronized (lock1) {
                    System.out.println(&quot;Thread1 acquired lock1&quot;);
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                    } catch (InterruptedException ignore) {}
                    synchronized (lock2) {
                        System.out.println(&quot;Thread1 acquired lock2&quot;);
                    }
                }
            }

        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override public void run() {
                synchronized (lock2) {
                    System.out.println(&quot;Thread2 acquired lock2&quot;);
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                    } catch (InterruptedException ignore) {}
                    synchronized (lock1) {
                        System.out.println(&quot;Thread2 acquired lock1&quot;);
                    }
                }
            }
        });
        thread2.start();

        // Wait a little for threads to deadlock.
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException ignore) {}
    }
</pre></p>
<p>The sample prints the following and then just hangs due to deadlocked threads:<br />
<pre class="brush: java; gutter: false;">
Thread1 acquired lock1
Thread2 acquired lock2
</pre></p>
<p><strong>Manual Deadlock Detection</strong></p>
<p>While your app is hanging like in the example above, you can get a thread dump and see the deadlocked threads. For example, on Mac, you can either do Ctrl-\ or simply use jstack and process id to get the thread dump which makes it very obvious where the deadlock is. In this example, the thread dump looks like this:<br />
<pre class="brush: java; gutter: false;">
Found one Java-level deadlock:
=============================
&quot;Thread-2&quot;:
  waiting to lock monitor 102054308 (object 7f3113800, a java.lang.Object),
  which is held by &quot;Thread-1&quot;
&quot;Thread-1&quot;:
  waiting to lock monitor 1020348b8 (object 7f3113810, a java.lang.Object),
  which is held by &quot;Thread-2&quot;

Java stack information for the threads listed above:
===================================================
&quot;Thread-2&quot;:
	at DeadlockTest$2.run(DeadlockTest.java:42)
	- waiting to lock &lt;7f3113800&gt; (a java.lang.Object)
	- locked &lt;7f3113810&gt; (a java.lang.Object)
	at java.lang.Thread.run(Thread.java:680)
&quot;Thread-1&quot;:
	at DeadlockTest$1.run(DeadlockTest.java:26)
	- waiting to lock &lt;7f3113810&gt; (a java.lang.Object)
	- locked &lt;7f3113800&gt; (a java.lang.Object)
	at java.lang.Thread.run(Thread.java:680)

Found 1 deadlock.
</pre></p>
<p><strong>Programmatic Deadlock Detection</strong></p>
<p>In more recent JDKs, there&#8217;s programmatic deadlock detection where you can get more information about the deadlocked threads and even stacktraces leading to deadlock as explained in <a href="http://stackoverflow.com/questions/1102359/programmatic-deadlock-detection-in-java" target="_blank">this stackoverflow.com entry</a>. In our example, let&#8217;s just detect the number of blocked threads in our application:<br />
<pre class="brush: java; gutter: false;">
    private static void detectDeadlock() {
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadBean.findMonitorDeadlockedThreads();
        int deadlockedThreads = threadIds != null? threadIds.length : 0;
        System.out.println(&quot;Number of deadlocked threads: &quot; + deadlockedThreads);
    }
</pre></p>
<p>If you call detectDeadlock to the end of the test1 method, you get the following nice output with the number of deadlocked threads:<br />
<pre class="brush: plain; gutter: false;">
Thread1 acquired lock1
Thread2 acquired lock2
Number of deadlocked threads: 2
</pre></p>
<p><strong>Deadlock sample with other locks</strong></p>
<p>This is all nice but what about advanced locks like ReentrantLock, does programmatic detection work in that case? Let&#8217;s use this sample:<br />
<pre class="brush: java; gutter: false;">
    private static void test2() {
        final ReentrantLock lock1 = new ReentrantLock();
        final ReentrantLock lock2 = new ReentrantLock();

        Thread thread1 = new Thread(new Runnable() {
            @Override public void run() {
                try {
                    lock1.lock();
                    System.out.println(&quot;Thread1 acquired lock1&quot;);
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                    } catch (InterruptedException ignore) {}
                    lock2.lock();
                    System.out.println(&quot;Thread1 acquired lock2&quot;);
                }
                finally {
                    lock2.unlock();
                    lock1.unlock();
                }
            }
        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override public void run() {
                try {
                    lock2.lock();
                    System.out.println(&quot;Thread2 acquired lock2&quot;);
                    try {
                        TimeUnit.MILLISECONDS.sleep(50);
                    } catch (InterruptedException ignore) {}
                    lock1.lock();
                    System.out.println(&quot;Thread2 acquired lock1&quot;);
                }
                finally {
                    lock1.unlock();
                    lock2.unlock();
                }
            }
        });
        thread2.start();

        // Wait a little for threads to deadlock.
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException ignore) {}

        detectDeadlock();
    }
</pre></p>
<p>And the output we get is:<br />
<pre class="brush: plain; gutter: false;">
Thread1 acquired lock1
Thread2 acquired lock2
Number of deadlocked threads: 0
</pre></p>
<p>Whoa! What happened? Deadlock detection does not work with ReentrantLocks? </p>
<p><strong>findMonitorDeadlockedThreads vs. findDeadlockedThreads</strong></p>
<p>Well, part of the blame is mine. There are two methods in ThreadMXBean to detect deadlocks: <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#findMonitorDeadlockedThreads%28%29" target="_blank">findMonitorDeadlockedThreads</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/management/ThreadMXBean.html#findDeadlockedThreads%28%29" target="_blank">findDeadlockedThreads</a>. I carelessly used the former but there&#8217;s a subtle difference between the two. </p>
<p>As JavaDocs point out, findMonitorDeadlockThreads &#8220;Finds cycles of threads that are in deadlock waiting to acquire object monitors&#8221; whereas findDeadlockedThreads &#8220;Finds cycles of threads that are in deadlock waiting to acquire object monitors or <strong>ownable synchronizers</strong>&#8220;. </p>
<p>What&#8217;s an ownable synchornizer? Again according to JavaDocs: &#8220;An ownable synchronizer is a synchronizer that may be exclusively owned by a thread and uses AbstractOwnableSynchronizer (or its subclass) to implement its synchronization property. ReentrantLock and ReentrantReadWriteLock are two examples of ownable synchronizers provided by the platform.&#8221;</p>
<p>In plain English, if you want to detect ReentrankLock or ReentrantReadWriteLock, make sure you use findMonitorThreads method. If we change our deadlock method to the following:<br />
<pre class="brush: java; gutter: false;">
    private static void detectDeadlock() {
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        long[] threadIds = threadBean.findDeadlockedThreads();
        int deadlockedThreads = threadIds != null? threadIds.length : 0;
        System.out.println(&quot;Number of deadlocked threads: &quot; + deadlockedThreads);
    }
</pre></p>
<p>We get the following expected output with test2:<br />
<pre class="brush: plain; gutter: false;">
Thread1 acquired lock1
Thread2 acquired lock2
Number of deadlocked threads: 2
</pre></p>
<p>So, you might ask at this point, why even bother with findMonitorDeadlockedThreads? Well, findDeadlockedThreads was added in JDK6. So, if your project is using JDK5 still, you need to use findMonitorDeadlockedThreads and hopefully this post will remind you the caveats of using findMonitorDeadlockedThreads. </p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/java/'>Java</a>, <a href='http://meteatamel.wordpress.com/category/threading/'>Threading</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/742/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/742/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/742/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=742&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript &#8211; Two function styles</title>
		<link>http://meteatamel.wordpress.com/2012/03/08/javascript-two-function-styles/</link>
		<comments>http://meteatamel.wordpress.com/2012/03/08/javascript-two-function-styles/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 21:13:29 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=765</guid>
		<description><![CDATA[There are two different styles of defining functions (and hence classes) in JavaScript. The first style is like defining a function variable. For example, you could define a class and a private method inside that class like this: The second style is defining functions directly. So, the previous example becomes like this: Syntactically, I prefer [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=765&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are two different styles of defining functions (and hence classes) in JavaScript. The first style is like defining a function variable. For example, you could define a class and a private method inside that class like this:<br />
<pre class="brush: jscript; gutter: false;">
Person = function(fname, lname, age) {
...
    var getFirstName = function() {
    ...
    };
};
</pre></p>
<p>The second style is defining functions directly. So, the previous example becomes like this:<br />
<pre class="brush: jscript; gutter: false;">
function Person(fname, lname, age) {
...
    function getFirstName() {
    ...
    };
};
</pre></p>
<p>Syntactically, I prefer the second style. It&#8217;s simpler and feels more natural. However, <a href="http://meteatamel.wordpress.com/2012/02/29/javascript-class-and-visibility-control/" title="JavaScript – Class and Visibility Control" target="_blank">in my previous post about JavaScript classes</a>, I used the first style for a reason. The reason is that even though you could use the second style for constructors and private methods, you cannot use it for privileged or public methods. You need to set privileged methods to &#8220;this&#8221; and public methods to protototype. So, the Person class from my previous post looks like this when I use the second style:<br />
<pre class="brush: jscript;">
// Constructor
function Person(fname, lname, age) {

    // Private variables
    var _fname = fname;
    var _lname = lname;
    var _age = age;

    // Private method
    function getFirstName() {
        return _fname;
    };

    // Public variable
    this.fullName = fname + ' ' + lname;

    // Privileged method - exposes private variables and methods publicly.
    this.getFirstNameAndAge = function() {
        return getFirstName() + &quot;, &quot; + _age;
    };

    Person.numberOfPeople++;
};

// Public static variable
Person.numberOfPeople = 0;

// Public method
Person.prototype.getFullNameAndNumberOfPeople = function () {
    return this.fullName + &quot; --&gt; &quot; + Person.numberOfPeople;
};
</pre></p>
<p>Notice how some functions are defined directly and some of them are defined as function variables. Even though this is not too bad, I prefer to stick with a single style, so I always go with the first style for consistency.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/javascript/'>JavaScript</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/765/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=765&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/03/08/javascript-two-function-styles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>JavaScript &#8211; Class and Visibility Control</title>
		<link>http://meteatamel.wordpress.com/2012/02/29/javascript-class-and-visibility-control/</link>
		<comments>http://meteatamel.wordpress.com/2012/02/29/javascript-class-and-visibility-control/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 21:49:27 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=725</guid>
		<description><![CDATA[Classes and visibility control in JavaScript are a little different from the traditional object oriented languages like Java. Here&#8217;s a simple JavaScript class I use as a cheat-sheet from time to time to remind myself the JavaScript syntax. And here&#8217;s the test output for the class: Filed under: JavaScript<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=725&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Classes and visibility control in JavaScript are a little different from the traditional object oriented languages like Java. Here&#8217;s a simple JavaScript class I use as a cheat-sheet from time to time to remind myself the JavaScript syntax.<br />
<pre class="brush: jscript;">
// Constructor
Person = function(fname, lname, age) {

    // Private variables
    var _fname = fname;
    var _lname = lname;
    var _age = age;

    // Private method
    var getFirstName = function() {
        return _fname;
    };

    // Public variable
    this.fullName = fname + ' ' + lname;

    // Privileged method - exposes private variables and methods publicly.
    this.getFirstNameAndAge = function() {
        return getFirstName() + &quot;, &quot; + _age;
    };

    Person.numberOfPeople++;
};

// Public static variable
Person.numberOfPeople = 0;

// Public method
Person.prototype.getFullNameAndNumberOfPeople = function () {
    return this.fullName + &quot; --&gt; &quot; + Person.numberOfPeople;
};
</pre></p>
<p>And here&#8217;s the test output for the class:<br />
<pre class="brush: jscript;">
var person = new Person('Mete', 'Atamel', 29);
var person2 = new Person('John', 'Smith', 50);
console.log(person._fname); // undefined
console.log(person.fullName); // Mete Atamel
console.log(person.getFirstNameAndAge()); // Mete, 29
console.log(Person.numberOfPeople); // 2
console.log(person.getFullNameAndNumberOfPeople()) // Mete Atamel --&gt; 2
</pre></p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/javascript/'>JavaScript</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/725/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/725/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/725/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=725&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/02/29/javascript-class-and-visibility-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
		<item>
		<title>JMX: RMI vs. JMXMP</title>
		<link>http://meteatamel.wordpress.com/2012/02/13/jmx-rmi-vs-jmxmp/</link>
		<comments>http://meteatamel.wordpress.com/2012/02/13/jmx-rmi-vs-jmxmp/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 09:42:14 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JMX]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=712</guid>
		<description><![CDATA[JMX is widely used for monitoring and administrating servers but it&#8217;s a little quirky sometimes and in this post, I want to explain one of those quirks. Typically, a client would connect to a JMX enabled server using a URL similar to this: service:jmx:rmi:///jndi/rmi://host:port1/jmxrmi This basically tells the client to connect to a JMX server [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=712&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://meteatamel.files.wordpress.com/2012/02/limassol.jpg"><img src="http://meteatamel.files.wordpress.com/2012/02/limassol.jpg?w=645&h=386" alt="" width="645" height="386" class="aligncenter size-full wp-image-735" /></a><br />
JMX is widely used for monitoring and administrating servers but it&#8217;s a little quirky sometimes and in this post, I want to explain one of those quirks. </p>
<p>Typically, a client would connect to a JMX enabled server using a URL similar to this:</p>
<p><code>service:jmx:rmi:///jndi/rmi://host:port1/jmxrmi</code></p>
<p>This basically tells the client to connect to a JMX server on the specified host and port over RMI. At least, that&#8217;s what you&#8217;d expect right? Well, the reality is a little different. When the client connects to host over that port, the JMX server responds with something like &#8220;Ok, I got your connect request on port1, now you can connect on port2&#8243; and accordingly to <a href="http://blog.markfeeney.com/2010/10/jmx-through-ssh-tunnel.html" target="_blank">this blog post</a>, port2 is chosen randomly.</p>
<p>Why is this problematic? Imagine trying to administer your JMX enabled server through a firewall where a predetermined number of ports are open. You can see how random port2 makes life quite difficult. So what can you do? JMXMP comes to rescue. There&#8217;s not much info on JMXMP out there but <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/jmx/overview/connectors.html" target="_blank">according to JavaDocs</a>:</p>
<blockquote><p>The JMX Messaging Protocol (JMXMP) connector is a configuration of the generic connector where the transport protocol is based on TCP and the object wrapping is native Java serialization. Security is more advanced than for the RMI connector. Security is based on the Java Secure Socket Extension (JSSE), the Java Authentication and Authorization Service (JAAS), and the Simple Authentication and Security Layer (SASL).</p></blockquote>
<p>It looks like JMXMP is not only better in terms of getting rid of random ports but it has better security. The only caveat is that JMXMP connector is optional (i.e. JDK does not include it by default). All you have to do is Google for the download page for <code>jmxremote_optional.jar</code> from Oracle or if you&#8217;re a Maven user, simply add the <a href="http://mvnrepository.com/artifact/javax.management/jmxremote_optional/1.0.1_04" target="_blank">following</a> to your <code>pom.xml</code>.</p>
<p>Once you have <code>jmxremote_optional.jar</code> in classpath of your client and server, you can connect your client to your JMX server using a URL like this with your own host and port:</p>
<p><code>service:jmx:jmxmp://host:port</code></p>
<p>The URL looks much cleaner than RMI, the port you specify is the only port you need to worry about and it&#8217;s more secure. I have no idea why JMXMP is not the default connector in JMX.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/java/'>Java</a>, <a href='http://meteatamel.wordpress.com/category/jmx/'>JMX</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/712/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/712/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/712/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=712&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/02/13/jmx-rmi-vs-jmxmp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>

		<media:content url="http://meteatamel.files.wordpress.com/2012/02/limassol.jpg" medium="image" />
	</item>
		<item>
		<title>Eclipse JEE Indigo and Maven Plugin</title>
		<link>http://meteatamel.wordpress.com/2012/01/30/eclipse-jee-indigo-and-maven-plugin/</link>
		<comments>http://meteatamel.wordpress.com/2012/01/30/eclipse-jee-indigo-and-maven-plugin/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 07:01:54 +0000</pubDate>
		<dc:creator>Mete</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[eclipse ide]]></category>
		<category><![CDATA[indigo]]></category>

		<guid isPermaLink="false">http://meteatamel.wordpress.com/?p=719</guid>
		<description><![CDATA[As you might know, the newest version of Eclipse (Indigo) has Maven integration by default. Even though it has some problems (as discussed here) compared to non-default Maven integration in previous version of Eclipse (Helios), it&#8217;s good enough for simple projects. What you might not know though Maven integration is limited to &#8220;Eclipse IDE for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=719&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As you might know, the newest version of Eclipse (Indigo) has Maven integration by default. Even though it has some problems (as discussed <a href="http://eclipsesource.com/blogs/2011/06/09/maven-and-eclipse-top-eclipse-indigo-feature-10/" target="_blank">here</a>) compared to non-default Maven integration in previous version of Eclipse (Helios), it&#8217;s good enough for simple projects. </p>
<p>What you might not know though Maven integration is limited to &#8220;Eclipse IDE for Java Developers&#8221; and not included in &#8220;Eclipse IDE for JEE Developers&#8221;. It baffles me how a new feature was left out in a more advanced version of Eclipse but regardless, <a href="http://www.sonatype.com/people/2011/06/where-is-m2eclipse/" target="_blank">this Sonatype Blog post</a> explains the situation and provides the <a href="http://download.eclipse.org/releases/indigo/" target="_blank">Indigo repository link</a> where you can add Maven integration to your Eclipse Indigo JEE version.</p>
<br />Filed under: <a href='http://meteatamel.wordpress.com/category/eclipse/'>Eclipse</a>, <a href='http://meteatamel.wordpress.com/category/maven/'>Maven</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/meteatamel.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/meteatamel.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/meteatamel.wordpress.com/719/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=meteatamel.wordpress.com&#038;blog=9592957&#038;post=719&#038;subd=meteatamel&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://meteatamel.wordpress.com/2012/01/30/eclipse-jee-indigo-and-maven-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a14f50e3016098288ba5efc84c8d2553?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Mete</media:title>
		</media:content>
	</item>
	</channel>
</rss>
