{"id":3034,"date":"2017-06-13T23:51:33","date_gmt":"2017-06-13T18:21:33","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=3034"},"modified":"2021-01-12T14:54:36","modified_gmt":"2021-01-12T09:24:36","slug":"java-executor-framework-tutorial-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-executor-framework-tutorial-example\/","title":{"rendered":"Java Executor framework tutorial with 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\"><li><a href=\"#Executor\">Executor<\/a><\/li><li><a href=\"#ExecutorService\">ExecutorService<\/a><\/li><li><a href=\"#ScheduledExecutorService\">ScheduledExecutorService<\/a><\/li><li><a href=\"#Executors\">Executors<\/a><\/li><li><a href=\"#ThreadPoolExecutor\">ThreadPoolExecutor<\/a><\/li><\/ul><\/div>\n<p>Java 5 has introduced new framework called <code>Executor Framework<\/code> for managing threads.We have already seen before how to create a <a href=\"https:\/\/java2blog.com\/java-thread-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">thread<\/a>.<br \/>\nIf you have noted, we need to create an object of thread class using <code>new Thread(runnableObject)<\/code>, so we need to create thread object for each task.Imagine a situation where you have thousands of task to be executed and if you create each thread for thousands of tasks, you may get performance overheads as creation and maintenance of each thread is also an overhead. Executor framework will solve the problem. In executor framework, you can create specified number of threads and reuse them to execute more tasks once it completes its current task.<br \/>\n<code>Executor framework<\/code> simplifies the design of creating multithreaded application and manages thread life cycles.The programmer does not have to create or manage threads themselves, that&#8217;s the biggest advantage of executor framework.<br \/>\nThere are some important classes or interfaces for executor framework.<\/p>\n<h2><span id=\"Executor\">Executor<\/span><\/h2>\n<p>This interface is used to submit new task.It has a method called &quot;execute&quot;.<\/p>\n<pre name=\"code\" class=\"\">public interface Executor {\n void execute(Runnable command);\n}<\/pre>\n<h2><span id=\"ExecutorService\">ExecutorService<\/span><\/h2>\n<p>It is sub-interface of Executor.This interface provides methods to manage lifecycle of tasks as well for executor.<br \/>\nFor example It provides method for shutting down executors.<\/p>\n<h2><span id=\"ScheduledExecutorService\">ScheduledExecutorService<\/span><\/h2>\n<p>It is sub-interface of executor service which provides methods for scheduling tasks at fixed intervals or with initial delay.<\/p>\n<h2><span id=\"Executors\">Executors<\/span><\/h2>\n<p>This class provides factory methods for creating thread pool.<br \/>\nImportant factory methods of Executors are:<\/p>\n<div style=\"text-align: justify;\"><b><a href=\"https:\/\/java2blog.com\/java-newfixedthreadpool-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">newFixedThreadPool<\/a>:<\/b> This method returns thread pool executor whose maximum size(let&#8217;s 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 thread is available.<\/div>\n<div style=\"text-align: justify;\"><b><a href=\"https:\/\/java2blog.com\/java-newcachedthreadpool-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">newCachedThreadPool<\/a>: <\/b>this method returns an unbounded thread pool. It doesn&#8217;t have maximum size but if it has less number of tasks, then it will tear down unused thread. If a thread has been unused for 1 mins(keepAliveTime), then it will tear it down.<\/div>\n<div style=\"text-align: justify;\"><b><a href=\"https:\/\/java2blog.com\/java-newsinglethreadexecutor-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">newSingleThreadedExecutor<\/a>: <\/b>this method returns an executor which is guaranteed to use the single thread.<b>\u00a0<\/b><\/div>\n<div style=\"text-align: justify;\"><b><a href=\"https:\/\/java2blog.com\/java-scheduledthreadpoolexecutor-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">newScheduledThreadPool<\/a>: <\/b>this method returns a fixed size thread pool that can schedule commands to run after a given delay, or to execute periodically.<\/div>\n<h2 style=\"text-align: justify;\"><span id=\"ThreadPoolExecutor\">ThreadPoolExecutor<\/span><\/h2>\n<div style=\"text-align: justify;\">ThreadPoolExecutor is actual implementation of ThreadPool. It extends AbstractThreadPoolExecutor which implements ExecutorService interface. You can create ThreadPoolExecutor from factory methods of Executor class. It is recommended a way to get an instance of <a href=\"https:\/\/java2blog.com\/java-threadpoolexecutor-example\/\" rel=\"noopener noreferrer\" target=\"_blank\">ThreadPoolExecutor<\/a> as seen above.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">This is index post for Executor Framework tutorial.Here is list of article for Java Executor Framework tutorial<\/div>\n<div id=\"toc_container\" class=\"no_bullets\">\n<p class=\"toc_title\">Java Executor Framework tutorial<\/p>\n<ul class=\"toc_list\">\n<li><a href=\"http:\/\/www.java2blog.com\/2016\/12\/java-threadpoolexecutor-example.html\" target=\"_blank\" rel=\"noopener noreferrer\">Java ThreadPoolExecutor example<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/01\/java-executorservice-example-using-callable-future.html\">Java ExecutorService example using Callable and Future<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/05\/java-newfixedthreadpool-example.html\">Java newFixedThreadPool Example<\/a><\/li>\n<li><a title=\"Java newCachedThreadPool Example\" href=\"http:\/\/www.java2blog.com\/2017\/01\/java-newcachedthreadpool-example.html\" target=\"_blank\" rel=\"noopener noreferrer\">Java newCachedThreadPool Example<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/06\/java-newsinglethreadexecutor-example.html\">Java newSingleThreadExecutor example<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/05\/java-scheduledthreadpoolexecutor-example.html\">Java scheduledthreadpoolexecutor\u00a0example<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/05\/java-futuretask-example.html\">Java FutureTask Example<\/a><\/li>\n<li><a href=\"http:\/\/www.java2blog.com\/2017\/06\/java-executorcompletionservice-example.html\">Java ExecutorCompletionService example<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsExecutorExecutorServiceScheduledExecutorServiceExecutorsThreadPoolExecutor Java 5 has introduced new framework called Executor Framework for managing threads.We have already seen before how to create a thread. If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have [&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":[9,212,144],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/3034"}],"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=3034"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/3034\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=3034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=3034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=3034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}