{"id":1174,"date":"2021-07-16T13:39:32","date_gmt":"2021-07-16T05:39:32","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=1174"},"modified":"2023-08-07T20:57:23","modified_gmt":"2023-08-07T12:57:23","slug":"%e9%87%8d%e5%86%99%e7%ba%bf%e7%a8%8b%e6%b1%a0threadfactory%e6%8e%a5%e5%8f%a3%e5%ae%9e%e7%8e%b0%e5%af%b9%e7%ba%bf%e7%a8%8b%e5%bc%82%e5%b8%b8%e7%9a%84%e6%8d%95%e8%8e%b7","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2021\/07\/16\/%e9%87%8d%e5%86%99%e7%ba%bf%e7%a8%8b%e6%b1%a0threadfactory%e6%8e%a5%e5%8f%a3%e5%ae%9e%e7%8e%b0%e5%af%b9%e7%ba%bf%e7%a8%8b%e5%bc%82%e5%b8%b8%e7%9a%84%e6%8d%95%e8%8e%b7\/","title":{"rendered":"\u91cd\u5199\u7ebf\u7a0b\u6c60ThreadFactory\u63a5\u53e3\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u5f02\u5e38\u7684\u6355\u83b7"},"content":{"rendered":"<p>\u91cd\u5199\u7ebf\u7a0b\u6c60ThreadFactory\u63a5\u53e3\u5b9e\u73b0\u5bf9\u7ebf\u7a0b\u5f02\u5e38\u7684\u6355\u83b7<br \/>\n\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u4f1a\u7528\u5230\u7ebf\u7a0b\u6c60\uff0c\u4f46\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5\u90fd\u6bd4\u8f83\u7b80\u5355\uff0c\u4f7f\u7528Executors\u6765\u521b\u5efa\u76f8\u5e94\u529f\u80fd\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u8fd9\u4e9b\u3002<\/p>\n<pre><code>1\u3001 Executors.newFixedThreadPool(int nThreads);\u521b\u5efa\u56fa\u5b9a\u5927\u5c0f(nThreads,\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc7int\u7684\u6700\u5927\u503c)\u7684\u7ebf\u7a0b\u6c60\n2\u3001Executors.newSingleThreadExecutor()\uff1a\u521b\u5efa\u5927\u5c0f\u4e3a1\u7684\u56fa\u5b9a\u7ebf\u7a0b\u6c60\u3002\n3\u3001Executors.newCachedThreadPool()\uff1b\u521b\u5efacorePoolSize\u4e3a0\uff0c\u6700\u5927\u7ebf\u7a0b\u6570\u4e3a\u6574\u578b\u7684\u6700\u5927\u6570\uff0c\u7ebf\u7a0bkeepAliveTime\u4e3a1\u5206\u949f\uff0c\u7f13\u5b58\u4efb\u52a1\u7684\u961f\u5217\u4e3aSynchronousQueue\u7684\u7ebf\u7a0b\u6c60\u3002\n4\u3001Executors.newScheduledThreadPool(int corePoolSize):\u521b\u5efacorePoolSize\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60\u3002\n<\/code><\/pre>\n<p>\u521b\u5efa\u8fc7\u7a0b\u4e2d\u6700\u5173\u6ce8\u7684\u53c2\u6570\u662fcorePoolSize\uff0cmaximumPoolSize\u5df2\u7528\u6765\u914d\u7f6e\u6838\u5fc3\u7ebf\u7a0b\u6570\u548c\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u800cKeepAliveTime\uff0cBlockingQueue\uff0cthreadFactory\u5219\u5e38\u88ab\u5ffd\u7565\uff0c\u6216\u8005\u8bf4\u4e0d\u8bbe\u7f6e\u4e5f\u65e0\u59a8\u6b63\u5e38\u4f7f\u7528\uff0c\u4f46\u5728\u4e00\u4e9b\u7279\u522b\u60c5\u51b5\u4e0b\u8fd8\u662f\u9700\u8981\u7279\u522b\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\u6765\u542f\u52a8\u7ebf\u7a0b\u6c60\uff0c\u8fd9\u6b21\u4e3b\u8981\u6765\u8bf4\u660eThreadFactory\u7684\u7528\u5904\u548c\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u3002<br \/>\nThreadFactory\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u63a5\u53e3\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u5b9e\u73b0\u7684\uff0c\u5176\u5185\u90e8\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<pre><code>public interface ThreadFactory {\n    Thread newThread(Runnable r);\n}\n<\/code><\/pre>\n<p>Executors\u4e2d\u6709\u9ed8\u8ba4\u5b9e\u73b0<\/p>\n<pre><code class=\"java\">static class DefaultThreadFactory implements ThreadFactory {\n    private static final AtomicInteger poolNumber = new AtomicInteger(1);\n    private final ThreadGroup group;\n    private final AtomicInteger threadNumber = new AtomicInteger(1);\n    private final String namePrefix;\n\n    DefaultThreadFactory() {\n        SecurityManager s = System.getSecurityManager();\n        group = (s != null) ? s.getThreadGroup() :\n                              Thread.currentThread().getThreadGroup();\n        namePrefix = \"pool-\" +\n                      poolNumber.getAndIncrement() +\n                     \"-thread-\";\n    }\n\n    public Thread newThread(Runnable r) {\n        Thread t = new Thread(group, r,\n                              namePrefix + threadNumber.getAndIncrement(),\n                              0);\n        if (t.isDaemon())\n            t.setDaemon(false);\n        if (t.getPriority() != Thread.NORM_PRIORITY)\n            t.setPriority(Thread.NORM_PRIORITY);\n        return t;\n    }\n}\n\n<\/code><\/pre>\n<p>newThread\u65b9\u6cd5\u4e2d\u5bf9\u7ebf\u7a0b\u7684\u547d\u540d\u548c\u5b88\u62a4\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u4f18\u5148\u7ea7\u8fdb\u884c\u914d\u7f6e\uff0c\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u52a0\u5165\u66f4\u591a\u81ea\u5b9a\u4e49\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u5904\u7406\u7ebf\u7a0b\u5f02\u5e38\u7684\u65b9\u6cd5\u8fd8\u6709\u5728\u7ebf\u7a0b\u542f\u52a8\u65f6\u6253\u5370\u65e5\u5fd7\u7b49\u7b49\uff0c\u8ba9\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\u548c\u542f\u52a8\u53d8\u5f97\u7075\u6d3b\u53ef\u63a7\u3002\u5f53\u5f02\u5e38\u53d1\u751f\u65f6\uff0c\u53ca\u65f6\u6253\u5370\u5f02\u5e38\u60c5\u51b5\u7136\u540e\u901a\u77e5\u5176\u4ed6\u670d\u52a1\u6216\u8005\u91cd\u542f\u7ebf\u7a0b\u8fdb\u884c\u91cd\u8bd5\u7b49\uff0c\u4f46UncaughtExceptionHandler\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8eexecute()\u65b9\u6cd5\u5f80\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1\u7684\u60c5\u51b5\uff0c\u5982\u679c\u662f\u9002\u7528submit\u65b9\u6cd5\u5219\u7531\u56de\u8c03get\u65b9\u6cd5\u65f6\u6765\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"java\">public class MyThreadFactory implements ThreadFactory {\n    static final Logger LOGGER= Logger.getAnonymousLogger();\n    private final UncaughtExceptionHandler ueh;\n    private final AtomicInteger threadNum  = new AtomicInteger(1);\n    private final String namePrefix;\n\n    public MyThreadFactory() {\n        this(new LoggingUncaughtExceptionHandler(),\u201dthreadFactory\u201d);\n    }\n\n    public MyThreadFactory(UncaughtExceptionHandler ueh, String namePrefix) {\n        this.ueh = ueh;\n        this.namePrefix = namePrefix;\n    }\n\n    protected Thread doMakeThread(final Runnable r){\n        return new Thread(r){\n            @Override\n            public String toString() {\n                ThreadGroup group = getThreadGroup();\n                String groupName = null == group?\u201d\u201d:group.getName();\n                return getClass().getSimpleName() + \u201c[\u201c+getName()+\u201d,\u201d+getId()+\u201d,\u201d+groupName+\u201d]@\u201c+hashCode();\n            }\n        };\n    }\n\n    @Override\n    public Thread newThread(Runnable r) {\n        Thread t = doMakeThread(r);\n        t.setUncaughtExceptionHandler(ueh);\n        t.setName(namePrefix + \u201c-\u201c +threadNum.getAndIncrement());\n        if (t.isDaemon()){\n            t.setDaemon(false);\n        }\n        if (t.getPriority() != Thread.NORM_PRIORITY){\n            t.setPriority(Thread.NORM_PRIORITY);\n        }\n        if (LOGGER.isLoggable(Level.FINE)){\n            LOGGER.fine(\u201cnew thread created\u201d+t);\n        }\n        return t;\n    }\n    static class LoggingUncaughtExceptionHandler implements UncaughtExceptionHandler{\n\n        @Override\n        public void uncaughtException(Thread t, Throwable e) {\n            System.out.println(\u201cerror\u201d);\n            LOGGER.log(Level.SEVERE,t+\u201dterminated:\u201d,e);\n        }\n    }\n\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u4f1a\u7528\u5230\u7ebf\u7a0b\u6c60\uff0c\u4f46\u521b\u5efa\u7ebf\u7a0b\u6c60\u7684\u65b9\u6cd5\u90fd\u6bd4\u8f83\u7b80\u5355\uff0c\u4f7f\u7528Executors\u6765\u521b\u5efa\u76f8\u5e94\u529f\u80fd\u7684\u7ebf\u7a0b\u6c60\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u8fd9\u4e9b\u3002<\/p>\n<p>&#8220;`<br \/>\n1\u3001 Executors.newFixedThreadPool(int nThreads);\u521b\u5efa\u56fa\u5b9a\u5927\u5c0f(nThreads,\u5927\u5c0f\u4e0d\u80fd\u8d85\u8fc7int\u7684\u6700\u5927\u503c)\u7684\u7ebf\u7a0b\u6c60<br \/>\n2\u3001Executors.newSingleThreadExecutor()\uff1a\u521b\u5efa\u5927\u5c0f\u4e3a1\u7684\u56fa\u5b9a\u7ebf\u7a0b\u6c60\u3002<br \/>\n3\u3001Executors.newCachedThreadPool()\uff1b\u521b\u5efacorePoolSize\u4e3a0\uff0c\u6700\u5927\u7ebf\u7a0b\u6570\u4e3a\u6574\u578b\u7684\u6700\u5927\u6570\uff0c\u7ebf\u7a0bkeepAliveTime\u4e3a1\u5206\u949f\uff0c\u7f13\u5b58\u4efb\u52a1\u7684\u961f\u5217\u4e3aSynchronousQueue\u7684\u7ebf\u7a0b\u6c60\u3002<br \/>\n4\u3001Executors.newScheduledThreadPool(int corePoolSize):\u521b\u5efacorePoolSize\u5927\u5c0f\u7684\u7ebf\u7a0b\u6c60\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-1174","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=1174"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1330,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1174\/revisions\/1330"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}