package myclassloadertest;
//子线程打印3次,主线程打印5次;再子线程打印3次,主线程打印5次;……
public class TraditionalTimerTest {
static boolean isSub = true;
static A a = new A();
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
public void run() {
while (true) {
synchronized (A.class) {
while (!isSub) {
try {
a.wait();
} catch (Exception e) {
}
}
try {
for (int i = 1; i <= 3; i++) {
System.out.println("i=" + i + " " + "sub");
}
isSub = false;
a.notify();
} catch (Exception e) {
}
}
}
}
}).start();
new TraditionalTimerTest().main();
}
void main() throws Exception {
while (true) {
synchronized (A.class) {
while (isSub) {
a.wait();
}
for (int i = 1; i <= 5; i++) {
System.out.println("i=" + i + " " + "main");
}
isSub = true;
a.notify();
}
}
}
}
class A {
}
我自己编的,为什么总是抛出异常啊? |