我感觉自己的是对的,生产者和消费者的问题,我为了简单就是生产一件商品以后缓冲区就满了,不能再生产,当缓冲区空后就不能再消费,但总是会有连续生产两个的结果或消费两个的结果
class Producer extends Thread{
private PCQueue pcqueue;
private int number;
public Producer(PCQueue pcqueue,int number){
this.pcqueue=pcqueue;
this.number=number;
}
public void run(){
for(int i=0;i<10;i++){
pcqueue.put(i);
System.out.println("Producer # "+this.number+" put: "+i);
try{
sleep((int)Math.random()*100);
}catch(Exception e){}
}
}
}class PCQueue{
private int seq;
private boolean available=false;      //缓冲区
public synchronized int get(){
while(available==false){      //缓冲区空的
try{
System.out.println("缓冲区是空的,没有商品可以拿出来"+available);
// System.out.println(available);
wait();
}catch(InterruptedException e){}
}
available=false;
notifyAll();
return seq;
}
public synchronized void put(int value){
while(available==true){       //缓冲区满的
try{
System.out.println("缓冲区满的,不可以再生产商品"+available);
wait();
}catch(InterruptedException e){}
}
available=true;
seq=value;
notifyAll();
}

}
class Consumer extends Thread{
private PCQueue pcqueue;
private int number;
public Consumer(PCQueue pcqueue,int number){
this.pcqueue=pcqueue;
this.number=number;
}
public void run(){
int value=0;
for(int i=0;i<10;i++){
value=pcqueue.get();
System.out.println("Consumer #"+this.number+" got"+value);
}
}
}
public class ProducerConsumer {
public static void main(String args[]){
PCQueue pcqueue = new PCQueue();
Producer p1=new Producer(pcqueue,1);
Consumer c1=new Consumer(pcqueue,1);
p1.start();
c1.start();
}
}

解决方案 »

  1.   

    put,get是synchronized的,但
    System.out.println("Consumer #"+this.number+" got "+value);和
    System.out.println("Producer # "+this.number+" put: "+i);不是
      

  2.   


    class Producer extends Thread {
    private PCQueue pcqueue;
    private int number; public Producer(PCQueue pcqueue, int number) {
    this.pcqueue = pcqueue;
    this.number = number;
    } public void run() {
    for (int i = 0; i < 10; i++) {
    pcqueue.put(i);
    try {
    sleep((int) Math.random() * 100);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }class PCQueue {
    private int seq;
    private boolean available = false; // 缓冲区 public synchronized int get() {

    while (available == false) { // 缓冲区空的
    try {
    System.out.println("缓冲区是空的,没有商品可以拿出来" + available);
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println("Consumer got: " + this.seq);
    available = false;
    notifyAll();
    return seq;
    } public synchronized void put(int value) {

    while (available == true) { // 缓冲区满的
    try {
    System.out.println("缓冲区满的,不可以再生产商品" + available);
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println("Producer # put: " + value);
    available = true;
    seq = value;

    notifyAll();
    }}class Consumer extends Thread {
    private PCQueue pcqueue;
    private int number; public Consumer(PCQueue pcqueue, int number) {
    this.pcqueue = pcqueue;
    this.number = number;
    } public void run() {
    int value = 0;
    for (int i = 0; i < 10; i++) {
    value = pcqueue.get();
    }
    }
    }public class ProducerConsumer {
    public static void main(String args[]) {
    PCQueue pcqueue = new PCQueue();
    Producer p1 = new Producer(pcqueue, 1);
    Consumer c1 = new Consumer(pcqueue, 1);
    p1.start();
    c1.start();
    }
    }打印:
    Producer # put: 0
    Consumer got: 0
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 1
    Consumer got: 1
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 2
    Consumer got: 2
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 3
    Consumer got: 3
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 4
    Consumer got: 4
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 5
    Consumer got: 5
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 6
    Consumer got: 6
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 7
    Consumer got: 7
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 8
    Consumer got: 8
    缓冲区是空的,没有商品可以拿出来false
    Producer # put: 9
    Consumer got: 9
      

  3.   

    你锁的这个pcqueue  在这里操作。
      

  4.   

    为什么不考虑用BlockingQueue来实现。直接用内部锁来锁定方法快,效率不高。可伸缩性也很差,用可阻塞队列可以简化这种生产者和消费者模式!
      

  5.   

    在楼主代码的基础上分享:
    package com.meritit.dm.help;class Producer extends Thread {
    private PCQueue pcqueue; public Producer(PCQueue pcqueue) {
    this.pcqueue = pcqueue;
    } public void run() {
    while (true) {
    pcqueue.add();
    }
    }
    }class PCQueue {
    private int seq = 0; public synchronized void remove() {
    while (0 == seq) { // 缓冲区空的
    try {
    System.out.println("商品已消费,不可再消费,剩余-->" + seq);
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    seq = 0;
    System.out.println("Consumer remove: 剩余-->" + seq);
    notifyAll();
    } public synchronized void add() { while (1 == seq) { // 缓冲区满的
    try {
    System.out.println("商品已生产,不可再生产,剩余-->" + seq);
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    seq = 1;
    System.out.println("Producer # add: 剩余-->" + seq);
    notifyAll();
    }
    }class Consumer extends Thread {
    private PCQueue pcqueue; public Consumer(PCQueue pcqueue) {
    this.pcqueue = pcqueue;
    } public void run() {
    while (true) {
    pcqueue.remove(); }
    }
    }public class ProducerConsumer {
    public static void main(String args[]) throws InterruptedException {
    PCQueue pcqueue = new PCQueue();
    Producer p1 = new Producer(pcqueue);
    Consumer c1 = new Consumer(pcqueue);
    p1.start();
    c1.start();
    }
    }打印的结果:
    商品已消费,不可再消费,剩余-->0
    Producer # add: 剩余-->1
    商品已生产,不可再生产,剩余-->1
    Consumer remove: 剩余-->0
    商品已消费,不可再消费,剩余-->0
    Producer # add: 剩余-->1
    商品已生产,不可再生产,剩余-->1
    Consumer remove: 剩余-->0
    商品已消费,不可再消费,剩余-->0
    Producer # add: 剩余-->1
    商品已生产,不可再生产,剩余-->1
    Consumer remove: 剩余-->0
    商品已消费,不可再消费,剩余-->0
    Producer # add: 剩余-->1
    商品已生产,不可再生产,剩余-->1