{"id":160,"date":"2015-04-10T19:23:51","date_gmt":"2015-04-10T11:23:51","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=160"},"modified":"2015-04-18T16:39:02","modified_gmt":"2015-04-18T08:39:02","slug":"%e4%b8%80%e4%b8%aa%e8%99%bd%e5%a4%8d%e6%9d%82%e4%bd%86%e5%8f%af%e7%9b%b4%e6%8e%a5%e5%a5%97%e7%94%a8%e7%9a%84%e7%ba%bf%e7%a8%8b%e6%b1%a0%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=160","title":{"rendered":"\u4e00\u4e2a\u867d\u590d\u6742\u4f46\u53ef\u76f4\u63a5\u5957\u7528\u7684\u7ebf\u7a0b\u6c60\u5b9e\u4f8b"},"content":{"rendered":"<p>\u7ee7\u201c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u201d\u6587\u7ae0\u53d1\u5e03\u540e\uff0c\u6700\u8fd1\u53c8\u7ee7\u7eed\u7814\u7a76\uff0c\u63a8\u51fa\u4e00\u4e2a\u6bd4\u8f83\u590d\u6742\u4f46\u529f\u80fd\u6bd4\u8f83\u5b8c\u5584\uff0c\u800c\u4e14\u53ef\u4ee5\u76f4\u63a5\u5957\u7528\u7684\u7ebf\u7a0b\u6c60\u7684\u5b9e\u4f8b\uff0c\u5e0c\u671b\u5bf9\u4f7f\u7528\u591a\u7ebf\u7a0b\u8bbe\u8ba1\u7684\u8bfb\u8005\u6709\u6240\u5e2e\u52a9\u3002<br \/>\n<!--more--><\/p>\n<p>\u8be5\u5b9e\u4f8b\u6765\u6e90\u4e8eApache\u9879\u76ee\u6e90\u4ee3\u7801\uff0c\u6e90\u7a0b\u5e8f\u6709800\u4f59\u884c\uff0c\u529f\u80fd\u6bd4\u8f83\u5168\u9762\uff0c\u800c\u4e14\u662f\u975e\u5e38\u5b8c\u5584\u7684\uff0c\u5e76\u4e14\u8fd0\u884c\u4e8e\u8bf8\u591a\u670d\u52a1\u5668\u5982tomcat\u4e0a\uff0c\u5c31\u662f\u5206\u6790\u8d77\u6765\u6709\u70b9\u7e41\u7410\u3002\u5982\u679c\u5f00\u53d1\u4eba\u5458\u76f4\u63a5\u628a\u8fd9\u6bb5\u7a0b\u5e8f\u62ff\u6765\u4fee\u6539\u540e\u4f7f\u7528\u5230\u81ea\u5df1\u7684\u5f00\u53d1\u9879\u76ee\u4e2d\uff0c\u4e0d\u5931\u4e3a\u62ff\u6765\u4e3b\u4e49\u7684\u4e0a\u7b56\u3002<\/p>\n<p>\u672c\u6587\u5bf9\u8be5\u6e90\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4fee\u6539\u548c\u7b80\u5316\uff0c\u5bf9\u5176\u4e2d\u6838\u5fc3\u90e8\u5206\u8fdb\u884c\u5206\u6790\uff0c\u7136\u540e\u521b\u5efa\u6d4b\u8bd5\u7c7b\u8fdb\u884c\u6d4b\u8bd5\u3002\u8bfb\u8005\u5b66\u4e60\u4e4b\u540e\u53ef\u4ee5\u76f4\u63a5\u6a21\u4eff\u548c\u5957\u7528\uff0c\u800c\u4e0d\u9700\u8981\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\u81ea\u5df1\u4eb2\u81ea\u518d\u53bb\u5199\u7ebf\u7a0b\u6c60\u7a0b\u5e8f\u4e86\u3002<\/p>\n<p>\u5173\u952e\u7684\u7a0b\u5e8f\u67092\u4e2a:\u7ebf\u7a0b\u6c60\u7c7b\uff08ThreadPool\uff09\u548c\u7ebf\u7a0b\u6c60\u63a5\u53e3\uff08ThreadPoolRunnable\uff09\u3002<\/p>\n<p>\u9996\u5148\u770b\u8f83\u590d\u6742\u7684\u7ebf\u7a0b\u6c60\u7c7b\u7a0b\u5e8f\uff0c\u6587\u4ef6\u540d\u4e3aThreadPool.java\uff0c\u6709200\u4f59\u884c\uff0c\u9700\u8981\u8bfb\u8005\u6709\u8010\u5fc3\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n[java]import java.util.Vector;<\/p>\n<p>public class ThreadPool {<br \/>\n    public static final int MAX_THREADS = 100;<br \/>\n    public static final int MAX_SPARE_THREADS = 50;<br \/>\n    public static final int MIN_SPARE_THREADS = 10;<br \/>\n    public static final int WORK_WAIT_TIMEOUT = 60 * 1000;<\/p>\n<p>    protected Vector pool;<br \/>\n    protected MonitorRunnable monitor;<br \/>\n    protected int maxThreads;<br \/>\n    protected int minSpareThreads;<br \/>\n    protected int maxSpareThreads;<br \/>\n    protected int currentThreadCount;<br \/>\n    protected int currentThreadsBusy;<br \/>\n    protected boolean stopThePool;<\/p>\n<p>    public ThreadPool() {<br \/>\n        maxThreads = MAX_THREADS;<br \/>\n        maxSpareThreads = MAX_SPARE_THREADS;<br \/>\n        minSpareThreads = MIN_SPARE_THREADS;<br \/>\n        currentThreadCount = 0;<br \/>\n        currentThreadsBusy = 0;<br \/>\n        stopThePool = false;<br \/>\n    }<\/p>\n<p>    public synchronized void start() {<br \/>\n        adjustLimits();<br \/>\n        openThreads(minSpareThreads);<br \/>\n        monitor = new MonitorRunnable(this);<br \/>\n    }<\/p>\n<p>    public void setMaxThreads(int maxThreads) {<br \/>\n        this.maxThreads = maxThreads;<br \/>\n    }<\/p>\n<p>    public int getMaxThreads() {<br \/>\n        return maxThreads;<br \/>\n    }<\/p>\n<p>    public void setMinSpareThreads(int minSpareThreads) {<br \/>\n        this.minSpareThreads = minSpareThreads;<br \/>\n    }<\/p>\n<p>    public int getMinSpareThreads() {<br \/>\n        return minSpareThreads;<br \/>\n    }<\/p>\n<p>    public void setMaxSpareThreads(int maxSpareThreads) {<br \/>\n        this.maxSpareThreads = maxSpareThreads;<br \/>\n    }<\/p>\n<p>    public int getMaxSpareThreads() {<br \/>\n        return maxSpareThreads;<br \/>\n    }<\/p>\n<p>    public void runIt(ThreadPoolRunnable r) {<br \/>\n        if (null == r) {<br \/>\n            throw new NullPointerException();<br \/>\n        }<br \/>\n        if (0 == currentThreadCount || stopThePool) {<br \/>\n            throw new IllegalStateException();<br \/>\n        }<br \/>\n        ControlRunnable c = null;<br \/>\n        synchronized (this) {<br \/>\n            if (currentThreadsBusy == currentThreadCount) {<br \/>\n                if (currentThreadCount &lt; maxThreads) {<br \/>\n                    int toOpen = currentThreadCount + minSpareThreads;<br \/>\n                    openThreads(toOpen);<br \/>\n                } else {<br \/>\n                    while (currentThreadsBusy == currentThreadCount) {<br \/>\n                        try {<br \/>\n                            this.wait();<br \/>\n                        }catch (InterruptedException e) {<br \/>\n                        }<br \/>\n                        if (0 == currentThreadCount || stopThePool) {<br \/>\n                            throw new IllegalStateException();<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            c = (ControlRunnable) pool.lastElement();<br \/>\n            pool.removeElement(c);<br \/>\n            currentThreadsBusy++;<br \/>\n        }<br \/>\n        c.runIt(r);<br \/>\n    }<\/p>\n<p>    public synchronized void shutdown() {<br \/>\n        if (!stopThePool) {<br \/>\n            stopThePool = true;<br \/>\n            monitor.terminate();<br \/>\n            monitor = null;<br \/>\n            for (int i = 0; i &lt; (currentThreadCount &#8211; currentThreadsBusy); i++) {<br \/>\n                try {<br \/>\n                    ((ControlRunnable) (pool.elementAt(i))).terminate();<br \/>\n                } catch (Throwable t) {<br \/>\n                }<br \/>\n            }<br \/>\n            currentThreadsBusy = currentThreadCount = 0;<br \/>\n            pool = null;<br \/>\n            notifyAll();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    protected synchronized void checkSpareControllers() {<br \/>\n        if (stopThePool) {<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        if ((currentThreadCount &#8211; currentThreadsBusy) &gt; maxSpareThreads) {<br \/>\n            int toFree = currentThreadCount &#8211; currentThreadsBusy &#8211; maxSpareThreads;<br \/>\n            for (int i = 0; i &lt; toFree; i++) {<br \/>\n                ControlRunnable c = (ControlRunnable) pool.firstElement();<br \/>\n                pool.removeElement(c);<br \/>\n                c.terminate();<br \/>\n                currentThreadCount&#8211;;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    protected synchronized void returnController(ControlRunnable c) {<br \/>\n        if (0 == currentThreadCount || stopThePool) {<br \/>\n            c.terminate();<br \/>\n            return;<br \/>\n        }<br \/>\n        currentThreadsBusy&#8211;;<\/p>\n<p>        pool.addElement(c);<br \/>\n        notify();<br \/>\n    }<\/p>\n<p>    protected synchronized void notifyThreadEnd() {<br \/>\n        currentThreadsBusy&#8211;;<br \/>\n        currentThreadCount&#8211;;<br \/>\n        notify();<br \/>\n        openThreads(minSpareThreads);<br \/>\n    }<\/p>\n<p>    protected void adjustLimits() {<br \/>\n        if (maxThreads &lt;= 0) {<br \/>\n            maxThreads = MAX_THREADS;<br \/>\n        }<br \/>\n        if (maxSpareThreads &gt;= maxThreads) {<br \/>\n            maxSpareThreads = maxThreads;<br \/>\n        }<br \/>\n        if (maxSpareThreads &lt;= 0) {<br \/>\n            if (1 == maxThreads) {<br \/>\n                maxSpareThreads = 1;<br \/>\n            } else {<br \/>\n                maxSpareThreads = maxThreads \/ 2;<br \/>\n            }<br \/>\n        }<br \/>\n        if (minSpareThreads &gt; maxSpareThreads) {<br \/>\n            minSpareThreads = maxSpareThreads;<br \/>\n        }<br \/>\n        if (minSpareThreads &lt;= 0) {<br \/>\n            if (1 == maxSpareThreads) {<br \/>\n                minSpareThreads = 1;<br \/>\n            } else {<br \/>\n                minSpareThreads = maxSpareThreads \/ 2;<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    protected void openThreads(int toOpen) {<br \/>\n        if (toOpen &gt; maxThreads) {<br \/>\n            toOpen = maxThreads;<br \/>\n        }<br \/>\n        if (0 == currentThreadCount) {<br \/>\n            pool = new Vector(toOpen);<br \/>\n        }<br \/>\n        for (int i = currentThreadCount; i &lt; toOpen; i++) {<br \/>\n            pool.addElement(new ControlRunnable(this));<br \/>\n        }<br \/>\n        currentThreadCount = toOpen;<br \/>\n    }<\/p>\n<p>    class MonitorRunnable implements Runnable {<br \/>\n        ThreadPool p;<br \/>\n        Thread t;<br \/>\n        boolean shouldTerminate;<br \/>\n        MonitorRunnable(ThreadPool p) {<br \/>\n            shouldTerminate = false;<br \/>\n            this.p = p;<br \/>\n            t = new Thread(this);<br \/>\n            t.start();<br \/>\n        }<br \/>\n        public void run() {<br \/>\n            while (true) {<br \/>\n                try {<br \/>\n                    synchronized (this) {<br \/>\n                        this.wait(WORK_WAIT_TIMEOUT);<br \/>\n                    }<br \/>\n                    if (shouldTerminate) {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                    p.checkSpareControllers();<br \/>\n                } catch (Throwable t) {<br \/>\n                    t.printStackTrace();<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        public synchronized void terminate() {<br \/>\n            shouldTerminate = true;<br \/>\n            this.notify();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    class ControlRunnable implements Runnable {<br \/>\n        ThreadPool p;<br \/>\n        Thread t;<br \/>\n        ThreadPoolRunnable toRun;<br \/>\n        boolean shouldTerminate;<br \/>\n        boolean shouldRun;<br \/>\n        boolean noThData;<br \/>\n        Object thData[] = null;<\/p>\n<p>        ControlRunnable(ThreadPool p) {<br \/>\n            toRun = null;<br \/>\n            shouldTerminate = false;<br \/>\n            shouldRun = false;<br \/>\n            this.p = p;<br \/>\n            t = new Thread(this);<br \/>\n            t.start();<br \/>\n            noThData = true;<br \/>\n            thData = null;<br \/>\n        }<\/p>\n<p>        public void run() {<br \/>\n            while (true) {<br \/>\n                try {<br \/>\n                    synchronized (this) {<br \/>\n                        if (!shouldRun &amp;&amp; !shouldTerminate) {<br \/>\n                            this.wait();<br \/>\n                        }<br \/>\n                    }<br \/>\n                    if (shouldTerminate) {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                    try {<br \/>\n                        if (noThData) {<br \/>\n                            thData = toRun.getInitData();<br \/>\n                            noThData = false;<br \/>\n                        }<br \/>\n                        if (shouldRun) {<br \/>\n                            toRun.runIt(thData);<br \/>\n                        }<br \/>\n                    } catch (Throwable t) {<br \/>\n                        System.err.println(&quot;ControlRunnable Throwable: &quot;);<br \/>\n                        t.printStackTrace();<br \/>\n                        shouldTerminate = true;<br \/>\n                        shouldRun = false;<br \/>\n                        p.notifyThreadEnd();<br \/>\n                    } finally {<br \/>\n                        if (shouldRun) {<br \/>\n                            shouldRun = false;<br \/>\n                            p.returnController(this);<br \/>\n                        }<br \/>\n                    }<br \/>\n                    if (shouldTerminate) {<br \/>\n                        break;<br \/>\n                    }<br \/>\n                } catch (InterruptedException ie) {<br \/>\n                }<br \/>\n            }<br \/>\n        }<\/p>\n<p>        public synchronized void runIt(ThreadPoolRunnable toRun) {<br \/>\n            if (toRun == null) {<br \/>\n                throw new NullPointerException(&quot;No Runnable&quot;);<br \/>\n            }<br \/>\n            this.toRun = toRun;<br \/>\n            shouldRun = true;<br \/>\n            this.notify();<br \/>\n        }<\/p>\n<p>        public synchronized void terminate() {<br \/>\n            shouldTerminate = true;<br \/>\n            this.notify();<br \/>\n        }<br \/>\n    }<br \/>\n}[\/java]<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n\u4ee5\u4e0a\u7a0b\u5e8f\u4e2d\uff0c\u5173\u952e\u7684\u662fopenThreads\u65b9\u6cd5\u3001runIt\u65b9\u6cd5\u4ee5\u53ca2\u4e2a\u5185\u90e8\u7c7b\uff1aMonitorRunnable\u548cControlRunnable\u3002<\/p>\n<p>\u521a\u5f00\u59cb\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u5f80Vector\u5bf9\u8c61\u91cc\u88c5\u5165minSpareThreads\u4e2a\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662fControlRunnable\u7ebf\u7a0b\u7c7b\uff0cControlRunnable\u7c7b\u5728\u5176\u6784\u9020\u65b9\u6cd5\u4e2d\u542f\u52a8\u7ebf\u7a0b\u3002\u5982\u679cshouldRun\u548cshouldTerminate\u90fd\u662ffalse\u7684\u8bdd\uff0c\u7ebf\u7a0b\u5c31\u7b49\u5f85\u3002\u5982\u679cshouldRun\u4e3atrue\uff0c\u5c31\u8c03\u7528ThreadPoolRunnable\u7684runIt(Object[])\u65b9\u6cd5\uff0c\u8be5\u63a5\u53e3\u7684\u65b9\u6cd5\u5c31\u662f\u6211\u4eec\u9700\u8981\u5728\u81ea\u5df1\u7684\u4efb\u52a1\u7c7b\u4e2d\u8986\u76d6\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u679cminSpareThreads\u4e2a\u7ebf\u7a0b\u90fd\u5904\u4e8eBusy\u540e\uff0c\u7ebf\u7a0b\u6c60\u4f1a\u518d\u521b\u5efa\u51faminSpareThreads\u4e2a\u7ebf\u7a0b\u3002MonitorRunnable\u662f\u7528\u6765\u76d1\u89c6\u7ebf\u7a0b\u6c60\u8fd0\u884c\u60c5\u51b5\u7684\uff0c\u5176\u7ebf\u7a0b\u95f4\u969460\u79d2\uff08WORK_WAIT_TIMEOUT\uff09\u8c03\u7528\u4e00\u6b21\u7ebf\u7a0b\u6c60\u7c7b\u7684checkSpareControllers\u65b9\u6cd5\uff0c\u5982\u679c\u53d1\u73b0(currentThreadCount &#8211; currentThreadsBusy) > maxSpareThreads\uff0c\u5c31\u4f1a\u8c03\u7528ControlRunnable\u7c7b\u7684terminate\u65b9\u6cd5\u5220\u9664\u7a7a\u95f2\u7ebf\u7a0b\uff0c\u51c6\u5907\u5220\u9664\u7684\u7ebf\u7a0b\u662f\u5426\u7a7a\u95f2\u662f\u901a\u8fc7shouldTerminate\u53c2\u6570\u6765\u5224\u65ad\u7684\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u63a5\u53e3ThreadPoolRunnable\u67092\u4e2a\u7a7a\u65b9\u6cd5getInitData\u548crunIt\uff0c\u6211\u4eec\u4e00\u822c\u81ea\u5df1\u521b\u5efa\u4e00\u4e2a\u4efb\u52a1\u7c7b\u5b9e\u73b0\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\uff0c\u628a\u5177\u4f53\u7684\u4efb\u52a1\u5185\u5bb9\u653e\u5728\u4efb\u52a1\u7c7b\u7684runIt\u65b9\u6cd5\u4e2d\u3002\u5982\u679c\u4e0d\u60f3\u7528getInitData\uff0c\u5c31\u8ba9\u5b83\u8fd4\u56denull\u503c\u3002<\/p>\n<p>\u7ebf\u7a0b\u6c60\u63a5\u53e3\u7a0b\u5e8f\u5f88\u7b80\u5355\uff0c\u6587\u4ef6\u540d\u4e3aThreadPoolRunnable.java\uff0c\u5c31\u51e0\u884c\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n[java]public interface ThreadPoolRunnable {<br \/>\n    public Object[] getInitData();<\/p>\n<p>    public void runIt(Object thData[]);<\/p>\n<p>}[\/java]<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>\u7ebf\u7a0b\u6c60\u7c7b\u548c\u7ebf\u7a0b\u6c60\u63a5\u53e3\u90fd\u5df2\u7ecf\u8bf4\u5b8c\uff0c\u4e0b\u9762\u5c31\u4e3e\u4e2a\u4f8b\u5b50\u8bf4\u8bf4\u600e\u4e48\u4f7f\u7528\u5b83\u4eec\u4e86\u3002<\/p>\n<p>\u6211\u4eec\u7684\u4efb\u52a1\u8fd8\u662f\u626b\u63cf\u7aef\u53e3\uff08\u8bf7\u53c2\u8003\u6211\u7684\u201c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u201d\uff09\uff0c\u6587\u4ef6\u540d\u4e3aTestThreadPool.java\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n[java]import java.net.InetAddress;<br \/>\nimport java.net.Socket;<\/p>\n<p>public class TestThreadPool {<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        String host = null;        \/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u76ee\u6807\u4e3b\u673a\u3002<br \/>\n        int beginport = 1;         \/\/\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5f00\u59cb\u7aef\u53e3\u3002<br \/>\n        int endport = 65535;       \/\/\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u7ed3\u675f\u7aef\u53e3\u3002<br \/>\n        try{<br \/>\n            host = args[0];<br \/>\n            beginport = Integer.parseInt(args[1]);<br \/>\n            endport = Integer.parseInt(args[2]);<br \/>\n            if(beginport &lt;= 0 || endport &gt;= 65536 || beginport &gt; endport){<br \/>\n                throw new Exception(&quot;Port is illegal&quot;);<br \/>\n            }<br \/>\n        }catch(Exception e){<br \/>\n            System.out.println(&quot;Usage: java PortScannerSingleThread host beginport endport&quot;);<br \/>\n            System.exit(0);<br \/>\n        }<\/p>\n<p>        ThreadPool tp = new ThreadPool();<br \/>\n        tp.setMaxThreads(100);<br \/>\n        tp.setMaxSpareThreads(50);<br \/>\n        tp.setMinSpareThreads(10);<br \/>\n        tp.start();<\/p>\n<p>        for(int i = beginport; i &lt;= endport; i++){<br \/>\n            Task task = new Task(host,i);<br \/>\n            tp.runIt(task);<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>class Task implements ThreadPoolRunnable{<br \/>\n    String host;<br \/>\n    int port;<\/p>\n<p>    Task(String host, int port){<br \/>\n        this.host = host;<br \/>\n        this.port = port;<br \/>\n    }<\/p>\n<p>    public Object[] getInitData(){<br \/>\n        return null;<br \/>\n    }<\/p>\n<p>    public void runIt(Object thData[]){<br \/>\n        Socket s = null;<br \/>\n        try{<br \/>\n            s = new Socket(InetAddress.getByName(host),port);<br \/>\n            System.out.println(&quot;The port &quot; + port + &quot; at &quot; + host + &quot; is open.&quot;);<br \/>\n        }catch(Exception e){<\/p>\n<p>        }finally{<br \/>\n            try{<br \/>\n                if(s != null) s.close();<br \/>\n            }catch(Exception e){<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}[\/java]<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\u5728TestThreadPool\u7c7b\u7684main\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4e86\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u76ee\u6807\u4e3b\u673aIP\u5730\u5740\uff0c\u5f00\u59cb\u7aef\u53e3\u548c\u7ed3\u675f\u7aef\u53e3\u3002\u7136\u540e\u901a\u8fc7new ThreadPool()\u521b\u5efa\u7ebf\u7a0b\u6c60\u7c7b\uff0c\u5e76\u901a\u8fc7setMaxThreads\u3001setMaxSpareThreads\u548csetMinSpareThreads\u8bbe\u7f6e\u7ebf\u7a0b\u6c60\u7684maxThreads\u3001maxSpareThreads\u548cminSpareThreads\u53c2\u6570\u3002Task\u7c7b\u901a\u8fc7\u5b9e\u73b0ThreadPoolRunnable\u63a5\u53e3\uff0c\u5728runIt\u4e2d\u5b9a\u4e49\u4e86\u5177\u4f53\u5185\u5bb9\uff08\u521b\u5efaSocket\u5bf9\u8c61\u8fbe\u5230\u626b\u63cf\u7aef\u53e3\u7684\u76ee\u7684\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u90fd\u5728JDK1.4.2\u7684\u73af\u5883\u4e0b\u7f16\u8bd1\u5e76\u8fd0\u884c\u901a\u8fc7\uff0c\u8f93\u5165 java TestThreadPool 10.1.1.182 1 10000 \u8fd0\u884c\u5f97\u51fa\u5982\u4e0b\u7ed3\u679c\uff1a<br \/>\nThe port 25 at 10.1.1.182 is open.<br \/>\nThe port 110 at 10.1.1.182 is open.<br \/>\nThe port 135 at 10.1.1.182 is open.<br \/>\n&#8230;<br \/>\n\u901a\u8fc7\u4ee5\u4e0a\u7684\u7ebf\u7a0b\u6c60\u7c7b\u3001\u7ebf\u7a0b\u6c60\u63a5\u53e3\u7684\u5206\u6790\u548c\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u5728\u7406\u89e3\u7684\u57fa\u7840\u4e0a\uff0c\u76f4\u63a5\u628a\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u7c7b\u548c\u63a5\u53e3\u62ff\u6765\uff0c\u5e94\u7528\u5230\u81ea\u5df1\u7684\u5f00\u53d1\u9879\u76ee\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u201c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u201d\u6587\u7ae0\u53d1\u5e03\u540e\uff0c\u6700\u8fd1\u53c8\u7ee7\u7eed\u7814\u7a76\uff0c\u63a8\u51fa\u4e00\u4e2a\u6bd4\u8f83\u590d\u6742\u4f46\u529f\u80fd\u6bd4\u8f83\u5b8c\u5584\uff0c\u800c\u4e14\u53ef\u4ee5\u76f4\u63a5\u5957\u7528\u7684\u7ebf\u7a0b\u6c60\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":308,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/308"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}