<?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: Beware The Unused Thread</title>
	<atom:link href="http://www.symphonious.net/2007/02/27/beware-the-unused-thread/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.symphonious.net/2007/02/27/beware-the-unused-thread/</link>
	<description>Living in a state of accord.</description>
	<lastBuildDate>Fri, 12 Mar 2010 07:41:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Adrian Sutton</title>
		<link>http://www.symphonious.net/2007/02/27/beware-the-unused-thread/comment-page-1/#comment-62917</link>
		<dc:creator>Adrian Sutton</dc:creator>
		<pubDate>Tue, 27 Feb 2007 20:19:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.symphonious.net/2007/02/27/beware-the-unused-thread/#comment-62917</guid>
		<description>Sorry, I should have been more clear - only Threads that are always running (while (true) as you point out) or threads that are never run (ie: never have their start() method called) will cause a memory leak. In your example the threads will be executed, exit their run method and be garbage collected just fine. The problem is only when you have:
new Thread();
and then don&#039;t start() it. Threads that haven&#039;t been started don&#039;t turn up in kill -3 as far as I&#039;m aware because they haven&#039;t actually spawned a separate thread yet, but they do stay around.

Threads in servlets are particularly nasty, in general they just shouldn&#039;t be used and if they are, as you say, Servlet.destroy should stop them.</description>
		<content:encoded><![CDATA[<p>Sorry, I should have been more clear &#8211; only Threads that are always running (while (true) as you point out) or threads that are never run (ie: never have their start() method called) will cause a memory leak. In your example the threads will be executed, exit their run method and be garbage collected just fine. The problem is only when you have:<br />
new Thread();<br />
and then don&#8217;t start() it. Threads that haven&#8217;t been started don&#8217;t turn up in kill -3 as far as I&#8217;m aware because they haven&#8217;t actually spawned a separate thread yet, but they do stay around.</p>
<p>Threads in servlets are particularly nasty, in general they just shouldn&#8217;t be used and if they are, as you say, Servlet.destroy should stop them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Julius Davies</title>
		<link>http://www.symphonious.net/2007/02/27/beware-the-unused-thread/comment-page-1/#comment-62853</link>
		<dc:creator>Julius Davies</dc:creator>
		<pubDate>Tue, 27 Feb 2007 15:24:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.symphonious.net/2007/02/27/beware-the-unused-thread/#comment-62853</guid>
		<description>Adrian, can you help me out, I have one question, and one comment.

#1.  This is only a problem for while( true ) threads, right?  Am I going to run into problems if just want to do a single small task in another thread?  For example:

// Obviously real-world example would read the response, log something, deal with exceptions...
Runnable r = new Runnable() {
  public void run() {
    try {
      new URL( &quot;http://mymonitor.com/its_alive?actor=me&quot; ).openStream();
    } catch ( Exception e ) { }
  }
};

// Let&#039;s attack!
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();

Are those 5 threads going to stick around even after the run() completes?  That&#039;s not intuitive to me!  I have also monitored this kind of thing using kill -3, and they seemed to go away.  (If they aren&#039;t around in kill -3, are they still taking up memory!?!?!).


#2.  My comment is that while( true ) threads *really* do stay around.  The Tomcat manager webapp ends up being useless to me in most circumstances.  I can&#039;t use stop/start or even undeploy/deploy against most webapps, because most start threads!  (Or Timer&#039;s, like you said).  It wouldn&#039;t be so bad if people would use the Servlet.destroy() hook to realize it&#039;s time to stop all the threads their webapp created, but seems that&#039;s quite rare.

Even though the Tomcat manager webapp throws out the old classloaders for the undeploy, the classloader will stay around if some threads are still busy in their infinite loops.  It gets really fun after a few months of undeploy/deploy when legions of old threads are fighting to read files, log events, read sockets!</description>
		<content:encoded><![CDATA[<p>Adrian, can you help me out, I have one question, and one comment.</p>
<p>#1.  This is only a problem for while( true ) threads, right?  Am I going to run into problems if just want to do a single small task in another thread?  For example:</p>
<p>// Obviously real-world example would read the response, log something, deal with exceptions&#8230;<br />
Runnable r = new Runnable() {<br />
  public void run() {<br />
    try {<br />
      new URL( &#8220;http://mymonitor.com/its_alive?actor=me&#8221; ).openStream();<br />
    } catch ( Exception e ) { }<br />
  }<br />
};</p>
<p>// Let&#8217;s attack!<br />
new Thread( r ).start();<br />
new Thread( r ).start();<br />
new Thread( r ).start();<br />
new Thread( r ).start();<br />
new Thread( r ).start();</p>
<p>Are those 5 threads going to stick around even after the run() completes?  That&#8217;s not intuitive to me!  I have also monitored this kind of thing using kill -3, and they seemed to go away.  (If they aren&#8217;t around in kill -3, are they still taking up memory!?!?!).</p>
<p>#2.  My comment is that while( true ) threads *really* do stay around.  The Tomcat manager webapp ends up being useless to me in most circumstances.  I can&#8217;t use stop/start or even undeploy/deploy against most webapps, because most start threads!  (Or Timer&#8217;s, like you said).  It wouldn&#8217;t be so bad if people would use the Servlet.destroy() hook to realize it&#8217;s time to stop all the threads their webapp created, but seems that&#8217;s quite rare.</p>
<p>Even though the Tomcat manager webapp throws out the old classloaders for the undeploy, the classloader will stay around if some threads are still busy in their infinite loops.  It gets really fun after a few months of undeploy/deploy when legions of old threads are fighting to read files, log events, read sockets!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
