线程池与许多单独的线程 [英] Thread Pool vs Many Individual Threads

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

问题描述

我正处于一个问题,我无法决定采取哪种解决方案。

I'm in the middle of a problem where I am unable decide which solution to take.

这个问题有点独特。让我们这样说,我从网络连续接收数据(每秒2到4次)。现在每个数据都属于不同的组合。
现在,我们可以调用这些组,group1,group2等。

The problem is a bit unique. Lets put it this way, i am receiving data from the network continuously (2 to 4 times per second). Now each data belongs to a different, lets say, group. Now, lets call these groups, group1, group2 and so on.

每个组都有一个专用的作业队列,来自网络的数据被过滤并添加到它的相应处理组。

Each group has a dedicated job queue where data from the network is filtered and added to its corresponding group for processing.

首先,我为每个组创建了一个专用线程,它将从作业队列中获取数据,处理它然后进入阻塞状态(使用Linked)阻止队列。。

At first I created a dedicated thread per group which would take data from the job queue, process it and then goes to blocking state (using Linked Blocking Queue).

但是我的大四学生建议我应该使用线程池,因为这样线程不会被阻塞,并且可以被其他组用于处理。

But my senior suggested that i should use thread pools because this way threads wont get blocked and will be usable by other groups for processing.

但事实上,我获得的数据足够快,并且线程处理它的时间足够长,线程可能不会进入阻塞模式。这也将保证数据按顺序处理(作业1在作业2之前完成),在合并中,可能不会发生很少的机会。

But here is the thing, the data im getting is fast enough and the time a thread takes to process it is long enough for the thread to, possibly, not go into blocking mode. And this will also guarantee that data gets processed sequentially (job 1 gets done before job 2), which in pooling, very little chances are, might not happen.

我的高级我还倾向于这样一个事实:池化也会为我们节省大量的内存,因为线程已经被POOLED(我认为他真的想要这个词;))。虽然我不同意这个,因为,我个人认为,汇集与否每个线程都有自己的堆栈内存。除非在线程池中有一些我不知道的东西。

My senior is also bent on the fact that pooling will also save us lots of memory because threads are POOLED (im thinking he really went for the word ;) ). While i dont agree to this because, i personally think, pooled or not each thread gets its own stack memory. Unless there is something in thread pools which i am not aware of.

最后一件事,我一直认为汇集可以帮助工作在短时间内出现在大量工作中。这是有道理的,因为线程生成会导致性能下降,因为初始化线程所花费的时间比执行作业所花费的时间多得多。所以汇集在这里有很多帮助。

One last thing, I always thought that pooling helps where jobs appear in a big number for short time. This makes sense because thread spawning would be a performance kill because of the time taken to init a thread is lot more than time spent on doing the job. So pooling helps a lot here.

但在我的情况下group1,group2,...,groupN始终保持活着状态。因此,如果有数据,他们仍然会在那里。因此线程产生不是问题所在。

But in my case group1, group2,...,groupN always remain alive. So if there is data or not they will still be there. So thread spawning is not the issue here.

我的大四学生并不相信,并希望我使用池化解决方案,因为它的内存占用很大。

My senior is not convinced and wants me to go with the pooling solution because its memory footprint is great.

那么,走哪条路?

谢谢。

推荐答案

好问题。
正如你所说,池化确实可以节省你的初始化时间。但它有另一个方面:资源管理。在这里,我问你这个 - 你有多少组(读专用线程)?
它们是否在应用程序的执行期间动态增长?

Good question. Pooling indeed saves you initialization time, as you said. But it has another aspect: resource management. And here I am asking you this- just how many groups (read- dedicated threads) do you have? do they grow dynamically during the execution span of the application?

例如,考虑一下这个问题的答案是肯定的情况。新的组类型是动态添加的。在这种情况下,您可能不希望将aa线程专用于每个线程,因为从技术上讲,对将要创建的组的数量没有限制,您将创建大量线程并且系统将进行上下文切换而不是执行实际工作。
线程池到救援线程池允许您指定对可能可能创建的最大线程数的限制,而不考虑负载。因此,应用程序可能会拒绝某些请求的服务,但是那些通过的服务会得到妥善处理,而不会严重耗尽系统资源。

For example, consider a situation where the answer to this question is yes. new Groups types are added dynamically. In this case, you might not want to dedicate a a thread to each one since there is technically no restrictions on the amount of groups that will be created, you will create a lot of threads and the system will be context switching instead of doing real work. Threadpooling to the rescue- thread pool allows you to specify a restriction on the maxumal number of threads that could be possibly created, with no regard to load. So the application may deny service from certain requests, but the ones that get through are handled properly, without critically depleting the system resources.

考虑到上述情况,我很有可能在你的情况下,为每个组都有一个专用的
线程是非常好的!

Considering the above, I is very possible that in your case, it is very much OK to have a dedicated thread for each group!

同样适用于你的大四学生的信念,它会节省内存..实际上,一个线程占用堆上的内存,但实际上它是多少,如果它是一个预定义的数量,比如说5.即使10-它可能是好的。无论如何,你不应该使用汇集,除非你是一个小修道院并且绝对相信你确实遇到了问题!

The same goes for your senior's conviction that it will save memory.. Indeed, a thread takes up memory on the heap, but is it really so much, if it is a predefined amount, say 5. Even 10- it is probably OK. Anyway, you should not use pooling unless you are a-priory and absolutely convinced that you actually have a problem!

池化是一项设计决策,而非建筑决策。如果您在遇到性能问题后发现合并,则无法在初始化时进行池化并继续进行优化。

Pooling is a design decision, not an architectural one. You can not-pool at the beggining and proceed with optimizations in case you find pooling to be beneficial after you encountered a performance issue.

考虑请求的序列化(按顺序执行)无论您使用的是线程池还是专用线程。顺序执行是队列的一个属性,加上一个处理程序线程。

Considering the serialization of requests (in order execution) it is no matter whether you are using a threadpool or a dedicated thread. The sequential execution is a property of the queue coupled with a single handler thread.

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

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