Java 我的消费者(显然)能在少数情况下成为生产者吗
我正在使用Java 我的消费者(显然)能在少数情况下成为生产者吗,java,producer-consumer,blockingqueue,Java,Producer Consumer,Blockingqueue,我正在使用ExecutorService和BlockingQueue解决事务轮询案例中的生产者消费者问题。我有一个事务列表,我想验证并采取行动。我得到一个新的交易,以不断验证。 考虑到BlockingQueue,我只有一个生产者,我希望保留3-5个并行消费者以加快验证 我可能需要等待一些事务才能完成。(说30秒)。 所以,我将验证,如果它是假的,并且时间大于30秒,我将放弃它。基本上,我只想在数据项是可消费的情况下使用 首先,这种方法好吗?或者我应该尝试其他解决方案(目前我还不知道) 以下是我改
ExecutorService
和BlockingQueue
解决事务轮询案例中的生产者消费者问题。我有一个事务列表,我想验证并采取行动。我得到一个新的交易,以不断验证。
考虑到BlockingQueue,我只有一个生产者,我希望保留3-5个并行消费者以加快验证
我可能需要等待一些事务才能完成。(说30秒)。
所以,我将验证,如果它是假的,并且时间大于30秒,我将放弃它。基本上,我只想在数据项是可消费的情况下使用
首先,这种方法好吗?或者我应该尝试其他解决方案(目前我还不知道)
以下是我改编自的代码:
import java.util.concurrent.*;
公共类生产者消费者{
公共静态void main(字符串参数[]){
BlockingQueue sharedQueue=新建LinkedBlockingQueue();
ExecutorService pes=Executors.newFixedThreadPool(2);
ExecutorService ces=Executors.newFixedThreadPool(2);
pes.提交(新制作人(sharedQueue,1));
pes.提交(新制作人(sharedQueue,2));
提交(新消费者(sharedQueue,1));
提交(新消费者(sharedQueue,2));
//关机应该和终止一起发生在某个地方
/* https://stackoverflow.com/questions/36644043/how-to-properly-shutdown-java-executorservice/36644320#36644320 */
pes.shutdown();
ces.shutdown();
}
}
类生成器实现了Runnable{
私有最终阻塞队列sharedQueue;
私用int threadNo;
公共生产者(BlockingQueue sharedQueue,int-threadNo){
this.threadNo=threadNo;
this.sharedQueue=sharedQueue;
}
@凌驾
公开募捐{
对于(int i=1;iNo),执行peek()
+remove()
是不安全的。所有使用者都可以peek
查看相同的值,但删除不同的元素。或者将元素取回并放回(在队列末尾),或者更好地-更早地筛选元素并将它们放在单独的队列中。No,执行peek()
+remove()
是不安全的。所有消费者都可以peek
查看相同的值,但删除不同的元素。可以选择元素并将元素放回(队列末尾)或更好的方法-更早地筛选元素并将它们放在单独的队列中。酷!谢谢。我的另一个选择是take()
和put()
。我看不出这有任何其他问题,但“消费者(显然)在少数情况下成为生产者”的想法让我担心。可以吗?酷!谢谢。我的另一个选择是take()
和put()
。除了“消费者(显然)在少数情况下成为生产者”的想法之外,我看不到任何其他问题让我担心,没事吧?
import java.util.concurrent.*;
public class ProducerConsumerWithES {
public static void main(String args[]){
BlockingQueue<Integer> sharedQueue = new LinkedBlockingQueue<Integer>();
ExecutorService pes = Executors.newFixedThreadPool(2);
ExecutorService ces = Executors.newFixedThreadPool(2);
pes.submit(new Producer(sharedQueue,1));
pes.submit(new Producer(sharedQueue,2));
ces.submit(new Consumer(sharedQueue,1));
ces.submit(new Consumer(sharedQueue,2));
// shutdown should happen somewhere along with awaitTermination
/* https://stackoverflow.com/questions/36644043/how-to-properly-shutdown-java-executorservice/36644320#36644320 */
pes.shutdown();
ces.shutdown();
}
}
class Producer implements Runnable {
private final BlockingQueue<Integer> sharedQueue;
private int threadNo;
public Producer(BlockingQueue<Integer> sharedQueue,int threadNo) {
this.threadNo = threadNo;
this.sharedQueue = sharedQueue;
}
@Override
public void run() {
for(int i=1; i<= 5; i++){
try {
int number = i+(10*threadNo);
System.out.println("Produced:" + number + ":by thread:"+ threadNo);
sharedQueue.put(number);
} catch (Exception err) {
err.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
private final BlockingQueue<Integer> sharedQueue;
private int threadNo;
public Consumer (BlockingQueue<Integer> sharedQueue,int threadNo) {
this.sharedQueue = sharedQueue;
this.threadNo = threadNo;
}
@Override
public void run() {
while(true){
try {
int num = sharedQueue.take();
System.out.println("Consumed: "+ num + ":by thread:"+threadNo);
} catch (Exception err) {
err.printStackTrace();
}
}
}
}