{"id":2207,"date":"2021-10-25T04:51:24","date_gmt":"2021-10-25T04:51:24","guid":{"rendered":"https:\/\/blog.embold.io\/?p=2207"},"modified":"2021-11-08T13:07:07","modified_gmt":"2021-11-08T13:07:07","slug":"process-executors-in-java","status":"publish","type":"post","link":"https:\/\/blog.embold.io\/fr\/process-executors-in-java\/","title":{"rendered":"Process executors in Java"},"content":{"rendered":"\n<h1>Introduction<\/h1>\n\n\n\n<p> In the Java programming language, it is not a real challenge to work with a limited number of threads, plus it is fairly easy to manage the execution of one or two threads simultaneously. However, it can become increasingly difficult to handle a substantial number of threads. That is where the&nbsp;<strong>executor<\/strong>&nbsp;can help with the creation and management of simultaneous threads. <\/p>\n\n\n\n<p>The&nbsp;<strong>Java ExecutorService<\/strong>&nbsp;interface provides management for running many tasks concurrently. <\/p>\n\n\n\n<h1>What is an executor?<\/h1>\n\n\n\n<p>An executor can use runnable classes without the need for creating a new thread through the reuse of an existing thread. Handling multiple threads is a feature of the Executor Framework. <\/p>\n\n\n\n<p>There are 3 components included:<\/p>\n\n\n\n<ol><li>Executor Framework <\/li><li>Thread Pools<\/li><li>Fork\/Join <\/li><\/ol>\n\n\n\n<h2>Embold usage<\/h2>\n\n\n\n<p>Why does Embold use Executor? <\/p>\n\n\n\n<ul><li> Embold processes multiple files parallelly in a few seconds. <\/li><li> Multiple processes running simultaneously are handled by an executor. For example, the scanning of multiple languages happens in parallel.  <\/li><li> Data accuracy and inter-thread process communication are important. These processes are interdependent.\u00a0 For example, when parsing is complete the calculation of rating is started automatically. <\/li><\/ul>\n\n\n\n<p>Embold uses\u00a0<strong>FixedThreadPool<\/strong>\u00a0which is normally configurable using the config file. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"530\" height=\"272\" src=\"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/image.png\" alt=\"\" class=\"wp-image-2209\" \/><\/figure>\n\n\n\n<p><strong>Possible Thread Leak In Executor Service<\/strong>&nbsp;is a unique Java custom code check that helps detect unwanted shutting down of the Executor service. <\/p>\n\n\n<p><strong>Rule Name:<\/strong> Possible Thread Leak In Executor Service<\/p>\n<p><strong>Description: <\/strong>Thread leak can be possible if ExecutorService is not getting shut down. There is a queue of tasks between client threads and the thread pool. When the normal thread finishes the &#8220;run&#8221; method (Runnable or Callable), it will be passed to the garbage collector for collection. But with ExecuterService, threads will be simply put on hold and they will not be selected for garbage collection. Therefore, the shutdown is needed for ExecutorService.<strong><br><\/strong><\/p>\n<p><strong>KPI<\/strong>: Resource Utilization<\/p>\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Non-Compliant code\n\n\t    class Demo {\n\n\t\t\/\/ ExecutorService is not closed anywhere\n\t\tpublic void process1() {\n\t\t    ExecutorService executorService = Executors.newSingleThreadExecutor();\n\t\t    executorService.execute(new Runnable() {\n\t\t        @Override\n\t\t        public void run() {\n\t\t            try {\n\t\t                doTask();\n\t\t            } catch (Exception e) {\n\t\t                logger.error(\"indexing failed\", e);\n\t\t            }\n\t\t        }\n\t\t    });\n\t\t}\n\t    }\n\n \nCompliant code:\n\n\t    class Demo {\n\n\t\tpublic void process2() {\n\t\t    ExecutorService executorService = Executors.newSingleThreadExecutor();\n\t\t    executorService.execute(new Runnable() {\n\t\t        @Override\n\t\t        public void run() {\n\t\t            try {\n\t\t                doTask();\n\t\t            } catch (Exception e) {\n\t\t                logger.error(\"indexing failed\", e);\n\t\t            }\n\t\t        }\n\t\t    });\n\t\t    executorService.shutdown();\n\t\t}\n\t    }\n<\/code><\/pre>\n\n\n\n<h2>Summary<\/h2>\n\n\n\n<p>In general, the&nbsp;<strong>ExecutorService<\/strong>&nbsp;will not automatically shut down while there are any tasks in the queue. One should confirm the count of threads in a pool to avoid the unnecessary overhead for this&nbsp;<strong>ExecutorService<\/strong>. It can help to run the application tasks efficiently.<\/p>\n\n\n\n<p>Developers can leverage using this&nbsp;<strong>Execution Framework<\/strong>&nbsp;to reduce the complexities involved in executing multiple threads.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the Java programming language, it is not a real challenge to work with a limited number of threads, plus it is fairly easy to manage the execution of&hellip;<\/p>\n","protected":false},"author":10,"featured_media":2218,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[211,213,108,212,210,214],"better_featured_image":{"id":2218,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":3000,"height":2000,"file":"2021\/10\/embold-executors-java-blog-img.png","sizes":{"thumbnail":{"file":"embold-executors-java-blog-img-150x150.png","width":150,"height":150,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-150x150.png"},"medium_large":{"file":"embold-executors-java-blog-img-768x512.png","width":768,"height":512,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-768x512.png"},"penci-full-thumb":{"file":"embold-executors-java-blog-img-1170x780.png","width":1170,"height":780,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-1170x780.png"},"penci-slider-thumb":{"file":"embold-executors-java-blog-img-1170x663.png","width":1170,"height":663,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-1170x663.png"},"penci-magazine-slider":{"file":"embold-executors-java-blog-img-780x516.png","width":780,"height":516,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-780x516.png"},"penci-slider-full-thumb":{"file":"embold-executors-java-blog-img-1920x800.png","width":1920,"height":800,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-1920x800.png"},"penci-single-full":{"file":"embold-executors-java-blog-img-1920x1280.png","width":1920,"height":1280,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-1920x1280.png"},"penci-thumb":{"file":"embold-executors-java-blog-img-585x390.png","width":585,"height":390,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-585x390.png"},"penci-masonry-thumb":{"file":"embold-executors-java-blog-img-585x390.png","width":585,"height":390,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-585x390.png"},"penci-thumb-square":{"file":"embold-executors-java-blog-img-585x585.png","width":585,"height":585,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-585x585.png"},"penci-thumb-vertical":{"file":"embold-executors-java-blog-img-480x650.png","width":480,"height":650,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-480x650.png"},"penci-thumb-small":{"file":"embold-executors-java-blog-img-263x175.png","width":263,"height":175,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-263x175.png"},"jr_insta_square":{"file":"embold-executors-java-blog-img-640x640.png","width":640,"height":640,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img-640x640.png"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":2207,"source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/10\/embold-executors-java-blog-img.png"},"translation":{"provider":"WPGlobus","version":"2.10.8","language":"fr","enabled_languages":["en","es","de","fr","ru"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false},"fr":{"title":false,"content":false,"excerpt":false},"ru":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2207"}],"collection":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/comments?post=2207"}],"version-history":[{"count":18,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2207\/revisions"}],"predecessor-version":[{"id":2243,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2207\/revisions\/2243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/media\/2218"}],"wp:attachment":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/media?parent=2207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/categories?post=2207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/tags?post=2207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}