{"id":680,"date":"2019-08-28T16:59:37","date_gmt":"2019-08-28T04:59:37","guid":{"rendered":"https:\/\/content.ronella.xyz\/apps\/wordpress\/?p=680"},"modified":"2023-08-18T09:16:37","modified_gmt":"2023-08-17T21:16:37","slug":"threadpoolexecutor-with-arrayblockingqueue-and-custom-thread-name","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=680","title":{"rendered":"ThreadPoolExecutor with ArrayBlockingQueue and Custom Thread Name"},"content":{"rendered":"<p>Create an implementation of <strong>ThreadFactory<\/strong> to create a thread with custom name for <strong>ThreadPoolExecutor<\/strong> as follows:<\/p>\n<pre style=\"white-space: pre;\">class MyThreadFactory implements ThreadFactory {\n    private AtomicLong threadCounter;\n\n    private MyThreadFactory() {\n        threadCounter = new AtomicLong();\n    }\n\n    @Override\n    public Thread newThread(Runnable runnable) {\n        var thread=new Thread(runnable);\n        thread.setName(String.join(\"-\",\"my-thread\",\n                String.valueOf(threadCounter.incrementAndGet())));\n        return thread;\n    }\n}\n<\/pre>\n<p>The preceding class will generate a thread with the name starting with <strong>my-thread<\/strong>. Use the instance of this class in constructing the <strong>ThreadPoolExecutor<\/strong> as follows:<\/p>\n<pre style=\"white-space: pre;\">var executor = new ThreadPoolExecutor(2, 4, 60L, TimeUnit.SECONDS,\n        new ArrayBlockingQueue&lt;&gt;(100), new MyThreadFactory(), \n        new ThreadPoolExecutor.CallerRunsPolicy());\n<\/pre>\n<p>The preceding declaration creates an instance of ThreadPoolExecutor with 2 core threads, 4 maximum threads, 60 seconds keep alive and supports 100 items in the queue. The queue size is defined by the instance of <strong>ArrayBlockingQueue<\/strong> class.<\/p>\n<p>Start all the core threads as follows:<\/p>\n<pre style=\"white-space: pre;\">executor.prestartAllCoreThreads();\n<\/pre>\n<p>Using a profiling tool we can search for all the threads whose names starts with <strong>my-thread<\/strong> like the following screenshot:<\/p>\n<p><a href=\"https:\/\/nasnz.ronella.xyz\/wordpress\/wp-content\/uploads\/2019\/08\/custom-thread-name-with-thread-factory.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-821\" src=\"https:\/\/nasnz.ronella.xyz\/wordpress\/wp-content\/uploads\/2019\/08\/custom-thread-name-with-thread-factory-300x52.png\" alt=\"\" width=\"300\" height=\"52\" srcset=\"https:\/\/nasnz.ronella.xyz\/wordpress\/wp-content\/uploads\/2019\/08\/custom-thread-name-with-thread-factory-300x52.png 300w, https:\/\/nasnz.ronella.xyz\/wordpress\/wp-content\/uploads\/2019\/08\/custom-thread-name-with-thread-factory.png 727w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<blockquote><p>Don't forget to call any <b>shutdown\/terminate<\/b> methods when done using the executor.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Create an implementation of ThreadFactory to create a thread with custom name for ThreadPoolExecutor as follows: class MyThreadFactory implements ThreadFactory { private AtomicLong threadCounter; private MyThreadFactory() { threadCounter = new AtomicLong(); } @Override public Thread newThread(Runnable runnable) { var thread=new Thread(runnable); thread.setName(String.join(&#8220;-&#8220;,&#8221;my-thread&#8221;, String.valueOf(threadCounter.incrementAndGet()))); return thread; } } The preceding class will generate a thread with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35,47,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/680"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=680"}],"version-history":[{"count":12,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions"}],"predecessor-version":[{"id":1610,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/680\/revisions\/1610"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}