待って通知予想、スレッドが絞首刑になっていると動作しません

Aquib Javed:

以下のコードサンプルです:スレッドは私がanything.And私はMyIncrementorクラスのコンストラクタを呼び出すことに関してれたていたことをもう一つの疑問をしないのですどこ5つのまたは6の値を消費した後、私は知りません絞首刑になっています。当初、私はそれがあまりにも働いていなかった、MyIncrementorクラスの新しいオブジェクトを作成することによって、生産や消費者クラスでgetおよびセットを呼び出すようにしようとしていました

/**
 * 
 */
package multithreadingDemo;

/**
 * @author Aquib
 *
 */
public class ThreadCommDemo {

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        MyIncrementor mi=new MyIncrementor();
        Producer1 p=new Producer1(mi);
        Consumerrs c=new Consumerrs(mi);
        Thread t1=new Thread(p);
        Thread t2=new Thread(c);
        t1.start();
        t2.start();


    }

}



class MyIncrementor {
    int myCount;
    boolean valueSet;

    /**
     * @return the myCount
     */
    public synchronized int getMyCount() {
        if (!valueSet) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("get" + myCount);
        valueSet = true;
        notifyAll();
        return myCount;
    }

    /**
     * @param myCount
     *            the myCount to set
     */
    public synchronized void setMyCount(int myCount) {
        if (valueSet) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("Set" + myCount);
        this.myCount = myCount;
        valueSet = false;
        notifyAll();
    }

}

class Producer1 implements Runnable {
    MyIncrementor mi;

    public Producer1(MyIncrementor mi) {
        // TODO Auto-generated constructor stub
        this.mi=mi;
    }

    public void run() {

        for (int i = 1; i < 10; i++) {
            mi.setMyCount(i);
            System.out.println("Produced" + mi.myCount);
             try 
             {
                   Thread.currentThread().sleep((int)(Math.random() * 100));
             } 
             catch (InterruptedException ie) 
             {
                   ie.printStackTrace();
             }
        }
    }
}

class Consumerrs implements Runnable {
    MyIncrementor mi;

    public Consumerrs(MyIncrementor mi) {
        // TODO Auto-generated constructor stub
        this.mi=mi;
    }

    public void run() {

        for (int i = 1; i < 10; i++) {
            int val = mi.getMyCount();
            System.out.println("Consumed" + val);
        }

    }

}
嫉妬セルゲイ:

あなたは自分の中に論理的ミス持つMyIncrementorクラスを、あなたが設定されているvalueSetあなたはどちらかあなたの条件を変更したり、その逆にフラグバイスを設定する必要がありますので、誤っgetMyCountおよびsetMyCount方法。

だから、ここの修正バージョンですMyIncrementor

class MyIncrementor {
  int myCount;
  boolean valueSet = false; //line changed - just to show that by default it is initialized to false

  /**
   * @return the myCount
   */
  public synchronized int getMyCount() {
    while (!valueSet) { //corrected as advised in comments, see @Tom Hawtin - tackline answer for details
      try {
        wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.println("get" + myCount);
    valueSet = false; //line changed - after getting the value set flag to false
    notifyAll();
    return myCount;
  }

  /**
   * @param myCount
   *            the myCount to set
   */
  public synchronized void setMyCount(int myCount) {
    while (valueSet) { //corrected as advised in comments, see @Tom Hawtin - tackline answer for details
      try {
        wait();
      } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    System.out.println("Set" + myCount);
    this.myCount = myCount;
    valueSet = true; //line changed - after setting the value set flag to true
    notifyAll();
  }

}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=225460&siteId=1