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
{
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
解决方案 »
- 如何监听所有Excpetion
- JTabbedPane中动态加载一个JPanel的问题,在线等,大家帮帮忙。
- 求助,运行不正确,怎么改?
- while(true)如何退出循环?
- 一个swt版本升迁的问题
- 怎么用SWING做一个动态曲线图啊????
- 为什么这样报错?Exception in thread "main" java.lang.NoSuchMethodError: main
- 简单的问题
- 请问java可不可以重载运算符,比如"="和"[]"
- 路人甲,你的问题没有解决呀。其他高手请救救火吧
- 下面的程序为什么输出abdcbdcb而不是abdcabdc呢?
- fckeditor 如何实现删除文件和图片的功能。网上全是asp
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循环结束!!
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-,当然有可能出现超出容量的情况
{
return "wotou的数量 : "+id;
} 这个可以改成public String toString()
{
return "第"id+"个芋头";
} 还有就是,生产者在互斥代码段出来之前就notify,走完互斥段然后在打印之前,消费者一拿到锁立刻拿出芋头,可以抢在生产者之前打印
它解除了等待线程 但是生产者 还没有跳出那个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++;
}
生产了: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
消费: 先消费,再生产
消费: 先消费,再生产
消费: 先消费,再生产
消费: 先消费,再生产
消费: 先消费,再生产
消费: 先消费,再生产