{"id":10,"date":"2020-07-17T16:00:00","date_gmt":"2020-07-17T16:00:00","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/07\/17\/multi-threading-in-java-using-executors\/"},"modified":"2020-10-06T07:24:43","modified_gmt":"2020-10-06T07:24:43","slug":"multi-threading-in-java-using-executors","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/multi-threading-in-java-using-executors\/","title":{"rendered":"Multi-threading in JAVA using “Executors”"},"content":{"rendered":"\n

Multi-threading is not always super easy to implement. There is Runnable, Thread, and all sorts of frameworks\/packages but we love the executors class to run our code in threads.<\/p>\n\n\n\n

Here is the code:<\/p>\n\n\n\n

A time consuming function such as an API call, download files etc.<\/p>\n\n\n\n

public Double someTimeConsumingFunction() throws InterruptedException {  log.info(\"Thread started\");\n  Thread.sleep(1000);\n  log.info(\"Thread finished\");\n  return Math.random();\n}<\/code><\/pre>\n\n\n\n

Our multi-threaded function<\/p>\n\n\n\n

public void threadsExample() {\n  ExecutorService taskExecutor = Executors.newFixedThreadPool(25);\n  List<Future<Double>> resultList = new ArrayList<>();\n  for (int i = 0; i < 10; i++) {\n    Future<Double> result = taskExecutor\n        .submit(new Callable<Double>() {\n          public Double call() {\n            try {\n              return someTimeConsumingFunction();\n            } catch (Exception e) {\n              log.error(e);\n            }\n            return null;\n          }\n        });\n    resultList.add(result);\n  }\n  taskExecutor.shutdown();\n  try {\n    log.info(\"Waiting for all threads to finish and terminate\");\n    taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);\n  } catch (InterruptedException e) {\n    Sentry.capture(e);\n  }\n  log.info(\"Finished execution\");\n}<\/code><\/pre>\n\n\n\n

As simple as that. I have actually bookmarked this page as I keep using this code again and again. Let me know in the comments if anything is unclear.<\/p>\n","protected":false},"excerpt":{"rendered":"

Multi-threading is not always super easy to implement. There is Runnable, Thread and all sort of frameworks\/packages but we love the executors class to run our code in threads.<\/p>\n","protected":false},"author":1,"featured_media":116,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[40,35],"tags":[8,3],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/10"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":3,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/10\/revisions\/118"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/116"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}