Thread pool that binds tasks for a given ID to the same thread

前端 未结 6 1891
别那么骄傲
别那么骄傲 2020-12-28 17:19

Are there any implementations of a thread pool (in Java) that ensures all tasks for the same logical ID are executed on the same thread?

The logic I\'m after is if t

6条回答
  •  春和景丽
    2020-12-28 17:59

    Create an array of executor services running one thread each and assign your queue entries to them by the hash code of your item id. The array can be of any size, depending on how many threads at most do you want to use.

    This will restrict that we can use from the executor service but still allows to use its capability to shut down the only thread when no longer needed (with allowCoreThreadTimeOut(true)) and restart it as required. Also, all queuing stuff will work without rewriting it.

提交回复
热议问题