{"id":3970,"date":"2018-06-09T08:59:38","date_gmt":"2018-06-09T00:59:38","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3970"},"modified":"2018-06-09T08:59:38","modified_gmt":"2018-06-09T00:59:38","slug":"java%e7%ba%bf%e7%a8%8b%e6%b1%a0%e7%9a%84%e4%bd%bf%e7%94%a8%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3970.html","title":{"rendered":"Java\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\u5b66\u4e60"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5728\u4e4b\u524d\u8bb0\u5f55\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7ebf\u7a0b\u7684\u65f6\u5019\u5c31\u53bb\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u6837\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u7b80\u4fbf\uff0c\u4f46\u662f\u5c31\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898\uff1a<br \/>\n\u5982\u679c\u5e76\u53d1\u7684\u7ebf\u7a0b\u6570\u91cf\u5f88\u591a\uff0c\u5e76\u4e14\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u662f\u6267\u884c\u4e00\u4e2a\u65f6\u95f4\u5f88\u77ed\u7684\u4efb\u52a1\u5c31\u7ed3\u675f\u4e86\uff0c\u8fd9\u6837\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u5c31\u4f1a\u5927\u5927\u964d\u4f4e\u7cfb\u7edf\u7684\u6548\u7387\uff0c\u56e0\u4e3a\u9891\u7e41\u521b\u5efa\u7ebf\u7a0b\u548c\u9500\u6bc1\u7ebf\u7a0b\u9700\u8981\u65f6\u95f4\u3002<\/p>\n<p>\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff0c\u6240\u4ee5\u5b66\u4e60\u4e00\u4e0bJava\u4e2d\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3001\u67e5\u9605\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong>\u4e00\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff08\u76f8\u8f83\u4e8enew Thread\u7684\u65b9\u5f0f\u6765\u8bf4\uff09\u7684\u597d\u5904\uff1a<\/strong><\/h6>\n<ul>\n<li>\u964d\u4f4e\u8d44\u6e90\u6d88\u8017\uff1a\u901a\u8fc7\u91cd\u590d\u5229\u7528\u5df2\u521b\u5efa\u7684\u7ebf\u7a0b\u964d\u4f4e\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u9020\u6210\u7684\u6d88\u8017\u3002<\/li>\n<li>\u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6\uff1a\u5f53\u4efb\u52a1\u5230\u8fbe\u65f6\uff0c\u4efb\u52a1\u53ef\u4ee5\u4e0d\u9700\u8981\u7684\u7b49\u5230\u7ebf\u7a0b\u521b\u5efa\u5c31\u80fd\u7acb\u5373\u6267\u884c\u3002<\/li>\n<li>\u63d0\u9ad8\u7ebf\u7a0b\u7684\u53ef\u7ba1\u7406\u6027\uff1a\u7ebf\u7a0b\u662f\u7a00\u7f3a\u8d44\u6e90\uff0c\u5982\u679c\u65e0\u9650\u5236\u7684\u521b\u5efa\uff0c\u4e0d\u4ec5\u4f1a\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90\uff0c\u8fd8\u4f1a\u964d\u4f4e\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u8fdb\u884c\u7edf\u4e00\u7684\u5206\u914d\u3001\u8c03\u4f18\u548c\u76d1\u63a7\u3002<\/li>\n<\/ul>\n<h6><strong>\u4e8c\u3001\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\u539f\u7406<\/strong><\/h6>\n<p>\u5f53\u4e00\u4e2a\u65b0\u4efb\u52a1\u63d0\u4ea4\u5230\u7ebf\u7a0b\u6c60\u65f6\uff0c\u7b80\u5355\u6765\u8bf4\u7ebf\u7a0b\u6c60\u7684\u5904\u7406\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u6838\u5fc3\u7ebf\u7a0b\u6c60\u91cc\u7684\u7ebf\u7a0b\u662f\u5426\u90fd\u5728\u6267\u884c\u4efb\u52a1\uff0c\u5982\u679c\u4e0d\u662f\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7ebf\u7a0b\u5904\u7406\u4efb\u52a1\uff0c\u5426\u5219\u8fdb\u5165\u4e0b\u4e2a\u6d41\u7a0b\uff1b<\/li>\n<li>\u5224\u65ad\u5de5\u4f5c\u961f\u5217\u662f\u5426\u5df2\u6ee1\uff0c\u5982\u679c\u672a\u6ee1\u5219\u5c06\u65b0\u63d0\u4ea4\u7684\u4efb\u52a1\u5b58\u50a8\u5728\u8be5\u5de5\u4f5c\u961f\u5217\uff0c\u5426\u5219\u8fdb\u5165\u4e0b\u4e2a\u6d41\u7a0b\uff1b<\/li>\n<li>\u5224\u65ad\u7ebf\u7a0b\u6c60\u91cc\u7684\u7ebf\u7a0b\u662f\u5426\u90fd\u5904\u4e8e\u5de5\u4f5c\u72b6\u6001\uff0c\u5982\u679c\u4e0d\u662f\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff0c\u5426\u5219\u4ea4\u7531\u9971\u548c\u7b56\u7565\u5904\u7406\u3002<\/li>\n<\/ol>\n<h6><strong>\u4e09\u3001Java\u4e2d\u7684ThreadPoolExecutor\u7c7b<\/strong><\/h6>\n<p>\u5177\u4f53\u7ec6\u8282\u8fd8\u662f\u770b\u4e0b\u9762\u7684\u300c\u53c2\u8003\u94fe\u63a5\u300d\u4e2d\u7684\u4ecb\u7ecd\u5427\uff0c\u66f4\u6df1\u5165\u8be6\u7ec6\u3002<\/p>\n<h6><strong>\u56db\u3001\u4f7f\u7528\u793a\u4f8b<\/strong><\/h6>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.misc;\r\n\r\nimport com.google.common.util.concurrent.ThreadFactoryBuilder;\r\nimport java.util.concurrent.*;\r\n\r\n\/**\r\n public ThreadPoolExecutor(int corePoolSize,\r\n int maximumPoolSize,\r\n long keepAliveTime,\r\n TimeUnit unit,\r\n BlockingQueue&lt;Runnable&gt; workQueue,\r\n ThreadFactory threadFactory,\r\n RejectedExecutionHandler handler)\r\n\r\n new ThreadPoolExecutor(\"\u7ebf\u7a0b\u6c60\u57fa\u672c\u5927\u5c0f\", \"\u7ebf\u7a0b\u6c60\u6700\u5927\u5927\u5c0f\", \"\u7ebf\u7a0b\u4fdd\u6301\u5b58\u6d3b\u7684\u65f6\u95f4\", \"\u65f6\u95f4\u5355\u4f4d\", \"\u4efb\u52a1\u961f\u5217\", \"\u7ebf\u7a0b\u5de5\u5382\", \"\u9971\u548c\u7b56\u7565\");\r\n *\/\r\npublic class ThreadPoolDemo {\r\n    \/**\r\n     * \u5b9a\u4e49\u9759\u6001\u5185\u90e8\u7ebf\u7a0b\u7c7b\r\n     *\/\r\n    public static class MyTask implements Runnable {\r\n\r\n        @Override\r\n        public void run() {\r\n            System.out.println(System.currentTimeMillis() + \":Thread name:\"\r\n                    + Thread.currentThread().getName());\r\n            try {\r\n                Thread.sleep(1000);\r\n            } catch (InterruptedException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        System.out.println(\"Start @ \" + System.currentTimeMillis());\r\n        MyTask myTask = new MyTask();\r\n        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()\r\n                .setNameFormat(\"demo-pool-%d\").build();\r\n        ExecutorService executorService = new ThreadPoolExecutor(5, 10,\r\n                0L, TimeUnit.MILLISECONDS,\r\n                new LinkedBlockingDeque&lt;Runnable&gt;(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());\r\n        for (int i = 0; i &lt; 10; i++) {\r\n            executorService.execute(myTask);\r\n        }\r\n        executorService.shutdown();\r\n        System.out.println(\"End @ \" + System.currentTimeMillis());\r\n    }\r\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">package com.ixyzero.learn.misc;\r\n\r\nimport java.util.concurrent.ArrayBlockingQueue;\r\nimport java.util.concurrent.ThreadPoolExecutor;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\npublic class ThreadPoolDemo2 {\r\n    public static void main(String[] args) {\r\n        long start = System.currentTimeMillis();\r\n        System.out.println(\"Start @ \" + start);\r\n        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,\r\n                new ArrayBlockingQueue&lt;Runnable&gt;(5));\r\n\r\n        for(int i=0; i&lt;15; i++) {\r\n            MyTask myTask = new MyTask(i);\r\n            executor.execute(myTask);\r\n            System.out.println(\"\u7ebf\u7a0b\u6c60\u4e2d\u7ebf\u7a0b\u6570\u76ee\uff1a\" + executor.getPoolSize() + \"\uff0c\u961f\u5217\u4e2d\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u6570\u76ee\uff1a\" +\r\n                    executor.getQueue().size() + \"\uff0c\u5df2\u6267\u884c\u5b8c\u7684\u4efb\u52a1\u6570\u76ee\uff1a\" + executor.getCompletedTaskCount());\r\n        }\r\n        executor.shutdown();\r\n        long end = System.currentTimeMillis();\r\n        System.out.println(\"End @ \" + end + \"\\nSpend \" + (end - start) + \" ms.\");\r\n    }\r\n}\r\n\r\nclass MyTask implements Runnable {\r\n    private int taskNum;\r\n\r\n    public MyTask(int num) {\r\n        this.taskNum = num;\r\n    }\r\n\r\n    @Override\r\n    public void run() {\r\n        System.out.println(\"\u6b63\u5728\u6267\u884ctask \" + taskNum);\r\n        try {\r\n            Thread.currentThread().sleep(4000);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n        System.out.println(\"task \"+taskNum+\" \u6267\u884c\u5b8c\u6bd5@\" + System.currentTimeMillis());\r\n    }\r\n}<\/pre>\n<p>\u4ece\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7ebf\u7a0b\u6c60\u4e2d\u7ebf\u7a0b\u7684\u6570\u76ee\u5927\u4e8e5\u65f6\uff0c\u4fbf\u5c06\u4efb\u52a1\u653e\u5165\u4efb\u52a1\u7f13\u5b58\u961f\u5217\u91cc\u9762\uff0c\u5f53\u4efb\u52a1\u7f13\u5b58\u961f\u5217\u6ee1\u4e86\u4e4b\u540e\uff0c\u4fbf\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u3002\u5982\u679c\u4e0a\u9762\u7a0b\u5e8f\u4e2d\uff0c\u5c06for\u5faa\u73af\u4e2d\u6539\u6210\u6267\u884c20\u4e2a\u4efb\u52a1\uff0c\u5c31\u4f1a\u629b\u51fa\u4efb\u52a1\u62d2\u7edd\u5f02\u5e38\u4e86\u3002<\/p>\n<hr \/>\n<p>\u4e0d\u8fc7\u5728Java doc\u4e2d\uff0c\u5e76\u4e0d\u63d0\u5021\u6211\u4eec\u76f4\u63a5\u4f7f\u7528ThreadPoolExecutor\uff0c\u800c\u662f\u4f7f\u7528Executors\u7c7b\u4e2d\u63d0\u4f9b\u7684\u51e0\u4e2a\u9759\u6001\u65b9\u6cd5\u6765\u521b\u5efa\u7ebf\u7a0b\u6c60\uff1a<\/p>\n<pre class=\"lang:default decode:true \">Executors.newCachedThreadPool();      \/\/\u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u6c60\uff0c\u7f13\u51b2\u6c60\u5bb9\u91cf\u5927\u5c0f\u4e3aInteger.MAX_VALUE\r\nExecutors.newSingleThreadExecutor();  \/\/\u521b\u5efa\u5bb9\u91cf\u4e3a1\u7684\u7f13\u51b2\u6c60\r\nExecutors.newFixedThreadPool(int);     \/\/\u521b\u5efa\u56fa\u5b9a\u5bb9\u91cf\u5927\u5c0f\u7684\u7f13\u51b2\u6c60\r\n<\/pre>\n<p>\u4ece\u5b83\u4eec\u7684\u5177\u4f53\u5b9e\u73b0\u6765\u770b\uff0c\u5b83\u4eec\u5b9e\u9645\u4e0a\u4e5f\u662f\u8c03\u7528\u4e86ThreadPoolExecutor\uff0c\u53ea\u4e0d\u8fc7\u53c2\u6570\u90fd\u5df2\u914d\u7f6e\u597d\u4e86\u3002<\/p>\n<ul>\n<li>newFixedThreadPool\u521b\u5efa\u7684\u7ebf\u7a0b\u6c60corePoolSize\u548cmaximumPoolSize\u503c\u662f\u76f8\u7b49\u7684\uff0c\u5b83\u4f7f\u7528\u7684LinkedBlockingQueue\uff1b<\/li>\n<li>newSingleThreadExecutor\u5c06corePoolSize\u548cmaximumPoolSize\u90fd\u8bbe\u7f6e\u4e3a1\uff0c\u4e5f\u4f7f\u7528\u7684LinkedBlockingQueue\uff1b<\/li>\n<li>newCachedThreadPool\u5c06corePoolSize\u8bbe\u7f6e\u4e3a0\uff0c\u5c06maximumPoolSize\u8bbe\u7f6e\u4e3aInteger.MAX_VALUE\uff0c\u4f7f\u7528\u7684SynchronousQueue\uff0c\u4e5f\u5c31\u662f\u8bf4\u6765\u4e86\u4efb\u52a1\u5c31\u521b\u5efa\u7ebf\u7a0b\u8fd0\u884c\uff0c\u5f53\u7ebf\u7a0b\u7a7a\u95f2\u8d85\u8fc760\u79d2\uff0c\u5c31\u9500\u6bc1\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<p><span style=\"color: #ff0000;\"><strong>\u5b9e\u9645\u4e2d\uff0c\u5982\u679cExecutors\u63d0\u4f9b\u7684\u4e09\u4e2a\u9759\u6001\u65b9\u6cd5\u80fd\u6ee1\u8db3\u8981\u6c42\uff0c\u5c31\u5c3d\u91cf\u4f7f\u7528\u5b83\u63d0\u4f9b\u7684\u4e09\u4e2a\u65b9\u6cd5\uff0c\u56e0\u4e3a\u81ea\u5df1\u53bb\u624b\u52a8\u914d\u7f6eThreadPoolExecutor\u7684\u53c2\u6570\u6709\u70b9\u9ebb\u70e6\uff0c\u8981\u6839\u636e\u5b9e\u9645\u4efb\u52a1\u7684\u7c7b\u578b\u548c\u6570\u91cf\u6765\u8fdb\u884c\u914d\u7f6e\u3002<\/strong><\/span><\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679cThreadPoolExecutor\u8fbe\u4e0d\u5230\u8981\u6c42\uff0c\u53ef\u4ee5\u81ea\u5df1\u7ee7\u627fThreadPoolExecutor\u7c7b\u8fdb\u884c\u91cd\u5199\u3002<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/dolphin0520\/p\/3932921.html\">Java\u5e76\u53d1\u7f16\u7a0b\uff1a\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/congge_1993\/article\/details\/73497439\">\u7ebf\u7a0b\u6c60\u521b\u5efa<\/a><\/li>\n<li><a href=\"http:\/\/www.infoq.com\/cn\/articles\/java-threadPool\">\u804a\u804a\u5e76\u53d1\uff08\u4e09\uff09Java\u7ebf\u7a0b\u6c60\u7684\u5206\u6790\u548c\u4f7f\u7528<\/a><br \/>\n<a href=\"http:\/\/ifeve.com\/java-threadpool\/\">http:\/\/ifeve.com\/java-threadpool\/<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/whthomas\/p\/java-executors.html\">Java\u7ebf\u7a0b\u6c60\u89e3\u6790<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/jmcui\/p\/8017473.html\">\u591a\u7ebf\u7a0b\u7f16\u7a0b\u5b66\u4e60\u4e94(\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa)<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728\u4e4b\u524d\u8bb0\u5f55\u7684\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7ebf\u7a0b\u7684\u65f6\u5019\u5c31\u53bb\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u6837\u5b9e\u73b0\u8d77\u6765\u975e\u5e38\u7b80\u4fbf\uff0c\u4f46\u662f\u5c31\u4f1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[61,1149,1150],"class_list":["post-3970","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-java","tag-threadpoolexecutor","tag-1150"],"views":4794,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3970"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3970\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3970\/revisions\/3971"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}