{"id":12567,"date":"2015-10-11T17:43:00","date_gmt":"2015-10-11T16:43:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=b0250d37dc16cc452c5fe365b96b7209"},"modified":"2022-07-28T09:58:19","modified_gmt":"2022-07-28T07:58:19","slug":"kotlin-callable-and-executorservice","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2015\/10\/11\/kotlin-callable-and-executorservice\/","title":{"rendered":"Kotlin, Callable and ExecutorService"},"content":{"rendered":"<p>I&#8217;ve recently written about using <a href=\"http:\/\/przybyszd.blogspot.com\/2015\/09\/groovy-callable-and-executorservice.html\">Callable in Groovy<\/a>, but how does it look like in <a href=\"https:\/\/kotlinlang.org\/\">kotlin<\/a>?<\/p>\n<h2 id=\"callable-instance-as-separete-class\">Callable instance as separete class<\/h2>\n<p>First, let&#8217;s create class which implements Callable interface:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nclass MyJob : Callable {\n    override fun call() = 42\n}\n<\/pre>\n<p>Now we could pass instance of this class to executorService:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nfun callableAsClassInstance() = executorService.submit(MyJob()).get()\n<\/pre>\n<p>When we run it, as we expect, it returns 42. (Tests are written in groovy, not in kotlin).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\ndef 'should submit callable as class instance from kotlin'() {\n    expect:\n        callableExample.callableAsClassInstance() == 42\n}\n<\/pre>\n<h2 id=\"casting-to-callable\">Casting to Callable<\/h2>\n<p>If we create map with string &#8220;call&#8221; to closure or just closure and try to cast to Callable, we always obtain CastClassException:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nfun callableAsMap() = executorService.submit(mapOf(\"call\" to { 42 }) as Callable).get()\n\nfun callableAsClosure() = executorService.submit({ 42 } as Callable).get()\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\ndef 'should submit callable as closure from kotlin'() {\n    when:\n        callableExample.callableAsClosure() == 42\n    then:\n        thrown(ClassCastException)\n}\n\ndef 'should submit callable as map from kotlin'() {\n    when:\n        callableExample.callableAsMap() == 42\n    then:\n        thrown(ClassCastException)\n}\n<\/pre>\n<p>It does not work as in groovy&#8230;<\/p>\n<h2 id=\"pass-instance-method\">Pass instance method<\/h2>\n<p>So maybe passing an instance method which produces value will work?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nprivate fun callMe() = 42\n\nfun callableAsPassedFunction(): Int? {\n    return executorService.submit(::callMe).get()\n}\n<\/pre>\n<p>It does not even compile. Why? It moans that there is no submit method which could be called with such argument&#8230;<\/p>\n<p>But, what interesting, if we create a value to which we assign closure and pass it to submit method then everything is ok and get returns 42.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nprivate val callMe = { 42 }\n\nfun callableAsPassedLocalFunction(): Int? {\n    return executorService.submit(callMe).get()\n}\n<\/pre>\n<h2 id=\"inline-implementation\">Inline implementation<\/h2>\n<p>Of course, there is also an option to create Callable inline:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\">\nfun callableAsInlineImplementation() = executorService.submit(Callable { 42 }).get()\n<\/pre>\n<p>And this is IHMO the best and the most comfortable way to create Callable in kotlin, because its syntax is much nicer than in java or groovy.<\/p>\n<p>Source code is available <a href=\"https:\/\/github.com\/alien11689\/CallableInKotlin\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"I&#8217;ve recently written about using Callable in Groovy, but how does it look like in kotlin?Callable instance as separete class First, let&#8217;s create class which implements Callable interface:class MyJob : Callable&lt;Int&gt; {    override fun call() = 42}&#8230;\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[556],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12567"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12567"}],"version-history":[{"count":8,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12567\/revisions"}],"predecessor-version":[{"id":14634,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12567\/revisions\/14634"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}