在 producer/consumer 模式中使用多个生产者和消费者对 java 中的字符串进行排序有什么好处?
What's the benefit of using multiple producers and consumers in the producer/consumer pattern for sorting strings in java?
我们在 class 期间学习了 producer/consumer 模式,但仅以一个生产者和一个消费者为例。在我们的作业中,我们被要求做以下事情:
Write a program that generates random strings, then sorts them
alphabetically using the producer/consumer pattern. Create 1000
strings and use a bounded queue of size 100. Use 100 producers and 10
consumers.
谁能向我解释在这种情况下实施多个生产者和消费者而不是单一生产者和消费者的好处?我假设多个生产者可以生成多个随机字符串,但多个消费者的设计目的是什么?要更快地从队列中删除字符串,然后将它们放入另一个数组进行排序?
谢谢。
根据教授的回复,我认为作业是要求我们按字母顺序对每个字符串进行排序,例如:"aagpqhtpqht" -> "aaghhppqqtt"
,而不是我理解的"puatn", "qupango", "agnqotu", "bowtn" -> "agnqotu", "bowtn", "puatn", "qupango"
。
所以我假设消费者的角色是一次从队列中取出一个字符串,然后对这个字符串进行排序。
在这种情况下,在这里使用多个使用者按字母顺序对字符串进行排序没有任何好处。这应该是使用多个生产者和消费者实施 producer/consumer 模式的一种做法。
我们在 class 期间学习了 producer/consumer 模式,但仅以一个生产者和一个消费者为例。在我们的作业中,我们被要求做以下事情:
Write a program that generates random strings, then sorts them alphabetically using the producer/consumer pattern. Create 1000 strings and use a bounded queue of size 100. Use 100 producers and 10 consumers.
谁能向我解释在这种情况下实施多个生产者和消费者而不是单一生产者和消费者的好处?我假设多个生产者可以生成多个随机字符串,但多个消费者的设计目的是什么?要更快地从队列中删除字符串,然后将它们放入另一个数组进行排序?
谢谢。
根据教授的回复,我认为作业是要求我们按字母顺序对每个字符串进行排序,例如:"aagpqhtpqht" -> "aaghhppqqtt"
,而不是我理解的"puatn", "qupango", "agnqotu", "bowtn" -> "agnqotu", "bowtn", "puatn", "qupango"
。
所以我假设消费者的角色是一次从队列中取出一个字符串,然后对这个字符串进行排序。
在这种情况下,在这里使用多个使用者按字母顺序对字符串进行排序没有任何好处。这应该是使用多个生产者和消费者实施 producer/consumer 模式的一种做法。