class WaitNotify extends Blockable { public WaitNotify(Container c) {super(c);} public synchronized void run() { while(true) { i++; update(); try { wait(1000); } catch (InterruptedException e) { System.err.println("Interrupted"); } } } } 上边是两种模式的例程
notify() is sending a call to a thread that has already stop;it do not tell which thread it will call,just sends the message to awake the stoped thread;similar to the notify(),notifyall() is used to sending calls to every method ,in spite of they are stop or runing.
如果线程1执行了object1.wait,而线程2执行了object2.notify,那么它们一点关系也没有。
public WaitNotify(Container c) {
super(c);
new Notifier(this);
}
public synchronized void run() {
while(true) {
i++;
update();
try {
wait();
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}class Notifier extends Thread {
private WaitNotify wn;
public Notifier(WaitNotify wn) {
this.wn=wn;
start();
}
public void run() {
while(true) {
try {
sleep(2000);
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
synchronized(wn) {
wm.notify();
}
}
}
}
因为我这个地方不管是java vc 都不太清楚
public WaitNotify(Container c) {super(c);}
public synchronized void run() {
while(true) {
i++;
update();
try {
wait(1000);
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}
上边是两种模式的例程
wait()
方法使调用它的线程进入等待状态 如果有时间参数 那么等待相应的时间 类似于sleep(int i);
notify()
对那些进入暂停状态的线程起作用 那个进程暂停 收到notify就重新运行
notifyall()对系统中所用得线程发消息
后来的看看我的总结对不对
谢谢
这个进程也被唤醒 对吗?ysbcg
那么我总结的对吗?
除了带参数的wait语句那个