java线程重用 [英] java thread reuse

查看:76
本文介绍了java线程重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都知道创建线程很贵。

我也知道你不能重新运行一个帖子。

I have always read that creating threads is expensive.
I also know that you cannot rerun a thread.

我在doc中看到执行者类:


创建一个根据需要创建新线程的线程池,但是当它们可用时会重复使用以前构造的线程。

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

注意'重复'这个词。

线程池如何重用线程?

How do thread pools 'reuse' threads?

推荐答案

我想我明白了什么让你困惑所以这里是我的答案:这个术语有点误导(显然,或者你不会问这个问题,特别强调重用):

I think I understood what is confuzzabling you so here's my longer answer: the terminology is a tiny bit misleading (obviously, or you wouldn't ask that question specifically putting the emphasis on 'reuse'):

线程池如何重用线程?

发生的事情是单个线程可用于处理多个任务(通常以 Runnable 的形式传递,但这取决于在你的'executor'框架上:默认执行程序接受 Runnable ,但你可以编写自己的executor/ thread-pool接受比 Runnable [例如, CancellableRunnable ])。

What is happening is that a single thread can be used to process several tasks (typically passed as Runnable, but this depend on your 'executor' framework: the default executors accepts Runnable, but you could write your own "executor" / thread-pool accepting something more complex than a Runnable [like, say, a CancellableRunnable]).

现在在default ExecutorService 实现如果某个线程在仍在使用时以某种方式终止,它会自动替换为新线程,但这不是他们正在谈论的'重用'。在这种情况下没有重用。

Now in the default ExecutorService implementation if a thread is somehow terminated while still in use, it is automatically replaced with a new thread, but this is not the 'reuse' they're talking about. There is no "reuse" in this case.

所以你确实无法调用 start() on一个Java线程两次但是你可以传递尽可能多的 Runnable 你想要一个执行者和每个 Runnable run()方法应调用一次。

So it is true that you cannot call start() on a Java Thread twice but you can pass as many Runnable as you want to an executor and each Runnable's run() method shall be called once.

你可以传递30 Runnable 到5个Java 线程并且每个工作线程可能正在调用,例如 run() 6次(实际上并不保证你将按照 Thread 执行6 Runnable 但是是一个细节)。

You can pass 30 Runnable to 5 Java Thread and each worker thread may be calling, for example, run() 6 times (practically there's not guarantee that you'll be executing exactly 6 Runnable per Thread but that is a detail).

在这个例子中, start()将被调用6次。每个这6个$ code> start()将只调用一次 run()方法每个线程

In this example start() would have been called 6 times. Each one these 6 start() will call exactly once the run() method of each Thread:

来自 Thread.start() Javadoc:

From Thread.start() Javadoc:


 * Causes this thread to begin execution; the Java Virtual Machine 
 * calls the <code>run</code> method of this thread.


但是然后在每个线程的内部 run()方法 Runnable 应该出列, run()将调用每个 Runnable 的方法。因此每个线程可以处理几个 Runnable 。这就是他们通过线程重用引用的内容。

BUT then inside each Thread's run() method Runnable shall be dequeued and the run() method of each Runnable is going to be called. So each thread can process several Runnable. That's what they refer to by "thread reuse".

执行自己的线程池的一种方法是使用阻塞队列,将runnables排入队列并使每个队列都有你的线程,一旦它完成了处理 runnable run()方法,就会使下一个 Runnable (或阻止)并运行其 run()方法,然后冲洗并重复。

One way to do your own thread pool is to use a blocking queue on to which you enqueue runnables and have each of your thread, once it's done processing the run() method of a Runnable, dequeue the next Runnable (or block) and run its run() method, then rinse and repeat.

我想部分混乱(而且有点令人困惑)来自于 Thread 需要一个 Runnable 并在调用 start()时默认线程调用Runnable的 run()方法pool Runnable

I guess part of the confusion (and it is a bit confusing) comes from the fact that a Thread takes a Runnable and upon calling start() the Runnable 's run() method is called while the default thread pools also take Runnable.

这篇关于java线程重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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