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
相关问答:
我想使用Lucene的代码,发现要import一批文件:
============
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
………………
===== ......
我们有项目想组建一个开发团队,
主要用 tomcat,java,struts2,我们的核心成员都是经验非常丰富的系统设计师,
如果您在天津,同时对java比较感兴趣,
可以加入我们。
希望要求
1。爱好编程
......
用RandomAccessFile写入TXT文件查看时是乱码,请问有什么方法可以写成可读的文件,不是用其他的文件操作,就是只用RandomAccessFile的方法是否可以实现?
Java code:
import java.io.*;
public class RandomAcce ......