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