Sleeping a thread inside an ExecutorService (Java/Clojure)

前端 未结 2 687
悲哀的现实
悲哀的现实 2021-02-09 15:41

I have a rather massive number of threads being created inside a clojure program:

(import \'(java.util.concurrent Executors)) 
(def *pool*   
  (Executors/newCac         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 15:47

    An alternative to sleeping your threads is to have each worker have a "sleepUntil" long value. When your executor calls a worker, if it is sleeping it returns immediately. Otherwise, it does its work, then returns. This can help keep your thread count down, because a FixedThreadPoolExecutor will be able to handle many more workers than it has threads, if most of them are flagged as sleeping and return quickly.

提交回复
热议问题