0

編集: 私は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クラス:あなたがアクセスするため、isEmptyisFullメソッドを同期させる必要があります

@Override 
    public void run() { 
     while(true){ 
      String line = buffer.getData(); 
      if(line != null){ 
       System.out.println(line); 

       //do stuff with the data. 
      } 
     } 
    } 
+0

あなたの問題について具体的に説明し、関連コードのみを投稿できますか? – talex

+0

投稿を編集しました。私はそれが今より良いことを願っています。 –

+1

どのようにデータをバッファに入れますか? – talex

答えて

1

変更あなたのコード(notyfyAll() invokationを追加)も

public synchronized void putData(String s){ 
    while(this.isFull()){ 
     try{ 
      wait(); 
     }catch(InterruptedException e){ 
      // 
      e.printStackTrace(); 
     } 
    } 
    data.add(s); 
    notifyAll(); 
} 

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); 
    } 
    notifyAll(); 
    return s_data; 
} 

data

+0

ああ、そうです。私は決して消費者を起こさない。 –

関連する問題