{"id":2630,"date":"2017-05-20T19:09:02","date_gmt":"2017-05-20T13:39:02","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=2630"},"modified":"2024-02-21T10:11:35","modified_gmt":"2024-02-21T04:41:35","slug":"java-newfixedthreadpool-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-newfixedthreadpool-example\/","title":{"rendered":"Java newFixedThreadPool Example"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><ul><li><ul><li><a href=\"#Executors_newFixedThreadPool_factory_method\">Executor\u2019s newFixedThreadPool factory method :<\/a><\/li><\/ul><\/li><li><a href=\"#Syntax\">Syntax:<\/a><\/li><\/ul><\/li><li><a href=\"#Java_newFixedThreadPool_example\">Java newFixedThreadPool example:<\/a><\/li><\/ul><\/div>\n<p>In this tutorial, we will learn about Executor\u2019s newFixedThreadPool factory method.<br \/>\nIn the last tutorial, I have shared an introduction to <a href=\"http:\/\/www.java2blog.com\/2016\/12\/java-threadpoolexecutor-example.html\">ThreadPoolExecutor<\/a>. If you are not aware of concepts of <code>ThreadPoolExecutor<\/code>, you should go through that first.<\/p>\n<h4><span id=\"Executors_newFixedThreadPool_factory_method\">Executor\u2019s newFixedThreadPool factory method :<\/span><\/h4>\n<p>This method returns <code>ThreadPoolExecutor<\/code> whose maximum size(let\u2019s say n threads) is fixed.If all n threads are busy performing the task and additional tasks are submitted, then they will have to be in the queue until a thread is available.<\/p>\n<h3><span id=\"Syntax\">Syntax:<\/span><\/h3>\n<pre name=\"code\">ExecutorService executorService=Executors.newFixedThreadPool(noOfThreads);\n<\/pre>\n<h2><span id=\"Java_newFixedThreadPool_example\">Java newFixedThreadPool example:<\/span><\/h2>\n<p>Let&#8217;s create a very simple example.<\/p>\n<p>Step 1: Create a Runnable task named &quot;LoopTask.java&quot;.<\/p>\n<pre name=\"code\">package org.arpit.java2blog;\n\npublic class LoopTask implements Runnable {\n\n    private String loopTaskName;\n\n    public LoopTask(String loopTaskName) {\n        super();\n        this.loopTaskName = loopTaskName;\n    }\n\n    @Override\n    public void run() {\n        System.out.println(\"Starting \"+loopTaskName);\n        for (int i = 1; i &lt;= 10; i++) {\n            System.out.println(\"Executing \"+loopTaskName+\" with \"+Thread.currentThread().getName()+\"====\"+i);\n        }\n        System.out.println(\"Ending \"+loopTaskName);\n    }\n}\n<\/pre>\n<p>Step 2: Create a class named <code>FixedThreadPoolMain.java<\/code>. This will be our main class.<\/p>\n<pre name=\"code\">package org.arpit.java2blog;\n\nimport java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\npublic class FixedThreadPoolMain {\n\n    public static void main(String args[])\n    {\n        ExecutorService es=Executors.newFixedThreadPool(3);\n\n        for (int i = 1; i &lt;= 6; i++) {\n            LoopTask loopTask=new LoopTask(\"LoopTask \"+i);\n            es.submit(loopTask);\n        }\n        es.shutdown();\n    }\n}\n<\/pre>\n<p>Let&#8217;s run above program to check the output:<\/p>\n<div class=\"content-box-purple\">Starting LoopTask 1<br \/>\nStarting LoopTask 3<br \/>\nStarting LoopTask 2<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====1<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====1<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====2<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====1<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====3<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====2<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====4<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====2<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====5<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====6<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====3<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====7<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====3<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====8<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====4<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====9<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====4<br \/>\nExecuting LoopTask 3 with pool-1-thread-3====10<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====5<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====6<br \/>\nEnding LoopTask 3<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====5<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====6<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====7<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====8<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====9<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====7<br \/>\nExecuting LoopTask 2 with pool-1-thread-2====10<br \/>\nStarting LoopTask 4<br \/>\nEnding LoopTask 2<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====8<br \/>\nStarting LoopTask 5<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====1<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====1<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====9<br \/>\nExecuting LoopTask 1 with pool-1-thread-1====10<br \/>\nEnding LoopTask 1<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====2<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====2<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====3<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====3<br \/>\nStarting LoopTask 6<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====4<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====4<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====5<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====6<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====1<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====2<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====3<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====4<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====7<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====5<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====6<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====7<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====8<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====8<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====5<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====9<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====9<br \/>\nExecuting LoopTask 4 with pool-1-thread-3====10<br \/>\nExecuting LoopTask 5 with pool-1-thread-2====10<br \/>\nEnding LoopTask 5<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====6<br \/>\nEnding LoopTask 4<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====7<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====8<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====9<br \/>\nExecuting LoopTask 6 with pool-1-thread-1====10<br \/>\nEnding LoopTask 6<\/div>\n<p>We have used new <code>newFixedThreadPool<\/code>, so when we have submitted <code>6 tasks<\/code>, <code>3 new threads<\/code> will be created and will execute <code>3 tasks<\/code>. <code>Other 3 tasks<\/code> will wait in wait <code>queue<\/code>. As soon as any task will be completed by thread, another task will be picked by this thread and will execute it.<br \/>\nThat&#8217;s all about Java newFixedThreadPool example.<\/p>\n<hr\/>\n<p><strong>You may also like:<\/strong><br \/>\n<div class=\"pt-cv-wrapper\">Error: View <strong>12d15fexqk<\/strong> may not exist<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsExecutor\u2019s newFixedThreadPool factory method :Syntax:Java newFixedThreadPool example: In this tutorial, we will learn about Executor\u2019s newFixedThreadPool factory method. In the last tutorial, I have shared an introduction to ThreadPoolExecutor. If you are not aware of concepts of ThreadPoolExecutor, you should go through that first. Executor\u2019s newFixedThreadPool factory method : This method returns ThreadPoolExecutor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[212,144,9],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2630"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=2630"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2630\/revisions"}],"predecessor-version":[{"id":26240,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/2630\/revisions\/26240"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=2630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=2630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=2630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}