<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/java-threadpool-implement/</link>
<title><![CDATA[java程序实现线程连接池功能]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Thu, 09 Sep 2010 16:43:33 +0000</pubDate> 
<guid>https://www.heckjj.com/java-threadpool-implement/</guid> 
<description>
<![CDATA[ 
	线程池：<br/><textarea name="code" class="java" rows="15" cols="100">import java.util.linkedlist;

public abstract class manager
&#123;

&nbsp;&nbsp;&nbsp;&nbsp;private string mthreadpoolname = null;

&nbsp;&nbsp;&nbsp;&nbsp;private int mthreadpoolmaxsize = 1;

&nbsp;&nbsp;&nbsp;&nbsp;private linkedlist workers = new linkedlist();

&nbsp;&nbsp;&nbsp;&nbsp;public manager()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public manager(string name, int poolmaxsize)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mthreadpoolname = name;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createworker(name, poolmaxsize);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mthreadpoolmaxsize = poolmaxsize;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;&nbsp; void&nbsp;&nbsp; createworker(int&nbsp;&nbsp; pool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p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;&nbsp;&nbsp;public synchronized worker getidleworker()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (worker) workers.removefirst();
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public synchronized void notifyfree(worker worker)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (workers.size() < mthreadpoolmaxsize)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;workers.addlast(worker);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worker = null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public int getthreadpoolmaxsize()
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return mthreadpoolmaxsize;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;public void setthreadpoolmaxsize(int threadpoolmaxsize)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.mthreadpoolmaxsize = threadpoolmaxsize;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&#125;</textarea><br/>线程抽象类：<textarea name="code" class="java" rows="15" cols="100">
public abstract class worker implements runnable
&#123;

&nbsp;&nbsp;private manager mmanager = null;

&nbsp;&nbsp;private thread mthread = null;

&nbsp;&nbsp;public worker()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public worker(string threadname, manager manager)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;mmanager = manager;
&nbsp;&nbsp;&nbsp;&nbsp;mthread = new thread(this, threadname);
&nbsp;&nbsp;&nbsp;&nbsp;init();
&nbsp;&nbsp;&nbsp;&nbsp;mthread.start();
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public abstract void init();

&nbsp;&nbsp;public void run()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;while (true)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waitforstart();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worker worker = mmanager.getidleworker();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;process();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;isrunning = false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mmanager.notifyfree(worker);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public abstract void process();

&nbsp;&nbsp;public void start()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;isrunning = true;
&nbsp;&nbsp;&nbsp;&nbsp;mmanager.getidleworker();
&nbsp;&nbsp;&nbsp;&nbsp;notifytostart();
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public synchronized void waitforstart()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait();
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;catch (interruptedexception ex)
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public synchronized void notifytostart()
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;notify();
&nbsp;&nbsp;&#125;

&#125;</textarea><br/>Tags - <a href="https://www.heckjj.com/tags/java%25E7%25BA%25BF%25E7%25A8%258B%25E8%25BF%259E%25E6%258E%25A5%25E6%25B1%25A0/" rel="tag">java线程连接池</a> , <a href="https://www.heckjj.com/tags/%25E7%25BA%25BF%25E7%25A8%258B%25E8%25BF%259E%25E6%258E%25A5%25E6%25B1%25A0/" rel="tag">线程连接池</a> , <a href="https://www.heckjj.com/tags/%25E8%25BF%259E%25E6%258E%25A5%25E6%25B1%25A0/" rel="tag">连接池</a> , <a href="https://www.heckjj.com/tags/%25E7%25BA%25BF%25E7%25A8%258B%25E6%25B1%25A0/" rel="tag">线程池</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/java-threadpool-implement/#blogcomment</link>
<title><![CDATA[[评论] java程序实现线程连接池功能]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/java-threadpool-implement/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>