{"id":2571,"date":"2011-01-16T12:19:19","date_gmt":"2011-01-16T06:49:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2571"},"modified":"2016-11-30T12:47:49","modified_gmt":"2016-11-30T07:17:49","slug":"how-to-use-thread-pooling-using-groovy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-use-thread-pooling-using-groovy\/","title":{"rendered":"How to use Thread-Pooling using Groovy"},"content":{"rendered":"<p>Grails Development is challenging and most <a title=\"Grails Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\" target=\"_blank\">Groovy and Grails <\/a>projects require in-depth understanding of thread-pooling.<\/p>\n<p>As a <a title=\"groovy developer\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Groovy developer<\/a>, I really like the thread-pooling API (in java.util.concurrent package), especially when you are hitting x number of API dealing with outside network to fetch information for a single request.<\/p>\n<p>Example: A website dealing with some kind of ticket booking service and there are 4 different vendors (sources where you retrieve ticket status from).<br \/>\nClasses you need to import.<\/p>\n<p>[groovy]<br \/>\nimport java.util.concurrent.ExecutorService<br \/>\nimport java.util.concurrent.Executors<br \/>\nimport java.util.concurrent.Callable<br \/>\nimport java.util.concurrent.Executors<br \/>\nimport java.util.concurrent.TimeUnit<br \/>\nimport java.util.concurrent.Future<br \/>\n[\/groovy]<\/p>\n<p>I am discussing it here in a very simple Groovish manner.<\/p>\n<p>[groovy]<br \/>\ndef myClosure = {num -&gt; println &quot;I Love Groovy ${num}&quot;}<br \/>\ndef threadPool = Executors.newFixedThreadPool(4)<br \/>\n try {<br \/>\n  List&lt;Future&gt; futures = (1..10).collect{num-&gt;<br \/>\n    threadPool.submit({-&gt;<br \/>\n    myClosure num } as Callable);<br \/>\n  }<br \/>\n  \/\/ recommended to use following statement to ensure the execution of all tasks.<br \/>\n  futures.each{it.get()}<br \/>\n}finally {<br \/>\n  threadPool.shutdown()<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>In the above example, thread pool size is 4, that means concurrent 4 tasks would be executed in a single go. And task is the code in &#8216;myClosure&#8217;.<\/p>\n<p>Let&#8217;s say you rely on output from tasks. In such a case, \u00a0you need to do the following:<\/p>\n<p>[groovy]<br \/>\n Collection results = futures.collect{it.get()}<br \/>\n[\/groovy]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grails Development is challenging and most Groovy and Grails projects require in-depth understanding of thread-pooling. As a Groovy developer, I really like the thread-pooling API (in java.util.concurrent package), especially when you are hitting x number of API dealing with outside network to fetch information for a single request. Example: A website dealing with some kind [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":102},"categories":[7],"tags":[4840,4005,9,3954],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2571"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2571"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}