[英]Thread Execution in Java
Problem statement : There are n Thread , n/2 thread are producer and n/2 are consumer, number produced by producer-1 thread must be consumed by consumer-1 thread.问题陈述:有n个线程,n/2个线程是生产者,n/2个是消费者,生产者1线程产生的数字必须被消费者1线程消耗。 Thread must also run in order, producer 1, then consumer 1, again producer 2 and then consumer 2…so on…..
线程也必须按顺序运行,生产者 1,然后是消费者 1,又是生产者 2,然后是消费者 2……依此类推……
I am implementing producer consumer in java using thread but requirement is that there are N/2 producer thread and N/2 Consumer Thread, N consumer thread should consume value produce by N producer and n-1 consumer thread should consume by n-1 producer value.我正在使用线程在java中实现生产者消费者,但要求是有N/2个生产者线程和N/2个消费者线程,N个消费者线程应该消耗由N个生产者产生的值,n-1个消费者线程应该由n-1个生产者消耗价值。
I have implementing this using blocking queue but not getting the desire output :我已经使用阻塞队列实现了这个,但没有得到想要的输出:
package paytm;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ProducerConsumerWithBlockingQueue {
public static class Producer implements Runnable {
private BlockingQueue<Integer> queue;
private int next = 0;
private String thereadName;
public Producer(BlockingQueue<Integer> queue,String threadName) {
this.queue = queue;
this.thereadName = threadName;
}
@Override
public void run() {
while (true) {
try {
if(next<10) {
queue.put(next);
System.out.println(thereadName+ " "+ next);
}
} catch (InterruptedException e) {
}
next++;
}
}
}
public static class Consumer implements Runnable {
private BlockingQueue<Integer> queue;
private String thereadName;
public Consumer(BlockingQueue<Integer> queue,String threadName) {
this.queue = queue;
this.thereadName = threadName;
}
@Override
public void run() {
while (true) {
synchronized (queue) {
Integer next;
try {
next = queue.take();
System.out.println(thereadName+ " "+ next);
} catch (InterruptedException e) {
}
}
}
}
}
public static void main(String args[]) throws Exception {
BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(1);
Thread producer1 = new Thread(new Producer(queue,"producer1"));
Thread producer2 = new Thread(new Producer(queue,"producer2"));
Thread consumer1 = new Thread(new Consumer(queue,"Consumer1"));
Thread consumer2 = new Thread(new Consumer(queue,"Consumer2"));
producer1.start();
producer2.start();
consumer1.start();
consumer2.start();
// producer1.join();
// producer2.join();
// consumer1.join();
// consumer2.join();
}
}
// Output :
producer1 0
consumer1 0
producer2 1
consumer2 1
producer3 2
consumer3 2 so on...
This might not do what you expect it to do:这可能不会做你期望它做的事情:
while (true)
synchronized(queue) {
...
}
}
Let's suppose that consumer1
wins the race and gets in to the synchronized
block while consumer2
is forced to wait.假设
consumer1
赢得比赛并进入synchronized
块,而consumer2
被迫等待。 What do you suppose will happen when consumer1
does its thing, and then exits from the synchronized
block?当
consumer1
做它的事情,然后从synchronized
块中退出时,你认为会发生什么?
The Java Language Specification does not say what must happen in that case, but in most implementations, what actually will happen is, consumer1
will go right back into the synchronized
block before consumer2
even starts to wake up. Java 语言规范没有说明在这种情况下必须发生什么,但在大多数实现中,实际会发生的是,
consumer1
将在consumer2
开始唤醒之前立即返回synchronized
块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.