62,614
社区成员
发帖
与我相关
我的任务
分享
package pcenshao.w_n.demo;
public class Test {
public static void main(String[] args) throws InterruptedException {
final T t1 = new T();
t1.setName("t1");
t1.start();
T t2 = new T(){
@Override
public void run() {
while(true){
synchronized (t1) {
t1.notify(); //唤醒在监视器上t1等待第一个线程
p("t1.nofity()");
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t2.setName("t2");
t2.start();
}
static class T extends Thread{
@Override
public void run() {
while(true){
p("running");
synchronized (this) {
try {
p("waiting for notify ...");
this.wait();
p("notified");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
static void p(String s){
System.out.println("thread:" + Thread.currentThread().getName() +"," + s);
}
}