Sleeping a thread inside an ExecutorService (Java/Clojure)

前端 未结 2 685
悲哀的现实
悲哀的现实 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 16:00

    An executor is conceptually a task queue + a worker pool. Your explanation of what will happen here is basically correct. When you submit a task to the executor, the work is enqueued until a thread can execute the task. When it is executing the task, that task owns the thread and sleeping will block other tasks from being executed on that worker thread.

    Depending on what you're doing that may be ok (although it is unusual and probably bad form to sleep inside a task). It's more common to block a thread as a side effect of waiting on IO (blocked on a socket or db call for example).

    Generally if you are doing periodic work, it is better to handle that outside the pool and fire tasks when they should be executed, or better yet, use a ScheduledExecutorService instead from Executors/newScheduledThreadPool.

    The other main mechanism in Java for performing time-based tasks is java.util.Timer, which is a bit easier to use but not as robust as the ScheduledExecutorService.

    Another alternative from Clojure is to explicitly put the worker into a background thread managed by Clojure instead of by you:

    (defn do-task [] 
      (println (java.util.Date.) "doing task"))
    
    (defn worker [f n wait]
                (doseq [task (repeat n f)]
                       (f)
                       (Thread/sleep wait)))
    
    ;; use future to execute worker in a background thread managed by Clojure
    (future (worker do-task 10 1000))
    
    ;; the call to future returns immediately but in the background console
    ;; you will see the tasks being run.
    

提交回复
热议问题