0
编辑: 我有一个生产者类,发送一些数据到SharedBuffer类。这个数据被添加到一个ArrayList中,其限制被设置为100.向所述列表添加数据没有问题,但是使用者类没有设法从列表中获取任何数据。根本没有输出数据生产者/消费者线程
根本没有输出(没有空或错误)。
编辑2:添加了将数据放入数组的方法。
SharedBuffer类:
static final int RESOURCE_LIMIT = 100;
private List<String> data = new ArrayList<String>();
// private boolean done = false;
public boolean isFull(){
return data.size() >= RESOURCE_LIMIT;
}
public boolean isEmpty(){
return data.size() <= 0;
}
public synchronized void putData(String s){
while(this.isFull()){
try{
wait();
}catch(InterruptedException e){
//
e.printStackTrace();
}
}
data.add(s);
//size works and there is data in list.
//System.out.println(data.size() + data.get(0));
public boolean isEmpty(){
return data.size() <= 0;
}
public synchronized String getData(){
while(this.isEmpty()){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
String s_data = (String)(data.get(0));
if(s_data != null){
data.remove(s_data);
System.out.println(s_data);
}
return s_data;
}
Consumer类:
@Override
public void run() {
while(true){
String line = buffer.getData();
if(line != null){
System.out.println(line);
//do stuff with the data.
}
}
}
你能更具体地了解你的问题,并只发布相关的代码。 – talex
我已编辑帖子。我希望现在好一点。 –
如何将数据放入缓冲区。 – talex