Reputation: 15008
Suppose I have a thread pool with 10 threads. Let's say that one of the threads in the pool executes some task. I want that in certain cases, the task will stop its execution, the thread will take another task and only later it will return to the previous task and continue it from where it stopped.
Is this possible? How can I do it?
Upvotes: 0
Views: 218
Reputation: 15042
What you describe is what coroutines do when you mix them with a threadpool dispatcher, assuming of course that you are invoking suspend functions from within the coroutine context.
When the suspend function is invoked and suspends then the thread that was occupied in the threadpool is now free to execute any other coroutine that is ready.
If you only care about using the JVM then you can use Kotlin coroutines:
Achieving this in a pure Java environment might be possible using one of these coroutine frameworks for Java:
Upvotes: 2