<?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>indiWiz.com &#187; concurrent</title>
	<atom:link href="http://indiwiz.com/tag/concurrent/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiwiz.com</link>
	<description>Subhash&#039;s Tech Log</description>
	<lastBuildDate>Wed, 03 Mar 2010 13:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Interrupting a Thread in Java</title>
		<link>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/</link>
		<comments>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:52:48 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://indiwiz.wordpress.com/?p=10</guid>
		<description><![CDATA[Long long ago, when I was studying in College, my professors taught me to abruptly interrupt a running Thread using Thread.stop() method. Later this method was deprecated. Then in some article I read to interrupt a running thread, you have to write your custom logic: class MyThread extends Thread{ private boolean isCancelled = false; public [...]]]></description>
			<content:encoded><![CDATA[<p>Long long ago, when I was studying in College, my professors taught me to abruptly interrupt a running Thread using <tt>Thread.stop()</tt> method. Later this method was deprecated. Then in some article I read to interrupt a running thread, you have to write your custom logic:</p>
<pre class="brush: java;">
class MyThread extends Thread{

    private boolean isCancelled = false;

    public void run(){
        while(isCancelled != true){
            // do long running job
        }
    }

    public void cancel(){
        isCancelled = true;
    }
}
</pre>
<p>This seems to be a valid code, but there is an issue. If we are doing some blocking operation, or an IO operation inside the loop, then the cancel notification does not reach until that is finished. Well, so how do we solve this issue?</p>
<h3>Thread API</h3>
<p>The <tt>java.lang.Thread</tt> class has the following API methods:</p>
<pre class="brush: java;">
void interrupt();
boolean isInterrupted();
</pre>
<p>To signal a running Thread about our request for termination, we can call its <tt>interrupt()</tt> method. Unlike the old <tt>stop()</tt> method, this does not abruptly terminate the execution of the Thread. What is does:</p>
<table border="1">
<tr>
<th>When Thread is in Blocking mode </th>
<th>When Thread is in normal execution</th>
</tr>
<tr>
<td>Thread&#8217;s interrupt status will be cleared, and InterruptedException thrown.</td>
<td>Thread&#8217;s interrupt status will be set. </td>
</tr>
</table>
<p>A Thread is in blocking mode during following states:</p>
<ol>
<li>Thread blocked in invocation of <tt>wait(...)</tt> methods; or</li>
<li><tt>join(...)</tt> methods; or</li>
<li><tt>sleep(...)</tt> methods.</li>
</ol>
<p>Now my actual reason for exploring this concept was due to <a href="http://code.google.com/p/rest-client/issues/detail?id=44">an issue in RESTClient</a>. I had wanted to implement a Stop button just as in a normal browser. So my Thread was not blocked using any of the interruptable methods, but due to a socket IO. So how can I solve this?</p>
<pre class="brush: java;">
class MyThread extends Thread{

    public void run(){
        while(isInterrupted() != true){
            // do socket IO
        }
    }

}
</pre>
<p>But this has the same limitation as the first code I wrote. If the socket IO is in progress, this condition is not checked until the IO is done. So what finally is the solution? It is simple:</p>
<pre class="brush: java;">
class MyThread extends Thread{

    private Socket soc = ..;

    public void run(){
        // do socket IO
    }

    @Override
    public void interrupt(){
        if(soc != null){
            soc.close(); // wrap in try/catch
        }
        super.interrupt();
    }

}
</pre>
<p>When we close the Socket object, the IO will fail raising an IOException which we can gracefully handle.</p>


<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;title=Interrupting+a+Thread+in+Java" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;title=Interrupting+a+Thread+in+Java" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;title=Interrupting+a+Thread+in+Java&amp;desc=Long%20long%20ago%2C%20when%20I%20was%20studying%20in%20College%2C%20my%20professors%20taught%20me%20to%20abruptly%20interrupt%20a%20running%20Thread%20using%20Thread.stop%28%29%20method.%20Later%20this%20method%20was%20deprecated.%20Then%20in%20some%20article%20I%20read%20to%20interrupt%20a%20running%20thread%2C%20you%20have%20to%20write%20your%20custom%20logic%3A%0A%0A%5Bsourcecode%20language%3D%27java%27%5D%0Acl" rel="nofollow" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;t=Interrupting+a+Thread+in+Java" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;title=Interrupting+a+Thread+in+Java&amp;summary=Long%20long%20ago%2C%20when%20I%20was%20studying%20in%20College%2C%20my%20professors%20taught%20me%20to%20abruptly%20interrupt%20a%20running%20Thread%20using%20Thread.stop%28%29%20method.%20Later%20this%20method%20was%20deprecated.%20Then%20in%20some%20article%20I%20read%20to%20interrupt%20a%20running%20thread%2C%20you%20have%20to%20write%20your%20custom%20logic%3A%0A%0A%5Bsourcecode%20language%3D%27java%27%5D%0Acl&amp;source=indiWiz.com" rel="nofollow" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/&amp;title=Interrupting+a+Thread+in+Java" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/interrupting-a-thread-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
