{"id":160,"date":"2015-04-10T19:23:51","date_gmt":"2015-04-10T11:23:51","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=160"},"modified":"2026-08-18T12:09:28","modified_gmt":"2026-08-18T04:09:28","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":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u7ee7\u300c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u300d\u4e4b\u540e\uff0c\u518d\u7ed9\u51fa\u4e00\u4e2a\u66f4\u590d\u6742\u3001\u4f46\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u3001\u53ef\u4ee5\u76f4\u63a5\u5957\u7528\u7684\u7ebf\u7a0b\u6c60\u3002\u5b9e\u4f8b\u6539\u5199\u81ea Apache \u9879\u76ee\u6e90\u7801\uff08\u539f\u6587\u7ea6 800 \u884c\uff0c\u8dd1\u5728 Tomcat \u7b49\u670d\u52a1\u5668\u4e0a\uff09\uff0c\u8fd9\u91cc\u505a\u4e86\u7cbe\u7b80\uff0c\u53ea\u7559\u6838\u5fc3\uff0c\u5e76\u914d\u6d4b\u8bd5\u7c7b\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5b66\u5b8c\u53ef\u4ee5\u76f4\u63a5\u6a21\u4eff\u5957\u7528\uff0c\u4e0d\u5fc5\u81ea\u5df1\u4ece\u96f6\u518d\u5199\u4e00\u904d\u6c60\u3002\u5173\u952e\u4e24\u5757\uff1a<strong style=\"color: #4870ac;\">\u7ebf\u7a0b\u6c60\u7c7b ThreadPool<\/strong> \u548c <strong style=\"color: #4870ac;\">\u63a5\u53e3 ThreadPoolRunnable<\/strong>\u3002<\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001ThreadPool\uff1a\u53ef\u4f38\u7f29\u7684\u5de5\u4eba\u6c60<\/h2>\n<p style=\"margin: 1em 0px;\">\u6587\u4ef6 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ThreadPool.java<\/code> \u4e24\u767e\u591a\u884c\u3002\u5148\u770b\u5b8c\u6574\u6e90\u7801\uff0c\u518d\u62c6 openThreads\u3001runIt \u4ee5\u53ca\u4e24\u4e2a\u5185\u90e8\u7c7b\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import java.util.Vector;\n\n\n\npublic class ThreadPool {\n\n    public static final int MAX_THREADS = 100;\n\n    public static final int MAX_SPARE_THREADS = 50;\n\n    public static final int MIN_SPARE_THREADS = 10;\n\n    public static final int WORK_WAIT_TIMEOUT = 60 * 1000;\n\n    \n\n    protected Vector pool;\n\n    protected MonitorRunnable monitor;\n\n    protected int maxThreads;\n\n    protected int minSpareThreads;\n\n    protected int maxSpareThreads;\n\n    protected int currentThreadCount;\n\n    protected int currentThreadsBusy;\n\n    protected boolean stopThePool;\n\n\n\n    public ThreadPool() {\n\n        maxThreads = MAX_THREADS;\n\n        maxSpareThreads = MAX_SPARE_THREADS;\n\n        minSpareThreads = MIN_SPARE_THREADS;\n\n        currentThreadCount = 0;\n\n        currentThreadsBusy = 0;\n\n        stopThePool = false;\n\n    }\n\n\n\n    public synchronized void start() {\n\n        adjustLimits();\n\n        openThreads(minSpareThreads);\n\n        monitor = new MonitorRunnable(this);\n\n    }\n\n\n\n    public void setMaxThreads(int maxThreads) {\n\n        this.maxThreads = maxThreads;\n\n    }\n\n\n\n    public int getMaxThreads() {\n\n        return maxThreads;\n\n    }\n\n\n\n    public void setMinSpareThreads(int minSpareThreads) {\n\n        this.minSpareThreads = minSpareThreads;\n\n    }\n\n\n\n    public int getMinSpareThreads() {\n\n        return minSpareThreads;\n\n    }\n\n\n\n    public void setMaxSpareThreads(int maxSpareThreads) {\n\n        this.maxSpareThreads = maxSpareThreads;\n\n    }\n\n\n\n    public int getMaxSpareThreads() {\n\n        return maxSpareThreads;\n\n    }\n\n    \n\n    public void runIt(ThreadPoolRunnable r) {\n\n        if (null == r) {\n\n            throw new NullPointerException();\n\n        }\n\n        if (0 == currentThreadCount || stopThePool) {\n\n            throw new IllegalStateException();\n\n        }\n\n        ControlRunnable c = null;\n\n        synchronized (this) {\n\n            if (currentThreadsBusy == currentThreadCount) {\n\n                if (currentThreadCount &lt; maxThreads) {\n\n                    int toOpen = currentThreadCount + minSpareThreads;\n\n                    openThreads(toOpen);\n\n                } else {\n\n                    while (currentThreadsBusy == currentThreadCount) {\n\n                        try {\n\n                            this.wait();\n\n                        }catch (InterruptedException e) {\n\n                        }\n\n                        if (0 == currentThreadCount || stopThePool) {\n\n                            throw new IllegalStateException();\n\n                        }\n\n                    }\n\n                }\n\n            }\n\n            c = (ControlRunnable) pool.lastElement();\n\n            pool.removeElement(c);\n\n            currentThreadsBusy++;\n\n        }\n\n        c.runIt(r);\n\n    }\n\n   \n\n    public synchronized void shutdown() {\n\n        if (!stopThePool) {\n\n            stopThePool = true;\n\n            monitor.terminate();\n\n            monitor = null;\n\n            for (int i = 0; i &lt; (currentThreadCount - currentThreadsBusy); i++) {\n\n                try {\n\n                    ((ControlRunnable) (pool.elementAt(i))).terminate();\n\n                } catch (Throwable t) {\n\n                }\n\n            }\n\n            currentThreadsBusy = currentThreadCount = 0;\n\n            pool = null;\n\n            notifyAll();\n\n        }\n\n    }\n\n    \n\n    protected synchronized void checkSpareControllers() {\n\n        if (stopThePool) {\n\n            return;\n\n        }\n\n        \n\n        if ((currentThreadCount - currentThreadsBusy) &gt; maxSpareThreads) {\n\n            int toFree = currentThreadCount - currentThreadsBusy - maxSpareThreads;\n\n            for (int i = 0; i &lt; toFree; i++) {\n\n                ControlRunnable c = (ControlRunnable) pool.firstElement();\n\n                pool.removeElement(c);\n\n                c.terminate();\n\n                currentThreadCount--;\n\n            }\n\n        }\n\n    }\n\n    \n\n    protected synchronized void returnController(ControlRunnable c) {\n\n        if (0 == currentThreadCount || stopThePool) {\n\n            c.terminate();\n\n            return;\n\n        }\n\n        currentThreadsBusy--;\n\n        \n\n        pool.addElement(c);\n\n        notify();\n\n    }\n\n    \n\n    protected synchronized void notifyThreadEnd() {\n\n        currentThreadsBusy--;\n\n        currentThreadCount--;\n\n        notify();\n\n        openThreads(minSpareThreads);\n\n    }\n\n    \n\n    protected void adjustLimits() {\n\n        if (maxThreads &lt;= 0) {\n\n            maxThreads = MAX_THREADS;\n\n        }\n\n        if (maxSpareThreads &gt;= maxThreads) {\n\n            maxSpareThreads = maxThreads;\n\n        }\n\n        if (maxSpareThreads &lt;= 0) {\n\n            if (1 == maxThreads) {\n\n                maxSpareThreads = 1;\n\n            } else {\n\n                maxSpareThreads = maxThreads \/ 2;\n\n            }\n\n        }\n\n        if (minSpareThreads &gt; maxSpareThreads) {\n\n            minSpareThreads = maxSpareThreads;\n\n        }\n\n        if (minSpareThreads &lt;= 0) {\n\n            if (1 == maxSpareThreads) {\n\n                minSpareThreads = 1;\n\n            } else {\n\n                minSpareThreads = maxSpareThreads \/ 2;\n\n            }\n\n        }\n\n    }\n\n\n\n    protected void openThreads(int toOpen) {\n\n        if (toOpen &gt; maxThreads) {\n\n            toOpen = maxThreads;\n\n        }\n\n        if (0 == currentThreadCount) {\n\n            pool = new Vector(toOpen);\n\n        }\n\n        for (int i = currentThreadCount; i &lt; toOpen; i++) {\n\n            pool.addElement(new ControlRunnable(this));\n\n        }\n\n        currentThreadCount = toOpen;\n\n    }\n\n    \n\n    class MonitorRunnable implements Runnable {\n\n        ThreadPool p;\n\n        Thread t;\n\n        boolean shouldTerminate;\n\n        MonitorRunnable(ThreadPool p) {\n\n            shouldTerminate = false;\n\n            this.p = p;\n\n            t = new Thread(this);\n\n            t.start();\n\n        }\n\n        public void run() {\n\n            while (true) {\n\n                try {\n\n                    synchronized (this) {\n\n                        this.wait(WORK_WAIT_TIMEOUT);\n\n                    }\n\n                    if (shouldTerminate) {\n\n                        break;\n\n                    }\n\n                    p.checkSpareControllers();\n\n                } catch (Throwable t) {\n\n                    t.printStackTrace();\n\n                }\n\n            }\n\n        }\n\n        \n\n        public synchronized void terminate() {\n\n            shouldTerminate = true;\n\n            this.notify();\n\n        }\n\n    }\n\n    \n\n    class ControlRunnable implements Runnable {\n\n        ThreadPool p;\n\n        Thread t;\n\n        ThreadPoolRunnable toRun;\n\n        boolean shouldTerminate;\n\n        boolean shouldRun;\n\n        boolean noThData;\n\n        Object thData[] = null;\n\n\n\n        ControlRunnable(ThreadPool p) {\n\n            toRun = null;\n\n            shouldTerminate = false;\n\n            shouldRun = false;\n\n            this.p = p;\n\n            t = new Thread(this);\n\n            t.start();\n\n            noThData = true;\n\n            thData = null;\n\n        }\n\n\n\n        public void run() {\n\n            while (true) {\n\n                try {\n\n                    synchronized (this) {\n\n                        if (!shouldRun &amp;&amp; !shouldTerminate) {\n\n                            this.wait();\n\n                        }\n\n                    }\n\n                    if (shouldTerminate) {\n\n                        break;\n\n                    }\n\n                    try {\n\n                        if (noThData) {\n\n                            thData = toRun.getInitData();\n\n                            noThData = false;\n\n                        }\n\n                        if (shouldRun) {\n\n                            toRun.runIt(thData);\n\n                        }\n\n                    } catch (Throwable t) {\n\n                        System.err.println(\"ControlRunnable Throwable: \");\n\n                        t.printStackTrace();\n\n                        shouldTerminate = true;\n\n                        shouldRun = false;\n\n                        p.notifyThreadEnd();\n\n                    } finally {\n\n                        if (shouldRun) { \n\n                            shouldRun = false;\n\n                            p.returnController(this);\n\n                        }\n\n                    }\n\n                    if (shouldTerminate) {\n\n                        break;\n\n                    }\n\n                } catch (InterruptedException ie) {\n\n                }\n\n            }\n\n        }\n\n\n\n        public synchronized void runIt(ThreadPoolRunnable toRun) {\n\n            if (toRun == null) {\n\n                throw new NullPointerException(\"No Runnable\");\n\n            }\n\n            this.toRun = toRun;\n\n            shouldRun = true;\n\n            this.notify();\n\n        }\n\n\n\n        public synchronized void terminate() {\n\n            shouldTerminate = true;\n\n            this.notify();\n\n        }\n\n    }\n\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u542f\u52a8\u65f6\u5f80 Vector \u91cc\u88c5 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">minSpareThreads<\/code> \u4e2a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ControlRunnable<\/code>\uff0c\u6784\u9020\u65b9\u6cd5\u91cc\u5c31\u4f1a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">t.start()<\/code>\u3002\u82e5 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">shouldRun<\/code> \u548c <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">shouldTerminate<\/code> \u90fd\u662f false\uff0c\u5de5\u4eba\u5c31 wait\uff1bshouldRun \u4e3a true \u65f6\u8c03\u7528\u4efb\u52a1\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">runIt(Object[])<\/code>\u2014\u2014\u8fd9\u5c31\u662f\u4f60\u8981\u8986\u76d6\u7684\u65b9\u6cd5\u3002<\/p>\n<p style=\"margin: 1em 0px;\">minSpareThreads \u4e2a\u5de5\u4eba\u90fd Busy \u4e4b\u540e\uff0c\u6c60\u4f1a\u518d\u521b\u5efa minSpareThreads \u4e2a\u3002<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">MonitorRunnable<\/code> \u6bcf\u9694 60 \u79d2\uff08<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">WORK_WAIT_TIMEOUT<\/code>\uff09\u8c03\u4e00\u6b21 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">checkSpareControllers<\/code>\uff1a\u82e5\u7a7a\u95f2\u5de5\u4eba\u591a\u4e8e <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">maxSpareThreads<\/code>\uff0c\u5c31 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">terminate<\/code> \u591a\u51fa\u6765\u7684\u7a7a\u95f2\u7ebf\u7a0b\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u53c2\u6570<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u9ed8\u8ba4<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">MAX_THREADS \/ maxThreads<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">100<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6c60\u4e0a\u9650<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">MAX_SPARE_THREADS \/ maxSpareThreads<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">50<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7a7a\u95f2\u5de5\u4eba\u4e0a\u9650\uff0c\u591a\u4e86\u5c31\u6536\u56de<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">MIN_SPARE_THREADS \/ minSpareThreads<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">10<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u542f\u52a8\u65f6\u9884\u521b\u5efa\uff0c\u5fd9\u65f6\u4e5f\u6309\u8fd9\u4e2a\u7c92\u5ea6\u6269<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">WORK_WAIT_TIMEOUT<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">60s<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u76d1\u89c6\u7ebf\u7a0b\u68c0\u67e5\u7a7a\u95f2\u7684\u95f4\u9694<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u4efb\u52a1\u63a5\u53e3 ThreadPoolRunnable<\/h2>\n<p style=\"margin: 1em 0px;\">\u63a5\u53e3\u53ea\u6709\u4e24\u4e2a\u7a7a\u65b9\u6cd5\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">getInitData<\/code> \u548c <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">runIt<\/code>\u3002\u81ea\u5df1\u5199\u4e00\u4e2a\u4efb\u52a1\u7c7b\u5b9e\u73b0\u5b83\uff0c\u628a\u6d3b\u653e\u8fdb <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">runIt<\/code>\u3002\u4e0d\u7528\u521d\u59cb\u5316\u6570\u636e\u5c31\u8ba9 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">getInitData<\/code> \u8fd4\u56de null\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public interface ThreadPoolRunnable {\n\n    public Object[] getInitData();\n\n    \n\n    public void runIt(Object thData[]);\n\n\n\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u5957\u7528\uff1a\u8fd8\u662f\u626b\u7aef\u53e3<\/h2>\n<p style=\"margin: 1em 0px;\">\u6d4b\u8bd5\u7c7b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">TestThreadPool.java<\/code>\uff0c\u4efb\u52a1\u4ecd\u662f\u626b\u63cf\u7aef\u53e3\uff08\u53c2\u89c1\u300c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u300d\uff09\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import java.net.InetAddress;\n\nimport java.net.Socket;\n\n\n\npublic class TestThreadPool {\n\n\n\n    public static void main(String[] args) {\n\n        String host = null;        \/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u76ee\u6807\u4e3b\u673a\u3002\n\n        int beginport = 1;         \/\/\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5f00\u59cb\u7aef\u53e3\u3002\n\n        int endport = 65535;       \/\/\u7b2c\u4e09\u4e2a\u53c2\u6570\uff0c\u7ed3\u675f\u7aef\u53e3\u3002\n\n        try{\n\n            host = args[0];\n\n            beginport = Integer.parseInt(args[1]);\n\n            endport = Integer.parseInt(args[2]);\n\n            if(beginport &lt;= 0 || endport &gt;= 65536 || beginport &gt; endport){\n\n                throw new Exception(\"Port is illegal\");\n\n            }\n\n        }catch(Exception e){\n\n            System.out.println(\"Usage: java PortScannerSingleThread host beginport endport\");\n\n            System.exit(0);\n\n        }\n\n        \n\n        ThreadPool tp = new ThreadPool();\n\n        tp.setMaxThreads(100);\n\n        tp.setMaxSpareThreads(50);\n\n        tp.setMinSpareThreads(10);\n\n        tp.start();\n\n        \n\n        for(int i = beginport; i &lt;= endport; i++){\n\n            Task task = new Task(host,i);\n\n            tp.runIt(task);\n\n        }\n\n    }\n\n\n\n}\n\n\n\nclass Task implements ThreadPoolRunnable{\n\n    String host;\n\n    int port;\n\n    \n\n    Task(String host, int port){\n\n        this.host = host;\n\n        this.port = port;\n\n    }\n\n    \n\n    public Object[] getInitData(){\n\n        return null;\n\n    }\n\n    \n\n    public void runIt(Object thData[]){\n\n        Socket s = null;\n\n        try{\n\n            s = new Socket(InetAddress.getByName(host),port);\n\n            System.out.println(\"The port \" + port + \" at \" + host + \" is open.\");\n\n        }catch(Exception e){\n\n            \n\n        }finally{\n\n            try{\n\n                if(s != null) s.close();\n\n            }catch(Exception e){\n\n            }\n\n        }\n\n    }\n\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">main \u91cc\u4e09\u4e2a\u53c2\u6570\uff1a\u76ee\u6807\u4e3b\u673a\u3001\u8d77\u6b62\u7aef\u53e3\u3002\u7136\u540e <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">new ThreadPool()<\/code>\uff0c\u7528 setter \u8c03 maxThreads \/ maxSpareThreads \/ minSpareThreads\uff0c\u518d <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">start()<\/code>\u3002Task \u5b9e\u73b0\u63a5\u53e3\uff0c\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">runIt<\/code> \u91cc new Socket \u5b8c\u6210\u626b\u63cf\u3002<\/p>\n<p style=\"margin: 1em 0px;\">JDK 1.4.2 \u7f16\u8bd1\u8fd0\u884c\u901a\u8fc7\u3002\u8f93\u5165 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">java TestThreadPool 10.1.1.182 1 10000<\/code>\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">The port 25 at 10.1.1.182 is open.\nThe port 110 at 10.1.1.182 is open.\nThe port 135 at 10.1.1.182 is open.\n...<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u628a ThreadPool \u548c ThreadPoolRunnable \u76f4\u63a5\u62f7\u8fdb\u81ea\u5df1\u7684\u9879\u76ee\u5373\u53ef\uff1a\u4efb\u52a1\u7c7b\u5b9e\u73b0\u63a5\u53e3\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">runIt<\/code> \u91cc\u5199\u4e1a\u52a1\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u5de5\u4eba\u4e0d\u591f\u5c31\u6309 minSpare \u6269\uff0c\u7a7a\u95f2\u592a\u591a\u5c31\u6309 maxSpare \u6536\uff1b\u4f60\u7684\u4efb\u52a1\u53ea\u9700\u5b9e\u73b0 runIt\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u300c\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u6a21\u578b\u7814\u7a76\u300d\u4e4b\u540e\uff0c\u518d\u7ed9\u51fa\u4e00\u4e2a\u66f4\u590d\u6742\u3001\u4f46\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u3001\u53ef\u4ee5\u76f4\u63a5\u5957\u7528\u7684\u7ebf\u7a0b\u6c60\u3002\u5b9e\u4f8b\u6539\u5199\u81ea Apache [&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":[178,44],"_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}]}}