简体   繁体   English

生产者 - 消费者的Java多线程

[英]Java multithreading on producer-consumer

I am following a video tutorial on Java multi-threading. 我正在关注Java多线程的视频教程。 It introduces on using Java to implement the famous "Producer-consumer" problem. 它介绍了如何使用Java来实现着名的“生产者 - 消费者”问题。

It used wait() and notifyAll() to ensure the proper communication between producer threads and consumer threads. 它使用wait()和notifyAll()来确保生产者线程和消费者线程之间的正确通信。

The tutor intentionally created several producer threads while only one consumer threads, but he left a question unanswered: "It is always best practice to have equal number of producer and consumer threads, if there are more producer threads than consumer, there will be problems". 导师故意创建了几个生产者线程,而只有一个消费者线程,但他留下了一个未回答的问题:“生产者和消费者线程数量相等,总是最好的做法,如果生产者线程多于消费者,就会出现问题” 。

However, he didn't specify what that problem is. 但是,他没有具体说明问题所在。 I personally imagine that would only be a situation that the basket is full. 我个人认为这只是篮子已满的情况。 Could experts help here? 专家可以帮忙吗? Thanks. 谢谢。

You could have any number of producers and any number of consumers. 您可以拥有任意数量的生产者和任意数量的消费者。

If the producers produce too fast, the queue will fill up, until you get memory problems or the producers are forced to stay idle until there's some place left in the queue. 如果生产者生产得太快,队列将填满,直到你遇到内存问题或生产者被迫保持闲置,直到队列中有一些地方。

If the consumers consume too fast, they will stay idle most of the time. 如果消费者消费太快,他们将在大多数时间保持闲置状态。

It is always best practice to have equal number of producers and consumer threads 拥有相同数量的生产者和消费者线程始终是最佳实践

I personally dont agree with this. 我个人不同意这一点。

How many producers and how consumer threads to have entirely depends on the situation. 有多少生产者以及消费者线程如何完全取决于具体情况。 There is no straight forward rule to that. 没有直截了当的规则。

I will tell you my case, 我会告诉你我的情况,

We had a program which was reading the CSV file,creating XML from it and publishing those XMLs on JMS Queue. 我们有一个程序正在读取CSV文件,从中创建XML并在JMS队列上发布这些XML。 This program was single threaded in the begining but the performance was not upto the mark.We found that publishing message to queue was taking much of time.So we decided to introduce multithreading. 这个程序在开始时是单线程的,但是性能没有达到标记。我们发现将消息发送到队列花了很多时间。所以我们决定引入多线程。

We Devided the logic in 2 threads- 我们在2个线程中划分逻辑 -

  1. Producer 制片人

    Reads the CSV and creates XML 读取CSV并创建XML

  2. Consumer 消费者

    Sends the XML to JMS queue. 将XML发送到JMS队列。

We decided to have only one producer, because we did not wanted to open multiple handles for the file though its possible. 我们决定只有一个生产者,因为我们不想为文件打开多个句柄尽管它可能。 On the other hand, we kept the number of consumer threads to be created configurable and it worked fine and we got expected performance gain. 另一方面,我们保持创建的消费者线程数量可配置,并且工作正常,我们获得了预期的性能提升。

To summarize, you need make sure that the speed of producer threads and consumer threads is almost matching.Otherwise if consumer thread is slower than producer, the records will pile up in memory(RAM) which can cause OutOfMemory in extreame cases. 总而言之,您需要确保生产者线程和消费者线程的速度几乎匹配。否则,如果消费者线程比生产者慢,则记录将堆积在内存(RAM)中,这可能导致极端情况下的OutOfMemory。

I don't see any issue besides the matter of Producer efficiency. 除了生产者效率之外,我没有看到任何问题。 If you have more Producers than Consumers, and they all produce/consume at the same rate, the extra Producers will effectively be idle. 如果你有比消费者更多的生产者,并且他们都以相同的速度生产/消费,那么额外的生产者将有效地闲置。

See What's best value for make -j , for example, where you're recommended to give n+1 jobs to your n cores, so that they're always under full load. 请参阅make -j的最佳值 ,例如,建议您为n内核提供n+1个作业,以便它们始终处于满负荷状态。

Well, the problem is the optimum use of resources namely the CPU and the memory. 那么,问题是资源最佳使用,即CPU和内存。 As you rightly guessed, the basket would be full (Hence more memory consumed), the producer thread would be idle for while till the consumer empties the basket for new tasks which will result into underutilization of CPU. 正如你正确猜测的那样,篮子将是满的(因此消耗更多的内存),生产者线程将闲置一段时间,直到消费者清空新任务的篮子,这将导致CPU的利用不足。

If there are equal number of producer and consumer, both the thread groups (producer and consumer) will be busy in their task and the basket would also have a steady storage. 如果生产者和消费者的数量相等,则线程组(生产者和消费者)将忙于他们的任务,并且篮子也将具有稳定的存储。

Now, too large or too less producer/consumers will also be a problem. 现在,太大或太少的生产者/消费者也将成为一个问题。 The large number of treads will hog the CPU and hence causing other problem (slowing etc). 大量的踏板会占用CPU,从而导致其他问题(减速等)。 So there is always an optimum number which depend on the hardware resources available on the machine. 因此总有一个最佳数量取决于机器上可用的硬件资源。

@JB Nizet summarized the situation pretty well, in that you can have too many producers or too many consumers. @JB Nizet很好地总结了这种情况,因为你可能有太多的生产者太多的消费者。 The determining factor is simply the rate at which producers produce versus the rate at which consumers consume. 决定因素只是生产者生产的比率与消费者消费的比率。 In other words, if it takes much more time to produce a resource than to consume it, than it is best to have more producers than consumers. 换句话说,如果生产资源需要花费更多的时间而不是消耗它,那么最好是拥有比消费者更多的生产者。 If, however, it takes more time to consume a resource than produce it, then it is best to have more consumers than producers. 但是,如果消耗资源比生产资源需要更多的时间,那么最好是拥有比生产者更多的消费者。

The tutor is probably assuming that the consumption process is more efficient than the production process, which may be true in many or possibly even most cases, but it's not necessarily a good assumption to make. 导师可能假设消费过程比生产过程更有效,这可能在许多甚至大多数情况下都是正确的,但它不一定是一个很好的假设。 Just try to keep this in mind when designing producer/consumer systems. 在设计生产者/消费者系统时,请尽量记住这一点。

I think it depends not on consumer/producer numbers but speed first of all. 我认为这不取决于消费者/生产者数量,而是取决于速度。 You may have 1 consumer which can process 1 message in 10 ms and 2 producers producing 1 message in 100 ms each. 您可能有1个消费者可以在10毫秒内处理1个消息,2个生产者可以在100毫秒内生成1个消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM