{"id":3552,"date":"2016-04-05T12:44:38","date_gmt":"2016-04-05T12:44:38","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3552"},"modified":"2016-04-05T12:44:38","modified_gmt":"2016-04-05T12:44:38","slug":"creating-thread-pool-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/creating-thread-pool-scala\/","title":{"rendered":"Creating a thread pool in Scala"},"content":{"rendered":"<p>Part of the magic of Scala is that you can easily parallelize operations with features like Futures and parallel collections. When you do this, scala creates a thread pool, so that it can allocate appropriate resources.<\/p>\n<p>If you want to control this, you can make your own thread pool by copying the following code. This is passed to the futures API as an implicit, so it will &#8220;just work&#8221; as long as you paste it before the parallelism code (any easy way to prove this is to make the thread pool very large, and watch your machine grind to a halt)<\/p>\n<pre lang=\"scala\">\nimplicit val ec = new ExecutionContext {\n  val threadPool = Executors.newFixedThreadPool(25)\n\n  def execute(runnable: Runnable) {\n    threadPool.submit(runnable)\n  }\n\n  def reportFailure(t: Throwable) {}\n}\n<\/pre>\n<p>You can then give tasks to the pool with futures:<\/p>\n<pre lang=\"scala\">\nval tasks =\n  for (i <- 1 to 1000000) yield Future {\n    doSomething\n  }\n \nval aggr = Future.sequence(tasks)\nAwait.result(aggr, Duration.Inf)\n<\/pre>\n<p>For the above code to work, you'll need the following imports:<\/p>\n<pre lang=\"scala\">\nimport scala.actors.threadpool.Executors\nimport scala.concurrent.duration.Duration\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Part of the magic of Scala is that you can easily parallelize operations with features like Futures and parallel collections. When you do this, scala creates a thread pool, so that it can allocate appropriate resources. If you want to control this, you can make your own thread pool by copying the following code. This &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/creating-thread-pool-scala\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating a thread pool in Scala&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,482,486],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3552"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3552"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3552\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}