等待线程(WaitThread )
package jintest;
public class WaitThread extends Thread {
public void run() {
waiting();
}
private synchronized void waiting() {
System.out.println("A--我开始等待");
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("A--我被通知了");
}
public void callup() {
synchronized (this) {
System.out.println("A--B一会就通知我");
notifyAll();
System.out.println("A--B刚刚通知我了");
}
}
}
--------------------------------------------------------------------------------
通知线程(CallThread )
package jintest;
public class CallThread extends Thread {
private WaitThread waiter;
public CallThread(WaitThread jinchun) {
waiter = jinchun;
}
public void run() {
System.out.println("B--我要去通知那个等待的线程");
waiter.callup();
System.out.println("B--我刚刚通知他了");
}
}
--------------------------------------------------------------------------------
16:33:46主线程(Test)
package jintest;
public class Test {
public static void main(String args[]) {
WaitThread A = new WaitThread();
A.start();
CallThread B = new CallThread(A);
B.start();
}
}