-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
It will be useful for the tasks which depend on the quality of service. For example, we have N
abstract clients using the common thread pool via CoroutineDispatcher. In rare moments a few clients have tasks with high priority to execution and they should be computed as soon as possible.
Note: If we create multiple pools for each priority, the situation will turn out that if there are no tasks with a certain priority, some pools will be idle while others may be overloaded.
I guess, that the problem can be solved like that:
- Create new
CoroutineContextElement
(for example,CoroutinePriority
); - If
CoroutinePriority
is provided byCoroutineContext
, wrap a dispatched task into an object which can be compared by taken priority and put it into the executor.
The new context element can thus be used:
launch(Dispatchers.Default + CoroutinePriority(42)) {
...
}
The element behaves like a hint. It works only in cases when the dispatcher created from custom thread pool with PriorityQueue and the priority is provided by context, so it produces no overhead in the other cases.
Unfortunately, the most of classes related with Dispatchers are internal in the coroutines package, so it isn't possible to create this logic outside the coroutines library without pain. The another possible solution is to provide the extended API.