以下のコードを実行しようとすると、コードは wait() のあるブロックにも notifyAll() のあるブロックにも入りません。ただし、プログラムの結果は「A B」または「B A」です。プログラムで何が欠けていたのかわかりません。
public class threads1 extends Thread {
static Object obj = new Object();
public threads1(String str) {
super(str);
}
public void run() {
try {
waitforsignal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void waitforsignal() throws InterruptedException {
synchronized (obj) {
System.out.println(Thread.currentThread().getName());
while (Thread.currentThread().getName() == "A") {
System.out.println("into wait");
obj.wait();
}
if ((Thread.currentThread().getName() == "B")) {
System.out.println("had notified");
obj.notifyAll();
}
}
}
public static void main(String... strings) throws InterruptedException {
Thread t1 = new threads1("A");
Thread t2 = new threads1("B");
t1.start();
t2.start();
}
}