# How to bridge Java Runnable with Kotlin Closures

**URL:** <https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996>\
**Category:** Uncategorized\
**Created:** [November 29, 2012, 11:47am UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996 "2012-11-29T11:47:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverPlow](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@OliverPlow](https://discuss.kotlinlang.org/u/OliverPlow)\
**Post date:** [November 29, 2012, 11:47am UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/1 "2012-11-29T11:47:52Z")

</div>

Hello,

I would like to do something like this:

> val list = ArrayList\<Runnable\>()  
> val func = { println(“hi”) }  
> println(func.javaClass.getName())  
> list.add(func)

Obviously, this won’t compile. Reason I want to do something like that is that I want to use [HawtDispatch](http://hawtdispatch.fusesource.org/) from within Kotlin code and I have to take HawtDispatch the way it was made. When I print the class name of func I get something like namespace$main$func$1. So that doesn’t give me a hint.

This code here in Groovy works and prints “hi” to the console:

> def list = new ArrayList\<Runnable\>()  
> def clos = { println(“hi”) }  
> list.add(clos)  
> list.get(0).run()

Is there some way to achieve the same in Kotlin? Then I find no way to get something like this to compile:

> &nbsp;&nbsp;var runnable = Runnable()  
> &nbsp;&nbsp;{  
> &nbsp;&nbsp;public fun run() {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;println(“foo”)  
> &nbsp;&nbsp;}  
> &nbsp;&nbsp;}

Any hints appreciated :-).  
Cheers, Oliver

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [November 29, 2012, 12:00pm UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/2 "2012-11-29T12:00:29Z")

</div>

You can define a funciton to convert from Kotlin Function0 to Runnable:

``

```kotlin
fun r(f: () -> Unit): Runnable = object : Runnable {override fun run() {f()}}
```

And then say  
``

```kotlin

val list = ArrayList<Runnable>()
val func = r { println(“hi”) }
println(func.javaClass.getName()) // this will be Runnable
list.add(func)

```

---

<div class="post-metadata">

**Author:** ![Natalia.Ukhorskaya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/natalia.ukhorskaya/32/1447_2.png) [@Natalia.Ukhorskaya](https://discuss.kotlinlang.org/u/Natalia.Ukhorskaya)\
**Post date:** [November 29, 2012, 12:10pm UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/3 "2012-11-29T12:10:33Z")

</div>

We also have a function "runnable" in kotlinLib - a helper function for creating a Runnable from a function

``

```kotlin

  val list = ArrayList<Runnable>()
  val clos = runnable {
           println(“hi”)
           }
  list.add(clos)
  list.get(0).run()

```

---

<div class="post-metadata">

**Author:** ![OliverPlow](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@OliverPlow](https://discuss.kotlinlang.org/u/OliverPlow)\
**Post date:** [November 29, 2012, 12:33pm UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/4 "2012-11-29T12:33:29Z")

</div>

Kool! That was helpful and very quick ;-). Thanks.

---

<div class="post-metadata">

**Author:** ![ivannikitin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ivannikitin/32/4516_2.png) [@ivannikitin](https://discuss.kotlinlang.org/u/ivannikitin)\
**Post date:** [December 8, 2017, 4:31pm UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/5 "2017-12-08T16:31:17Z")

</div>

> [@abreslav](#):
>
> fun r(f: () -\> Unit): Runnable = object : Runnable {override fun run() {f()}}

Simpler solution would be  
fun r(f: () → Unit): Runnable = Runnable { f() }

---

<div class="post-metadata">

**Author:** ![mochadwi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mochadwi/32/5282_2.png) [@mochadwi](https://discuss.kotlinlang.org/u/mochadwi)\
**Post date:** [December 30, 2019, 1:15pm UTC](https://discuss.kotlinlang.org/t/how-to-bridge-java-runnable-with-kotlin-closures/996/6 "2019-12-30T13:15:01Z")

</div>

awesome thanks @ivannikitin
