ExecutorService that executes tasks sequentially but takes threads from a pool

后端 未结 6 1775
孤城傲影
孤城傲影 2021-02-20 11:22

I am trying to build an implementation of the ExecutorService, let\'s call it SequentialPooledExecutor, with the following properties.

6条回答
  •  遥遥无期
    2021-02-20 11:45

    If you have thousands of keys which must be processed sequentially, but you don't have thousands of cores you can use a hashing strategy to distribute the work like this

    ExecutorService[] es = // many single threaded executors
    
    public  Future submit(String key, Callable calls) {
        int h = Math.abs(key.hashCode() % es.length);
        return es[h].submit(calls);
    }
    

    In general you only need 2 * N threads to keep N cores busy, if your task is CPU bound, more than that just adds overhead.

提交回复
热议问题