简体   繁体   English

我应该使用一个线程来更新数组中每个对象的值吗

[英]Should I use one thread to update the value of each object in an array

I am doing mutual information for 50 words for a big corpus. 我正在为50个单词做一个大语料库的相互信息。 I need to count the concurrence for those target words and the neighbour words for each of them. 我需要计算这些目标词和每个目标词的邻近词的并发性。

There is an object array with size of 50, one for each word. 有一个大小为50的对象数组,每个单词一个。

I have a thread to read lines. 我有一个线程来读取行。 When there is a new line comes in, I need to update the values of all those objects. 当有新行出现时,我需要更新所有这些对象的值。

I want to write in a multithread way. 我想以多线程的方式编写。

Should I use 50 threads, and each thread is responsible for one object in this array? 我应该使用50个线程,并且每个线程负责此数组中的一个对象吗?

If I use one thread for each object, should I use 50 queues to store the input lines, and one queue for each thread? 如果我为每个对象使用一个线程,是否应该使用50个队列存储输入行,并为每个线程使用一个队列?

I am thinking Producer-Consumer pattern, but it looks a little different where there is just one queue... 我正在考虑生产者-消费者模式,但是在只有一个队列的情况下看起来有点不同...

Multithreading is most beneficial when running on multi-core/multi-cpu machines. 在多核/多cpu机器上运行时,多线程是最有益的。 The number of Threads should be inferred from the number of available cores on the machine. 线程数应从计算机上可用内核的数量推断出来。 If 5 cores are available, 5 Threads should be spun off. 如果有5个核心可用,则应拆分5个线程。 Any more threads and you could start loosing efficiency due to CPU context switch. 任何更多的线程,由于CPU上下文切换,您可能开始失去效率。

Here's an example of how you could do that: 这是一个如何做到这一点的例子:

int coreCount = Runtime.getRuntime().availableProcessors();
for(int i = 0; i < coreCount; i++) {
  Thread thread = new YourThreadClass();
  thread.start();
}

It is a bit confusing what your intention is. 您的意图有点令人困惑。 I'll try to discuss it in general. 我将尝试讨论它。 It is not advisable to have more threads running than you have CPU cores (some argue it is twice that number), because after that, you don't gain any more execution speed, but indeed pay a penalty. 不建议运行的线程多于CPU内核(有人说这是该数字的两倍),因为在那之后,您将无法获得更多的执行速度,但确实要付出一定的代价。 You should also consider what the effect of your "new line" input is. 您还应该考虑“新行”输入的效果。 If all objects are updated in the same manner, there is no need for that many queues, one should be sufficient. 如果所有对象都以相同的方式更新,则不需要那么多队列,一个队列就足够了。 If those objects are updated separately, at non-depending times, you could implement it as you suggested 50 threads with 50 queues, but if the update is uniform, you should consider finding a suitable solution with less threads (perhaps there is some logical connection between your objects, which could help you group some of them in fewer threads) 如果这些对象在非依赖时间分别更新,则可以按照建议的50个线程和50个队列的方式实现,但如果更新是统一的,则应考虑找到一个线程数量较少的合适解决方案(也许存在某种逻辑连接)之间的对象,这可以帮助您将一些对象归为更少的线程)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否应该为每个线程使用单独的ScriptEngine和CompiledScript实例? - Should I use a separate ScriptEngine and CompiledScript instances per each thread? 是否应该为每个线程创建一个新对象? - Should you create a new object for each thread? 我应该为每个组件制作一个GridBagConstraint对象 - Should I make one GridBagConstraint object for each component 我应该使用Same HttpUrlconnection还是每次定义一个新的? - Should i use Same HttpUrlconnection or define each time a new one? 我应该为每个类似的动作还是泛型的动作使用单独的ActionListener? - Should I use separate ActionListener for each similar action or generic one? 我有一个由2个对象组成的数组,当我使用for循环时,两个对象都得到了只有其中一个应该具有的更改 - I have an Array of 2 objects, when I use a for loop, both of the object gets the change that only one of them should have 如果我从多个线程使用 JSch,我应该如何使用它 - If I use JSch from more than one thread, how should I use it 我应该对我的应用程序中的所有SQLiteDatabase使用一个SQLiteHelper还是对其中每个使用一个? - Should I use one SQLiteHelper for all SQLiteDatabases in my app or one for each one of them? 我应该如何将 mockito 用于对象内的对象然后值 - How should i use mockito for object inside objects and then value 我应该使用一个全局Retrofit实例,还是为Android中的每个请求创建一个? - Should I use one global Retrofit instance or create one for each request in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM