<?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>-Flyぁ梦- &#187; 线程</title>
	<atom:link href="http://blog.11034.org/tag/%e7%ba%bf%e7%a8%8b/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>线程清理</title>
		<link>http://blog.11034.org/2016-08/thread_stop.html</link>
		<comments>http://blog.11034.org/2016-08/thread_stop.html#comments</comments>
		<pubDate>Fri, 05 Aug 2016 09:26:53 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[线程]]></category>

		<guid isPermaLink="false">http://blog.11034.org/?p=2862</guid>
		<description><![CDATA[执行脚本遇到一个情况，脚本无法自动退出完毕，必须手动执行Ctrl-C才能退出，之前遇到过这个问题是因为数据库连 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>执行脚本遇到一个情况，脚本无法自动退出完毕，必须手动执行Ctrl-C才能退出，之前遇到过这个问题是因为数据库连接没有被正确关闭导致，这次的原因是一个另起的线程没有正常停止的原因。</p>
<p><span id="more-2862"></span></p>
<p>此线程A维护一个队列，线程A负责从队列中消费，主线程负责往线程中添加，队列池用的LinkedBlockQueue。线程A主要工作就是，从queue中take()出一个资源，但是处理相关逻辑，然后循环。</p>
<p>所以在脚本执行完毕后，需要给线程一个通知，当队列为空时，自动退出循环结束线程。然后在主线程中等待theadA.join()即可。</p>
<p>然后的问题是，LinkedBlockQueue.take()是无限等待的，所以还需要执行theadA.interrupt()才能够跳出这个无限等待的API。</p>
<p>但是如果执行theadA.interrupt()时，队列还不为空，就会出问题。</p>
<p>所以使用LinkedBlockQueue.poll(long timeout, TimeUnit unit)，就不需要执行theadA.interrupt()，也能够判断到队列为空自动退出循环了。</p>
<p>但是有超时时间的poll方法性能上应该要比无超时时间的take()要低，而且正常执行过程中当队列为空时反复超时跳出等待再进去新的等待，浪费CPU。</p>
<p>这是个有点纠结的问题。</p>
<p>以下代码，用stop()来立刻停止线程（无论queue是否还有剩下的未处理），用interrupt()来等待处理完queue中所有后再退出。</p>
<p>这里或者可以在interrupt()中返回queue.size()，根据返回的size若为0可以执行theadA.interrupt()，不为0则不执行，但是又有问题同步队列的size()方法会引起线程同步的问题（size()方法并没有太大的意义）。</p>
<p><code class="markdown_inline_code">running和interrupt变量是会在不同的线程write和read的，所以必须要用volatile修饰，保证一致性</code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">volatile</span> <span style="color: #000066; font-weight: bold;">boolean</span> running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	running <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">volatile</span> <span style="color: #000066; font-weight: bold;">boolean</span> interrupt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> interrupt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	interrupt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@Override
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>running<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Object</span> obj <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				obj <span style="color: #339933;">=</span> queue.<span style="color: #006633;">take</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				obj <span style="color: #339933;">=</span> queue.<span style="color: #006633;">poll</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span>, TimeUnit.<span style="color: #006633;">SECONDS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				logger.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;used to break queue.take() infinite waiting&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				logger.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;get obj erorr&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>obj <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">do</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>interrupt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">int</span> size <span style="color: #339933;">=</span> queue.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>size <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;interrupt after queue size == 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
					logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;interrupt but queue size == &quot;</span> <span style="color: #339933;">+</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			logger.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;erorr&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;stop succ&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2011-03-07 -- <a target="_blank" href="http://blog.11034.org/2011-03/zijingang_to_shanghai.html" title="玉泉、紫金港、上海">玉泉、紫金港、上海</a></li><li>2011-02-05 -- <a target="_blank" href="http://blog.11034.org/2011-02/pc_repair.html" title="学计算机的还是该会修电脑啊">学计算机的还是该会修电脑啊</a></li><li>2012-02-10 -- <a target="_blank" href="http://blog.11034.org/2012-02/wp-sns-share_2-5.html" title="wp_sns_share更新2.5">wp_sns_share更新2.5</a></li><li>2023-03-22 -- <a target="_blank" href="http://blog.11034.org/2023-03/disney.html" title="上海迪士尼周末游">上海迪士尼周末游</a></li><li>2011-04-24 -- <a target="_blank" href="http://blog.11034.org/2011-04/something_to_talk.html" title="流水账，碎碎念">流水账，碎碎念</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li><li>2016-06-17 -- <a target="_blank" href="http://blog.11034.org/2016-06/countdownlatch.html" title="CountDownLatch简单分析">CountDownLatch简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2016-08/thread_stop.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
