Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to launch a runnable within a Kotlin coroutine scope?

The context is that I need to work with some legacy Java code that uses the old world threading models. Thread pool executors, schedulers and runnables and threads.

However, my new code is all coroutine compatible.

So, given a runnable

val runnable = Runnable { 
    print("Hurray")
}

Is there a better way to run this runnable within a coroutine scope than the following which feels a little clumsy (you're really just going around the fact that you're trying to run a runnable and squeezing it in a coroutine)

GlobalScope.launch(Dispatchers.IO) { runnable.run() }

Is there a way interoperably work with runnables and coroutines?

like image 266
Aditya Anand Avatar asked Jan 04 '20 08:01

Aditya Anand


People also ask

How to start the coroutines in Kotlin?

There are mainly two functions in Kotlin to start the coroutines . The launch will not block the main thread, but on the other hand, the execution of the rest part of the code will not wait for the launch result since launch is not a suspend call. Following is a Kotlin Program Using Launch:

What is scope in Kotlin coroutine?

Scope in Kotlin’s coroutines can be defined as the restrictions within which the Kotlin coroutines are being executed. Scopes help to predict the lifecycle of the coroutines. There are basically 3 scopes in Kotlin coroutines: Global Scope. LifeCycle Scope. ViewModel Scope.

What is the difference between launch and launch of coroutine?

Note that all coroutines must run in a scope. A CoroutineScope manages one or more related coroutines. launch is a function that creates a coroutine and dispatches the execution of its function body to the corresponding dispatcher. Dispatchers.IO indicates that this coroutine should be executed on a thread reserved for I/O operations.

What is async in Kotlin?

Async is also used to start the coroutines, but it blocks the main thread at the entry point of the await () function in the program. Following is a Kotlin Program Using Async: One important point to note is that Async makes both of the networks call for result1 and result2 in parallel, whereas with launch parallel function calls are not made.


1 Answers

Given the entirety of your input in the question, this is all you need to execute the Runnable:

runnable.run()

Now, if you implicitly assume there is some long-lasting computation or blocking I/O behind that runnable, and you don't want to block the current thread, then you must dispatch it to a background thread pool, which is exactly the same you would have to do in Java. In that case you would have to write what you call the "clumsy" idiom.

If your wish is to be able to just write runnable.run() and have Kotlin somehow, automagically, turn your blocking IO code into suspending, that is impossible for fundamental reasons. The Java code in that case executes native system calls which block the calling thread and there is no way around that fact.

like image 89
Marko Topolnik Avatar answered Sep 30 '22 01:09

Marko Topolnik