[英]How does java.util.concurrent classes reuse threads?
I'm talking about the Thread
instances, if they get their Runnable
provided as a constructor argument and you can only execute their start
method once, how come the Executor*
family of classes reuse them? 我说的是
Thread
实例,如果它们将其Runnable
作为构造函数参数提供,并且您只能执行一次start
方法,那么Executor*
系列类如何重用它们?
PS: I know and use the Executors classes which are nicer abstraction than bare threads, I'm asking this just out of curiosity. PS:我知道并使用Executors类比裸线程更好,它是抽象的,我只是出于好奇而问。
The runnables (lets call them R) passed to executor threads are in fact wrapped inside other runnables (let's call them W). 传递给执行程序线程的可运行对象(称为R)实际上包装在其他可运行对象(称为W)中。 The pseudo-code of the run() method of W is
W的run()方法的伪代码为
while (threadMustRun) {
wait for new R to be submitted and assigned to this thread
execute R.run()
}
It's actually more complex than that, but you should get the idea. 它实际上比这更复杂,但是您应该明白这一点。 To really understand what it does, look at the code the the
ThreadPoolExecutor.Worker
inner class. 要真正了解它的作用,请查看
ThreadPoolExecutor.Worker
内部类的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.