package threadlearning;高手指教一下啊public class ProducerConsumer { public static void main(String[] args) 
{
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(c).start();
}}class WoTou
{
private int id;

public WoTou(int id)
{
this.id = id;
}

public String toString()
{
return "wotou的数量 : "+id; 
}
}class Producer implements Runnable 
{
SyncStack ss = null;
Producer(SyncStack ss) 
{
this.ss = ss;
}

public void run() 
{
for(int i=0; i<10; i++) 
{
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
}
}class Consumer implements Runnable 
{
SyncStack ss = null;
Consumer(SyncStack ss) 
{
this.ss = ss;
}

public void run() 
{
for(int i=0; i<10; i++) 
{
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class SyncStack
{
WoTou [] arrWt = new WoTou[6];
int index = 0;

public synchronized void push(WoTou wt)
{
while(index >= 6)
{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

this.notify();
arrWt[index]= wt;
index++;
}

public synchronized WoTou pop()
{
while(index==0)
{
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

this.notify();
index--;
return arrWt[index];
}

}
输出结果
消费了: wotou的数量 : 0
生产了:wotou的数量 : 0
生产了:wotou的数量 : 1
生产了:wotou的数量 : 2
生产了:wotou的数量 : 3
生产了:wotou的数量 : 4
生产了:wotou的数量 : 5
生产了:wotou的数量 : 6
生产了:wotou的数量 : 7--------------怎么会超出容量呢?????????????
消费了: wotou的数量 : 6
生产了:wotou的数量 : 8
消费了: wotou的数量 : 7
消费了: wotou的数量 : 8
生产了:wotou的数量 : 9
消费了: wotou的数量 : 9
消费了: wotou的数量 : 5
消费了: wotou的数量 : 4
消费了: wotou的数量 : 3
消费了: wotou的数量 : 2
消费了: wotou的数量 : 1

解决方案 »

  1.   

    SyncStack这个的容量并没有超出
    for(int i=0; i <10; i++) 

    WoTou wt = new WoTou(i); 
    ss.push(wt); 
    System.out.println("生产了:" + wt); 楼主new 10个WoTou,new一个就push一个,等到SyncStack满了,就wait(),此时程序并没有停止,等到SyncStack不满的时候,还会继续new WoTou,直到for循环结束!!
      

  2.   

    那为什么会     生产了:wotou的数量 : 7--------------怎么会超出容呢????????????? 那时SyncStack已经满罗 容量是6而已啊
      

  3.   


    public synchronized void push(WoTou wt) 

    while(index >= 6) 

    try { 
    this.wait(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 

    } this.notify(); 
    arrWt[index]= wt; 
    index++; 
    } public synchronized WoTou pop() 

    while(index==0) 

    try { 
    this.wait(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 

    } this.notify(); 
    index--; 
    return arrWt[index]; 
    } } 生产者生产,栈满了,该线程就进入wait,可是消费者消费,先解除了所有等待线程的等待,再去Index-,当然有可能出现超出容量的情况
      

  4.   

    消费者notify以后还是在互斥代码段里啊,生产者还是要等index--的吧
      

  5.   

    public String toString()
    {
    return "wotou的数量 : "+id;
    } 这个可以改成public String toString()
    {
    return "第"id+"个芋头";
    } 还有就是,生产者在互斥代码段出来之前就notify,走完互斥段然后在打印之前,消费者一拿到锁立刻拿出芋头,可以抢在生产者之前打印
      

  6.   


    它解除了等待线程 但是生产者 还没有跳出那个while循环啊,应该等index--之后才能生产啊
    那就不应该有  
             生产了:wotou的数量 : 7--------------
    public synchronized void push(WoTou wt) 

    while(index >= 6) 

    try { 
    this.wait(); 
    } catch (InterruptedException e) { 
    e.printStackTrace(); 

    } this.notify(); 
    arrWt[index]= wt; 
    index++; 

      

  7.   

    那个id是窝头的编号第一个就是1,第二个就是2“wotou的数量 :”这个表述根本就是错的这个互斥,主要是用来保证仓库不会满出来,不是控制生产数量的
      

  8.   

    消费了: wotou的数量 : 0
    生产了:wotou的数量 : 0
    生产了:wotou的数量 : 1
    生产了:wotou的数量 : 2
    生产了:wotou的数量 : 3
    生产了:wotou的数量 : 4
    生产了:wotou的数量 : 5
    生产了:wotou的数量 : 6
    生产了:wotou的数量 : 7--------------怎么会超出容量呢????????????? 从这可以看出,现在共生产了8个wotou,消费了1个wotou,index>=6的时候wait了,怎么会出现7个呢?WoTou wt = new WoTou(i);
    ss.push(wt);
    System.out.println("生产了:" + wt); 
    i=7的时候,ss.put(wt7);index=6;线程wait().
    这时消费者
    WoTou wt = ss.pop();//执行到这的时候,pop()方法,index--;生产者线程执行,打印了 "生产了:wotou的数量 : 7",然后才
    System.out.println("消费了: " + wt); 第一行打印 "消费了: wotou的数量 : 0",也是一样的道理,消费者执行pop(),index=0,wait(),
    WoTou wt = new WoTou(i);
    ss.push(wt);//index++了,消费者线程执行,打印了第一行.
    然后才
    System.out.println("生产了:" + wt); 程序简单改一改就看出来了.public void run() {
    for (int i = 0; i < 10; i++) {
    try {
    Thread.sleep(200);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("消费: 先消费,再生产");
    WoTou wt = ss.pop();
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }执行结果:
    生产:wotou ID : 0
    生产:wotou ID : 1
    生产:wotou ID : 2
    生产:wotou ID : 3
    生产:wotou ID : 4
    生产:wotou ID : 5//生产到6个,就开始消费.
    消费: 先消费,再生产
    生产:wotou ID : 6
    消费: 先消费,再生产
    生产:wotou ID : 7
    消费: 先消费,再生产
    生产:wotou ID : 8
    消费: 先消费,再生产
    生产:wotou ID : 9
    消费: 先消费,再生产
    消费: 先消费,再生产
    消费: 先消费,再生产
    消费: 先消费,再生产
    消费: 先消费,再生产
    消费: 先消费,再生产