オブジェクトロックの参照は変更できません

1670 ワード

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author max
 *  , 
 */
public class TestNotify{
    static Boolean lock = true;
//    static  A lock = new A(1);
    static Boolean condition = true;
    public static void main(String[] args) throws Exception{
        new TestThread().start();
        new TestThread().start();
        Thread.sleep(1000);
        System.out.println("Doing something");
        synchronized(lock){
//            Boolean b = lock;  // 
//            lock.setA(0);      // 1   condition , lock 
            lock = false;
//            b.notifyAll();     // 2
            lock.notifyAll();
        }
    }
}

class TestThread extends Thread{
    public void run(){
        synchronized(TestNotify.lock){
            while(TestNotify.lock){
//            while(TestNotify.lock.getA() == 1){    // condition
                try {
                    TestNotify.lock.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println(getId());
        }
    }
}

class A{
    int a;
    public A(int i){
        a = i;
    }

    public void setA(int i){
        a = i;
    }

    public int getA()
    {
        return a;
    }
}