package intro
thread;
class Test {
public synchronized void w() {
Systemoutprintln(go into w)
try {
wait()
} catch (InterruptedException e) {
// TODO Autogenerated catch block
eprintStackTrace()
}
Systemoutprintln(get out w)
notify()
}
public synchronized void n() {
Systemoutprintln(go into n)
try {
wait()
} catch (InterruptedException e) {
// TODO Autogenerated catch block
eprintStackTrace()
}
Systemoutprintln(get out n)
}
}
class ThreadA extends Thread {
Test t = null;
public ThreadA(Test t) {
thist = t;
}
public void run() {
tw()
}
}
class ThreadA extends Thread {
Test t = null;
public ThreadA(Test t) {
thist = t;
}
public void run() {
tn()
}
}
public class ThreadTest {
public static void main(String[] args) {
Test t = new Test()
ThreadA a = new ThreadA(t)
ThreadA a = new ThreadA(t)
astart()
astart()
}
}
结果
开始输出
go into w
go into n
过秒后输出
get out w
get out n