編集: 私はSharedBufferクラスにデータを送るプロデューサクラスを持っています。このデータは、制限が100に設定されたArrayListに追加されます。リストにデータを追加することは問題ありませんが、コンシューマクラスはリストからデータを取得できません。プロデューサ/コンシューマスレッディングの出力データがありません
出力が全く生成されません(ヌルまたはエラーなし)。
編集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クラス:あなたがアクセスするため、isEmpty
とisFull
メソッドを同期させる必要があります
@Override
public void run() {
while(true){
String line = buffer.getData();
if(line != null){
System.out.println(line);
//do stuff with the data.
}
}
}
あなたの問題について具体的に説明し、関連コードのみを投稿できますか? – talex
投稿を編集しました。私はそれが今より良いことを願っています。 –
どのようにデータをバッファに入れますか? – talex