您的位置:首页 > 移动开发 > Objective-C

java Object类wait和notify Demo

2012-11-20 20:12 357 查看
import java.util.logging.Level;
import java.util.logging.Logger;

class Task extends Thread {

@Override
public void run() {
try {
synchronized (this) {
Thread t = Thread.currentThread();
System.out.println(t.getId() + t.getName() + ":task start, wait for notify...");
this.wait();
System.out.println(t.getId() + t.getName() + ":task continue...");
}
} catch (InterruptedException ex) {
Logger.getLogger(Task.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

public class WaitDemo {

public static void main(String[] args) throws InterruptedException {
Task task = new Task();
Thread t = Thread.currentThread();
System.out.println(t.getId() + t.getName() + ":task start...");
task.start();
Thread.sleep(2000);
synchronized (task) {
task.notify();
}
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: