java中生产者消费者问题
小弟编写一个生产者消费者,碰到一个非常郁闷的问题,百思不得其解。请各位高手帮忙!
程序出现的问题是产品应该是先生产才能消费。但是确有先消费后生产的情况,
截图如下:
代码如下:
package com.lcm;
public class ProducterConsumer {
public static void main(String[] args) {
int stackSize = 15;
Stack stack = new Stack(stackSize); //存储量为15的容器
Producer p = new Producer(stack, 20);
Consumer c = new Consumer(stack, 20);
new Thread(p).start();
new Thread(c).start();
}
}
class Thing {
int id;
public Thing(int id) {
this.id = id;
}
}
class Stack {
int counter = 0;
int stackSize;
Thing[] things = null;
public Stack(int stackSize) {
this.stackSize = stackSize;
things = new Thing[stackSize];
}
public synchronized void push(Thing thing) { //生产
while(counter == stackSize) { //已经满了 等待消费
System.out.println("已经存满!等待消费。。。。。");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
things[counter] = thing;
counter ++;
}
public synchronized Thing pop() { //消费
while(counter == 0
相关问答:
我的开发的平台是Myeclipes6.0 + tomcat5.x + mysql
我所有的编码方式都是用的UTF-8
我只用了Strtus框架
在一个form中如下用的是post的提交方式:
<form class="form" action=&quo ......
jsp学习前要了解那些知识?学了java了。不知道还要学习些什么知识才可以很好的学习jsp
HTML、JavaScript
直接学就可以了,遇到不会就现学就行。
Html css servlet
去了解浏览器与web服务器的简单交 ......
工作地点:上海张江
学历要求:大本或以上
专业要求:计算机相关专业
英语:熟练(美国项目,英语工作环境,英语是必须)
简历投递:wendy_qian@163.com
MSN:wendydzmm@hotmail.com
......
字符流的读和写最终在底层都是通过字节流来完成的吗? 读写文本文件字符流应该就可以了吧。。
各位大哥大姐帮帮忙阿
Java流包括字节流和字符流,字节流通过IO设备以字节数据的方式读入,而字符流则是通过字节流 ......
如题。
遥遥无期啊,买房难啊
22世纪吧,,,
明天就可以,但是明天永远是明天啊!!!!
买房?不是it民工想的。
加油吧~``
我觉得也是 房自己随便盖一个既是.... 买咱是别想了 ......