# Non blocking interop with JVM like Deffered.await()

**URL:** <https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061>\
**Category:** Support\
**Created:** [November 27, 2020, 10:35am UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061 "2020-11-27T10:35:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![valentin.po](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@valentin.po](https://discuss.kotlinlang.org/u/valentin.po)\
**Post date:** [November 27, 2020, 10:35am UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/1 "2020-11-27T10:35:23Z")

</div>

Hello, I need a help with some interop with Java 8.

Mine application consist of 3 parts.

1. kotlin part that execute REST calls with suspend function for text recognition that should return Transcription. REST response could be immediate or could take 5-20 min, in this situation i’m delay for such time as I need response with Transcription.

2. Java part should return Transcription from step 1 to internal index engine. I’m using CompletableFuture.get() but as I see it block a Thread.

3. For those step used shared pool that catch incoming document, init it (step 2 → execute step 1) and index.

Is it possible some how interop step 2 like Deffered.await() to non block thread from thread pool?

Thanks.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [November 27, 2020, 12:28pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/2 "2020-11-27T12:28:23Z")

</div>

Probably you need [kotlinx.coroutines/integration/kotlinx-coroutines-jdk8 at master · Kotlin/kotlinx.coroutines · GitHub](https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-jdk8).

---

<div class="post-metadata">

**Author:** ![valentin.po](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@valentin.po](https://discuss.kotlinlang.org/u/valentin.po)\
**Post date:** [November 27, 2020, 12:36pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/3 "2020-11-27T12:36:45Z")

</div>

Thanks.  
I’m already using it, and it is wrap with CompletableFeature, which get() method is blocking, if I understand it correct.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [November 27, 2020, 5:33pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/4 "2020-11-27T17:33:46Z")

</div>

In Java you must use callbacks to write async non-blocking code.

Try `CompletableFuture.thenAccept` instead of calling `get`. The supplied callback will run when the task completes without blocking any threads. You can control the callback thread with `thenAcceptAsync`. The code that uses the result goes in the callback not after the call to `thenAccept`, which will return immediately without waiting.

I suggest referring to guide, here’s an example : [Java CompletableFuture Tutorial with Examples](https://www.callicoder.com/java-8-completablefuture-tutorial/#:~:text=CompletableFuture%20is%20used%20for%20asynchronous,its%20progress%2C%20completion%20or%20failure.)

---

<div class="post-metadata">

**Author:** ![valentin.po](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@valentin.po](https://discuss.kotlinlang.org/u/valentin.po)\
**Post date:** [November 30, 2020, 1:04pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/5 "2020-11-30T13:04:21Z")

</div>

It is not possible, as I have no access to change interface method. And this method is

Optional getText() - so I want implement it and make Thread non block.

Thanks.

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [November 30, 2020, 5:59pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/6 "2020-11-30T17:59:31Z")

</div>

All Java methods block their thread until they return. There is no way around this. To get a result without blocking the thread you must use a callback parameter, so the method can return immediately, and the callback can be stored and invoked later.

Even in Kotlin with coroutines, the suspend modifier is rewriting the code to use callbacks (it adds a callback parameter to the signature).

If you have a synchronous Java API that you are implementing, then it will have to block the thread. The standard way to implement such an API with coroutines is `runBlocking`. Be careful to ensure that `runBlocking` is not called from a coroutine dispatcher to avoid deadlocks.

---

<div class="post-metadata">

**Author:** ![valentin.po](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@valentin.po](https://discuss.kotlinlang.org/u/valentin.po)\
**Post date:** [November 30, 2020, 6:37pm UTC](https://discuss.kotlinlang.org/t/non-blocking-interop-with-jvm-like-deffered-await/20061/7 "2020-11-30T18:37:07Z")

</div>

Thanks!
