有没有一种方法可以确保将线程分配给一组指定的对象? [英] Is there a way to ensure that threads are assigned to a specified set of objects?

查看:82
本文介绍了有没有一种方法可以确保将线程分配给一组指定的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个应用程序,在该应用程序中,从3个不同来源接收消息会影响一组对象.每个消息(来自任何来源)都有一个对象作为目标.每个消息接收器将在其自己的线程上运行.

We are working on an application where a set of objects can be affected by receiving messages from 3 different sources. Each message (from any of the sources) has a single object as its target. Each message receiver will be running on its own thread.

我们希望对消息的处理(在接收之后)尽可能快,因此针对目标对象的消息处理将使用线程池中的另一个线程来完成.与从发件人读取/接收消息相比,对消息的处理将花费更长的时间.

We want the processing of the messages (after receiving), to be as high-speed as possible, so the message processing against the target objects will be done with another thread from a thread pool. The processing of the message will take longer than the reading/receiving of the messages from the senders.

我认为,如果池中的每个线程仅专用于一组特定的对象,它将更快,例如:

I am thinking that it will be faster if each thread from the pool is dedicated only to a particular set of objects, for example:

Thread1 -> objects named A-L
Thread2 -> objects named M-Z

每组对象(或线程)都有专用的待处理消息队列.

with each set of objects (or Thread) having a dedicated queue of messages to pending being processed.

我的假设是,如果唯一需要的线程同步是在每个接收线程和一个处理线程之间,则在需要将消息放入阻塞队列的持续时间内,它将比随机分配工作线程更快来处理消息(在这种情况下,可能有2个不同的线程,并带有同一对象的消息).

My assumption is that if the only thread synchronization needed is between each receiving thread and one processing thread, for the duration of time that it needs to put the message on a blocking queue, that it will be faster than randomly assigning worker threads to process the messages (in which case there might be 2 different threads with messages for the same object).

我的问题实际上是两个部分:

My question is really 2 parts:

  1. 人们是否同意专用于工作者线程的假设 特定对象集是更好/更快的方法吗?

  1. Do people agree with the assumption that dedicating worker threads to a particular set of objects is a better/faster approach?

假设这是一种更好的方法,请执行现有的Java ThreadPool 上课有办法支持这一点吗?还是需要我们编码 我们自己的ThreadPool实现?

Assuming this is a better approach, do the existing Java ThreadPool classes have a way to support this? Or does it require us coding our own ThreadPool implementation?

感谢您提供的任何建议.

Thanks for any advice that you can offer.

推荐答案

[是]将工作线程专用于一组特定的对象是一种更好/更快的方法吗?

[Is] dedicating worker threads to a particular set of objects is a better/faster approach?

我认为总体目标是尝试最大程度地同时处理这些入站消息.您有来自3个来源的接收者,这些接收者需要将消息放入将进行最佳处理的池中.由于来自3个来源中的任何一个的消息都可能处理无法同时处理 的同一目标对象,因此您希望以某种方式划分消息,以便可以同时处理它们,但前提是必须保证不会引用相同的目标对象.

I assume the overall goals is to trying to maximize the concurrent processing of these inbound messages. You have receivers from the 3 sources, that need to put the messages in a pool that will be optimally handled. Because messages from any of the 3 sources may deal with the same target object which cannot be processed simultaneously, you want someway to divide up your messages so they can be processed concurrently but only if they are guaranteed to not refer to the same target object.

我会在目标对象(可能只是name.hashCode())上实现hashCode()方法,然后使用该值将对象放入BlockingQueue的数组中,每个数组都有一个使用它们的线程.使用Executors.newSingleThreadExecutor()数组会很好.通过队列数量修改哈希值模式,然后将其放入该队列中.您将需要预定义最大数量的处理器.取决于处理的CPU强度.

I would implement the hashCode() method on your target object (maybe just name.hashCode()) and then use the value to put the objects into an array of BlockingQueues, each with a single thread consuming them. Using an array of Executors.newSingleThreadExecutor() would be fine. Mod the hash value mode by the number of queues and put it in that queue. You will need to pre-define the number of processors to maximum. Depends on how CPU intensive the processing is.

因此,类似以下代码的东西应该起作用:

So something like the following code should work:

 private static final int NUM_PROCESSING_QUEUES = 6;
 ...
 ExecutorService[] pools = new ExecutorService[NUM_PROCESSING_QUEUES];
 for (int i = 0; i < pools.length; i++) {
    pools[i] = Executors.newSingleThreadExecutor();
 }
 ...
 // receiver loop:
 while (true) {
    Message message = receiveMessage();
    int hash = Math.abs(message.hashCode());
    // put each message in the appropriate pool based on its hash
    // this assumes message is runnable
    pools[hash % pools.length].submit(message);
 }

此机制的好处之一是,您可以限制目标对象之间的同步.您知道,同一目标对象将仅由单个线程更新.

One of the benefits of this mechanism is that you may be able to limit the synchronization about the target objects. You know that the same target object will only be updated by a single thread.

人们是否同意这样的假设,即将工作线程专用于一组特定的对象是一种更好/更快的方法?

Do people agree with the assumption that dedicating worker threads to a particular set of objects is a better/faster approach?

是的.这似乎是获得最佳并发的正确方法.

Yes. That seems the right way to get optimal concurrency.

假设这是一种更好的方法,现有的Java ThreadPool类是否可以支持此方法?还是需要我们编码自己的ThreadPool实现?

Assuming this is a better approach, do the existing Java ThreadPool classes have a way to support this? Or does it require us coding our own ThreadPool implementation?

我不知道任何实现此目的的线程池.但是,我会编写您自己的实现.就像上面的代码概述一样使用它们.

I don't know of any thread-pool which accomplishes this. I would not write your own implementation however. Just use them like the code outlines above.

这篇关于有没有一种方法可以确保将线程分配给一组指定的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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