主题:java线程同步 wait/notify
public class ThreadSuo {
private static int number = 0;
private static boolean ready = false;
private final static Object obj = new Object();
private final static Object obj1 = new Object();
private static class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
while (!ready) {
synchronized (obj) {
System.out.println("---------------->run: wait obj");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("run-------------->number:" + number);
synchronized (obj1) {
System.out.println("run------------->wake obj1");
obj1.notifyAll();
}
}
}
}
public static void main(String[] args) {
new MyThread().start();
synchronized (obj) {
number = 42;
obj.notifyAll();
System.out.println("------------------>wake obj");
}
synchronized (obj1) {
try {
obj1.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------->wait obj1");
ready = true;
}
System.out.println("------------------>main over");
}
private static int number = 0;
private static boolean ready = false;
private final static Object obj = new Object();
private final static Object obj1 = new Object();
private static class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
while (!ready) {
synchronized (obj) {
System.out.println("---------------->run: wait obj");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("run-------------->number:" + number);
synchronized (obj1) {
System.out.println("run------------->wake obj1");
obj1.notifyAll();
}
}
}
}
public static void main(String[] args) {
new MyThread().start();
synchronized (obj) {
number = 42;
obj.notifyAll();
System.out.println("------------------>wake obj");
}
synchronized (obj1) {
try {
obj1.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------->wait obj1");
ready = true;
}
System.out.println("------------------>main over");
}
}
请问下各位大神,怎么这个程序无法执行完,谢谢指点。