{"id":115,"date":"2016-02-16T08:33:53","date_gmt":"2016-02-16T08:33:53","guid":{"rendered":"https:\/\/boochlin.com\/?p=115"},"modified":"2016-02-16T08:34:49","modified_gmt":"2016-02-16T08:34:49","slug":"naming-threads-and-thread-pools-of-executorservice","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=115","title":{"rendered":"Naming threads and thread pools of ExecutorService"},"content":{"rendered":"<p>\u5982\u4f55\u66ff ExecutorService \u6240\u7ba1\u7406\u7684 thread \u8a2d\u5b9a\u4e00\u500b\u81ea\u5df1\u770b\u5f97\u723d\u7684\u540d\u5b50<\/p>\n<p>\u5176\u5be6\u9019\u662f\u4e00\u4ef6\u5f88\u91cd\u8981\u7684\u4e8b\u60c5\uff0c\u6839\u64da clean code \u88e1\u9762\u6240\u63d0\u5230\u7684\uff0c\u4e00\u500b\u597d\u7684\u540d\u5b50\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\u4e26 debug\uff0c\u901a\u5e38\u914d\u5408 thread \u89c0\u6e2c\u5de5\u5177\u5c31\u53ef\u4ee5\u627e\u51fa\u5230\u5e95\u6709\u591a\u5c11\u83ab\u540d\u5176\u5999\u7684 thread \u5728\u6d88\u8017\u4f60\u7684\u8cc7\u6e90\u3002<\/p>\n<p>\u4ee5 android ddms \u70ba\u4f8b<br \/>\n<a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-16-\u4e0b\u53482.52.27.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-116\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/02\/\u87a2\u5e55\u5feb\u7167-2016-02-16-\u4e0b\u53482.52.27.png\" alt=\"ddms_thread\" width=\"632\" height=\"665\" \/><\/a><\/p>\n<p>\u770b\u5f97\u9019\u908a\u4f60\u53ef\u80fd\u6703\u89ba\u5f97\u9019\u7bc7\u5f88\u5ee2\uff0c\u4e0d\u904e\u662f\u66ff thread \u53d6\u500b\u540d\u5b50\uff0c\u539f\u751f\u5c31\u6709\u5566<\/p>\n<pre>Thread thread = new Thread(Runnable);\r\nthread.setName(\"Load bitmap task\");\r\n<\/pre>\n<p>\u4f46\u662f\uff5e\uff5e\uff5e<\/p>\n<p><!--more--><\/p>\n<p>\u5982\u679c\u662f\u4f7f\u7528 ExecutorService \u505a\u70ba thread \u6392\u7a0b\u5de5\u5177\u7684\u8a71\uff0c\u5c31\u6703\u767c\u73fe\u5c45\u7136\u6c92\u6709 setName() \u53ef\u4ee5\u7528\uff0c\u9019\u4e0d\u662f\u7576\u7136\u7684\u55ce\uff01\u5c31\u7b97\u53ef\u4ee5\u53d6\u540d\u5b50\u4e5f\u6703\u8b8a\u6210\u53d6 ExecutorService \u7684\u540d\u5b50\u5427\u3002\u800c\u9810\u8a2d\u5728 thread pool\u88e1\u7684 thread \u4e00\u5f8b\u662f\u9019\u683c\u5f0f\uff08\u5982\u4e0a\u5716\uff09<\/p>\n<pre>String namePrefix = \"pool-\" + poolNumber.getAndIncrement() + \"-thread-\" + thredNumber.getAndIncrement();\r\n<\/pre>\n<p>\u57fa\u672c\u4e0a\u9019\u547d\u540d\u6709\u8ddf\u6c92\u6709\u5dee\u4e0d\u591a\uff0c\u53ea\u4e0d\u904e\u591a\u77e5\u9053\u9019\u662f\u7531 pool \u5efa\u51fa\u4f86\u7684\uff0c\u70ba\u4e86\u89e3\u6c7a\u9019\u500b\u554f\u984c\uff0c\u5148\u63d0\u4f9b\u6700\u7c21\u55ae\u7684\u571f\u7832\u6cd5-\u76f4\u63a5\u5728runnable \u628a thread \u6293\u51fa\u4f86\u547d\u540d<\/p>\n<pre>new Runnable () {\r\n    public void run() {\r\n        Thread.currentThread().setName(\"Your name\");\r\n        doStuff();\r\n    }\r\n}\r\n<\/pre>\n<p>\u4f46\u662f\u9019\u771f\u7684\u592a\u571f\u7832\u4e86\uff0c\u5be6\u5728\u6c92\u6709\u7f8e\u611f\uff0c\u4e5f\u4ee3\u8868\u8005\u6709\u8dd1\u5230\u9019\u500b runnable \u7684 thread \u624d\u6709\u6a5f\u6703\u88ab\u547d\u540d\uff0c\u800c\u4e14\u5982\u679c\u4efb\u52d9\u4e0d\u540c\u7684\u8a71\uff0c\u4e00\u500bthread \u66f4\u6709\u53ef\u80fd\u6703\u5e38\u5e38\u8b8a\u63db\u540d\u7a31\u3002<\/p>\n<p>\u6700\u597d\u7684\u65b9\u6cd5\u662f\u5229\u7528\u5de5\u5ee0\u6a21\u5f0f\uff0c\u5728\u5275\u5efa thread pool \u6642\u628a\u5275\u9020 thread \u7684\u5de5\u5ee0\u65b9\u6cd5\u4e5f\u653e\u9032\u53bb<\/p>\n<pre>ExecutorService mPool;\r\npublic ExecutorService getExecutorPool() {\r\n        if (mPool == null) {\r\n            mPool = Executors.newFixedThreadPool(POOL_THREAD_NUM, new NameThreadFactory(TAG));\r\n        }\r\n        return mPool;\r\n    }\r\n}\r\n\/\/ \u53e6\u5916\u5beb\u7684 thread factory, \u7c21\u55ae\u53ea\u591a\u52a0\u547d\u540d\u7684\u5de5\u4f5c\r\npublic class NameThreadFactory implements ThreadFactory {\r\n    private static final AtomicInteger poolNumber = new AtomicInteger(1);\r\n    private final ThreadGroup group;\r\n    private final AtomicInteger threadNumber = new AtomicInteger(1);\r\n    private final String namePrefix;\r\n\r\n    public NameThreadFactory(String namePrefix) {\r\n        SecurityManager s = System.getSecurityManager();\r\n        group = (s != null) ? s.getThreadGroup() :\r\n                Thread.currentThread().getThreadGroup();\r\n        this.namePrefix = namePrefix + \"-\" +\r\n                poolNumber.getAndIncrement() +\r\n                \"-thread-\";\r\n    }\r\n\r\n    public Thread newThread(Runnable r) {\r\n        Thread t = new Thread(group, r,\r\n                namePrefix + threadNumber.getAndIncrement(),\r\n                0);\r\n        if (t.isDaemon())\r\n            t.setDaemon(false);\r\n        if (t.getPriority() != Thread.NORM_PRIORITY)\r\n            t.setPriority(Thread.NORM_PRIORITY);\r\n        return t;\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>\u6216\u8005\u66f4\u7c21\u55ae\u7684\u4f7f\u7528\u5df2\u6709\u7684\u8eca\u8f2a guava , <a href=\"http:\/\/search.maven.org\/#artifactdetails%7Ccom.google.guava%7Cguava%7C18.0%7Cbundle\" target=\"_blank\">google common lib<\/a><br \/>\n\u5148\u5fc5\u9808\u5728 gradle \u52a0\u4e0a<\/p>\n<pre>compile 'com.google.guava:guava:18.0'\r\n<\/pre>\n<p>\u63a5\u8005\uff5e<\/p>\n<pre>ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()\r\n                                       .setNameFormat(\"workerthread-%d\").build()\r\n<\/pre>\n<p>\u4e5f\u6216\u8005 apache \u7684\u9019\u4e00\u5957 <a href=\"http:\/\/mvnrepository.com\/artifact\/org.apache.commons\/commons-lang3\/3.0\" target=\"_blank\">lib<\/a> \uff0c\u8acb\u5148\u6293jar\u4e0b\u4f86<\/p>\n<pre>BasicThreadFactory factory = new BasicThreadFactory.Builder()\r\n     .namingPattern(\"workerthread-%d\")\r\n     .daemon(true)\r\n     .build();\r\n<\/pre>\n<p>\u611f\u8b1d\u6536\u770b<\/p>\n<p>reference:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/6113746\/naming-threads-and-thread-pools-of-executorservice\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/6113746\/naming-threads-and-thread-pools-of-executorservice<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u66ff ExecutorService \u6240\u7ba1\u7406\u7684 thread \u8a2d\u5b9a\u4e00\u500b\u81ea\u5df1\u770b\u5f97\u723d\u7684\u540d\u5b50 \u5176\u5be6\u9019\u662f\u4e00\u4ef6\u5f88\u91cd\u8981\u7684\u4e8b\u60c5\uff0c\u6839\u64da clean code \u88e1\u9762\u6240\u63d0\u5230\u7684\uff0c\u4e00\u500b\u597d\u7684\u540d\u5b50\u53ef\u4ee5\u5e6b\u52a9\u4f60\u7406\u89e3\u7a0b\u5f0f\u78bc\u4e26 deb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,5,36,52],"tags":[6,48,15,37],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/115"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":0,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}