<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: EJCP: #9 Stopping threads</title>
	<atom:link href="http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/</link>
	<description></description>
	<lastBuildDate>Tue, 09 Mar 2010 17:30:15 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: SWiki</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-91946</link>
		<dc:creator>SWiki</dc:creator>
		<pubDate>Tue, 23 Jun 2009 17:14:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-91946</guid>
		<description>This is a simple clear example, Thanks!</description>
		<content:encoded><![CDATA[<p>This is a simple clear example, Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JMM: Thank God or the Devil for Strong Cache Coherence? &#171; Blog of Peter Veentjer</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-44384</link>
		<dc:creator>JMM: Thank God or the Devil for Strong Cache Coherence? &#171; Blog of Peter Veentjer</dc:creator>
		<pubDate>Sat, 17 May 2008 12:34:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-44384</guid>
		<description>[...] Model problems, but the compiler can cause it as well. Currently it already is possible that a compiler optimization could break an application, no matter how strong the cache coherence of the hardware is. And no [...]</description>
		<content:encoded><![CDATA[<p>[...] Model problems, but the compiler can cause it as well. Currently it already is possible that a compiler optimization could break an application, no matter how strong the cache coherence of the hardware is. And no [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-35411</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Fri, 22 Feb 2008 10:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-35411</guid>
		<description>@Stephen Zhang,

In principle this solution would work. But when a thread hits an interruption-tripwire (an interrupted exception is thrown when such a tripwire is passed), the interrupt status is removed. And if the InterruptedException is wrapped in a RuntimeException (a practice I see quite often) all information about the desired stop is lost.

Another problem is that you don\&#039;t always want to interrupt, but want to stop as soon as the thread is ready for the next iteration.

So personally I would still go for a stop flag (to prevent not stopping) and maybe for the interrupt (if a thread needs to be shutdown immediately).</description>
		<content:encoded><![CDATA[<p>@Stephen Zhang,</p>
<p>In principle this solution would work. But when a thread hits an interruption-tripwire (an interrupted exception is thrown when such a tripwire is passed), the interrupt status is removed. And if the InterruptedException is wrapped in a RuntimeException (a practice I see quite often) all information about the desired stop is lost.</p>
<p>Another problem is that you don\&#8217;t always want to interrupt, but want to stop as soon as the thread is ready for the next iteration.</p>
<p>So personally I would still go for a stop flag (to prevent not stopping) and maybe for the interrupt (if a thread needs to be shutdown immediately).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Someone</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-35039</link>
		<dc:creator>Someone</dc:creator>
		<pubDate>Wed, 20 Feb 2008 10:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-35039</guid>
		<description>For what it&#039;s worth, we have that class in our internal framework. It&#039;s called a MortalThread!</description>
		<content:encoded><![CDATA[<p>For what it&#8217;s worth, we have that class in our internal framework. It&#8217;s called a MortalThread!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Zhang</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34988</link>
		<dc:creator>Stephen Zhang</dc:creator>
		<pubDate>Wed, 20 Feb 2008 04:26:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34988</guid>
		<description>public void run()
{
    while(!Thread.currrentThread().isInterrupted())
{
       //some actions
}
}

public void stop( )
{
Thread.currentThread().interupt();
}

and make some other thread call stop method would be enough to safely stop a thread, though you may need to add some housekeeping code to implement a interruption policy like clean up unfinished operation etc..</description>
		<content:encoded><![CDATA[<p>public void run()<br />
{<br />
    while(!Thread.currrentThread().isInterrupted())<br />
{<br />
       //some actions<br />
}<br />
}</p>
<p>public void stop( )<br />
{<br />
Thread.currentThread().interupt();<br />
}</p>
<p>and make some other thread call stop method would be enough to safely stop a thread, though you may need to add some housekeeping code to implement a interruption policy like clean up unfinished operation etc..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34136</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Thu, 14 Feb 2008 17:01:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34136</guid>
		<description>&gt;&gt; Would be declaring stop as volatile solve the 
&gt;&gt; problem? Under 1.4.2? Under 1.5+? Maybe you can 
&gt;&gt; explain shortly what “volatile” means.

Hmm.. under the 1.4 virtual machine the variable is visible, but the reordering is still possible. The behavior of this code under 1.4 is not very well defined. The 1.4 vm&#039;s should not be used, they are broken.

Volatile (under 1.5) does 2 things:
-prevent visibility problems
-prevent reorderings</description>
		<content:encoded><![CDATA[<p>>> Would be declaring stop as volatile solve the<br />
>> problem? Under 1.4.2? Under 1.5+? Maybe you can<br />
>> explain shortly what “volatile” means.</p>
<p>Hmm.. under the 1.4 virtual machine the variable is visible, but the reordering is still possible. The behavior of this code under 1.4 is not very well defined. The 1.4 vm&#8217;s should not be used, they are broken.</p>
<p>Volatile (under 1.5) does 2 things:<br />
-prevent visibility problems<br />
-prevent reorderings</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34135</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Thu, 14 Feb 2008 16:58:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34135</guid>
		<description>&gt;&gt; thread.interrupt() and thread.isInterrupted() will do the same

True.. partly.. I have seen way too much code that just gobbles up the interrupted exception.

try{
    ..some blocking call
}catch(InterruptedException ex){
    throw new RuntimeException(ex);
} 

In this case the interrupted status is removed from the thread, and the interruptedexception is hidden. So for the loop it is very hard to figure out what to do. If you look at the ThreadPoolExecutor, they also are not just relying on the Thread interrupt status because it is very easy to break.</description>
		<content:encoded><![CDATA[<p>>> thread.interrupt() and thread.isInterrupted() will do the same</p>
<p>True.. partly.. I have seen way too much code that just gobbles up the interrupted exception.</p>
<p>try{<br />
    ..some blocking call<br />
}catch(InterruptedException ex){<br />
    throw new RuntimeException(ex);<br />
} </p>
<p>In this case the interrupted status is removed from the thread, and the interruptedexception is hidden. So for the loop it is very hard to figure out what to do. If you look at the ThreadPoolExecutor, they also are not just relying on the Thread interrupt status because it is very easy to break.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Veentjer</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34134</link>
		<dc:creator>Peter Veentjer</dc:creator>
		<pubDate>Thu, 14 Feb 2008 16:55:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34134</guid>
		<description>&gt;&gt;What exactly do you mean &gt;&gt;by “execution of a &gt;&gt;single iteration doesn’t take too much time? 

I a single iteration takes 5 minutes for example, the shutdown of the thread can take 5 minutes. If the task blocks for and undetermined amount of time, you can&#039;t say anything about the shutdown duration.

&gt;&gt;Or are you coming back to the interruption of &gt;&gt;Threads later on in your top 10?

I don&#039;t think so. If you really want to know more about shutting down threads, please have a look at &quot;Concurrent Programming in Java&quot; chapter 7.</description>
		<content:encoded><![CDATA[<p>>>What exactly do you mean >>by “execution of a >>single iteration doesn’t take too much time? </p>
<p>I a single iteration takes 5 minutes for example, the shutdown of the thread can take 5 minutes. If the task blocks for and undetermined amount of time, you can&#8217;t say anything about the shutdown duration.</p>
<p>>>Or are you coming back to the interruption of >>Threads later on in your top 10?</p>
<p>I don&#8217;t think so. If you really want to know more about shutting down threads, please have a look at &#8220;Concurrent Programming in Java&#8221; chapter 7.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34091</link>
		<dc:creator>Erik</dc:creator>
		<pubDate>Thu, 14 Feb 2008 09:09:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34091</guid>
		<description>Hi,

interesting read. Would be declaring stop as volatile solve the problem? Under 1.4.2? Under 1.5+? Maybe you can explain shortly what &quot;volatile&quot; means.

Thanks.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>interesting read. Would be declaring stop as volatile solve the problem? Under 1.4.2? Under 1.5+? Maybe you can explain shortly what &#8220;volatile&#8221; means.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ilgvars</title>
		<link>http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/comment-page-1/#comment-34078</link>
		<dc:creator>ilgvars</dc:creator>
		<pubDate>Thu, 14 Feb 2008 05:52:25 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/2008/02/12/ejcp-9-stopping-threads/#comment-34078</guid>
		<description>thread.interrupt() and thread.isInterrupted() will do the same</description>
		<content:encoded><![CDATA[<p>thread.interrupt() and thread.isInterrupted() will do the same</p>
]]></content:encoded>
	</item>
</channel>
</rss>
