生产者消费者问题显示顺序错误
Producer consumer problem display a wrong order
以下生产者消费者代码显示错误顺序(在生产者生产消费者消费之前。有时生产者生产许多物品(cubbyhole 只允许一件物品))。这是为什么?
public class CubbyHole {
private int content;
private boolean available=false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {}
}
available = false;
notify();
return content;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
content = value;
available = true;
notifyAll();
}
}
public class Consumer extends Thread {
CubbyHole c;
public Consumer(CubbyHole c){
this.c=c;
}
public void run(){
int val=0;
for(int i =0;i<10;i++){
val=c.get();
System.out.println("consumer gets "+val);
}
}
}
public class Producer extends Thread {
CubbyHole c;
public Producer(CubbyHole c){
this.c=c;
}
public void run(){
for(int i=0;i<10;i++){
c.put(i);
System.out.println("Producer puts "+i);
}
}
}
public class Dimo {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p = new Producer(c);
Consumer con = new Consumer(c);
p.start();
con.start();
}
}
此代码得到以下输出
Producer puts 0
Producer puts 1
consumer gets 0
consumer gets 1
Producer puts 2
Producer puts 3
consumer gets 2
consumer gets 3
Producer puts 4
consumer gets 4
consumer gets 5
Producer puts 5
Producer puts 6
consumer gets 6
Producer puts 7
Producer puts 8
consumer gets 7
consumer gets 8
Producer puts 9
consumer gets 9
任何人都可以解释这段代码有什么问题吗?如何在此代码中获得正确的顺序?
检查这两行:
val=c.get();
System.out.println("consumer gets "+val);
可能发生的情况是消费者呼叫小房间并在空闲时在那里等待。然后它获取值并释放它。现在在这两行之间——在 c.get() 之后和 printlin 之前,另一个线程可以完成它的整个过程——调用 put(i) 并打印。那时您将获得两个 producer.put() 值。
您应该将打印部件移到小孔代码内,以确保打印出您想要的内容。
以下生产者消费者代码显示错误顺序(在生产者生产消费者消费之前。有时生产者生产许多物品(cubbyhole 只允许一件物品))。这是为什么?
public class CubbyHole {
private int content;
private boolean available=false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {}
}
available = false;
notify();
return content;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
content = value;
available = true;
notifyAll();
}
}
public class Consumer extends Thread {
CubbyHole c;
public Consumer(CubbyHole c){
this.c=c;
}
public void run(){
int val=0;
for(int i =0;i<10;i++){
val=c.get();
System.out.println("consumer gets "+val);
}
}
}
public class Producer extends Thread {
CubbyHole c;
public Producer(CubbyHole c){
this.c=c;
}
public void run(){
for(int i=0;i<10;i++){
c.put(i);
System.out.println("Producer puts "+i);
}
}
}
public class Dimo {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p = new Producer(c);
Consumer con = new Consumer(c);
p.start();
con.start();
}
}
此代码得到以下输出
Producer puts 0
Producer puts 1
consumer gets 0
consumer gets 1
Producer puts 2
Producer puts 3
consumer gets 2
consumer gets 3
Producer puts 4
consumer gets 4
consumer gets 5
Producer puts 5
Producer puts 6
consumer gets 6
Producer puts 7
Producer puts 8
consumer gets 7
consumer gets 8
Producer puts 9
consumer gets 9
任何人都可以解释这段代码有什么问题吗?如何在此代码中获得正确的顺序?
检查这两行:
val=c.get();
System.out.println("consumer gets "+val);
可能发生的情况是消费者呼叫小房间并在空闲时在那里等待。然后它获取值并释放它。现在在这两行之间——在 c.get() 之后和 printlin 之前,另一个线程可以完成它的整个过程——调用 put(i) 并打印。那时您将获得两个 producer.put() 值。
您应该将打印部件移到小孔代码内,以确保打印出您想要的内容。