Skip to content

Add priority hint for CoroutineDispatchers #1617

@khovanskiy

Description

@khovanskiy

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:

  1. Create new CoroutineContextElement (for example, CoroutinePriority);
  2. If CoroutinePriority is provided by CoroutineContext, 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions