在ExecutorService中隐藏线程(Java / Clojure) [英] Sleeping a thread inside an ExecutorService (Java/Clojure)

查看:138
本文介绍了在ExecutorService中隐藏线程(Java / Clojure)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在clojure程序中创建了大量的线程:

 (import'(java.util。并发执行器)
(def * pool *
(Executors / newCachedThreadPool))

(defn do-something []
; work
Thread / sleep 200
; repeat)

(dotimes [i 10000]
(.submit * pool * do-something))
/ pre>

对我来说JVM之间已经有一段时间,我基本上想知道在这里是否有任何参数反对使用sleep或yield在正在执行的函数中执行者?如果我理解正确,在这种情况下,我的每个工人都有自己的线程,因此应该没有副作用。



如果执行器使用FixedThreadPool:

 (Executors / newFixedThreadPool 1000)

事情变得更加复杂,因为线程不会返回到池,直到他们的工作完成,这意味着其他排队的工作者将需要更长的时间来完成,如果线程正在睡觉。



我对这个实例中线程的理解是否正确?



(注意:我怀疑我的设计实际上是错误的,但只是想确保我在正确的页面)

解决方案

执行器概念上是一个任务队列+ 。你对这里发生的事情的解释基本上是正确的。当您向执行程序提交任务时,工作将排队,直到线程可以执行任务。当它执行任务时,该任务拥有线程,并且睡眠将阻止其他任务在该工作线程上执行。



根据你所做的,这可能是确定的(虽然它是不寻常的,可能是一个不好的形式睡在一个任务)。更常见的是阻塞线程作为等待IO的副作用(例如在套接字或数据库调用上被阻塞)。



一般来说,如果你正在做定期工作,最好在池外处理它们,并在应该执行时触发任务,或者更好地使用 ScheduledExecutorService ,而不是Executors / newScheduledThreadPool。



Java中执行基于时间的任务的另一个主要机制是 java.util.Timer ,它更容易使用,但不如ScheduledExecutorService那么强大。



Clojure的另一个替代方法是将工作者显式地放入由Clojure而不是由您管理的后台线程中:

 (defn do-task [] 
(println(java.util.Date。)doing task))

(defn worker [ fn wait]
(doseq [task(repeat nf)]
(f)
(Thread / sleep wait)))

;使用未来在Clojure管理的后台线程中执行worker
(future(worker do-task 10 1000))

;对未来的调用立即返回,但在后台控制台
;;您将看到正在运行的任务。


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

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

(defn do-something []
  ; work
  Thread/sleep 200
  ; repeat)

(dotimes [i 10000]
  (.submit *pool* do-something)) 

It's been a while between JVMs for me and I am basically wondering here if there is any argument against using sleep or yield inside the function that is being executed by the Executor? If I understand correctly, in this case, every one of my workers has it's own thread and therefore there should be no side effects.

If the Executor is using a FixedThreadPool:

(Executors/newFixedThreadPool 1000)

Things become more complicated because threads will not be returned to the pool until their work is complete, meaning the other queued workers will take longer to complete if the threads are sleeping.

Is my understanding of threading in this instance correct?

(Note: I suspect my design is actually wrong, but just want to make sure I am on the right page)

解决方案

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.

这篇关于在ExecutorService中隐藏线程(Java / Clojure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆