Java中的notyfy()和notifyAll()的本质区别
wait()方法表示,放弃当前对资源的占有权,等啊等啊,一直等到有人通知我,我才会运行后面的代码。
notify()方法表示,当前的线程已经放弃对资源的占有,
通知等待的线程来获得对资源的占有权,但是只有一个线程能够从wait状态中恢复,
然后继续运行wait()后面的语句;
notifyall()方法表示,当前的线程已经放弃对资源的占有,
通知所有的等待线程从wait()方法后的语句开始运行。
读出什么区别没有?
上例子,先是一个nofiyall()的例子:
java代码
package com.thread.wait; public class wait { private int counter = 0; private string name = null; public wait(int counter,string name){ this.counter = counter; this.name = name; } public synchronized void dosomthing(){ int tempcounter = --counter; if(tempcounter <= 0){ customizednotifyall(); } else { while(tempcounter > 0){ try { system.out.println(thread.currentthread().getname()+"-<"+name+tempcounter+">"+"will invoke wait()"); --tempcounter; wait(); } catch (interruptedexception e) { e.printstacktrace(); notifyall(); } system.out.println(thread.currentthread().getname()+"-<"+name+tempcounter+">"+"has been actived"); } customizednotifyall(); } } public void customizednotifyall(){ notifyall(); system.out.println(thread.currentthread().getname()+"-<"+name+counter+">"+"::"+"invoked notifyall() and finished"); } }
java代码
package com.thread.wait; public class testthread implements runnable { private wait wait; public testthread(wait wait){ this.wait = wait; } public void run() { wait.dosomthing(); } public static void main(string [] args){ wait wait = new wait(4,"david"); thread t1 = new thread(new testthread(wait)); thread t2 = new thread(new testthread(wait)); thread t3 = new thread(new testthread(wait)); thread t4 = new thread(new testthread(wait)); t1.start(); t2.start(); t3.start(); t4.start(); } }
运行的结果:
thread-0-<david3>will invoke wait() thread-1-<david2>will invoke wait() thread-2-<david1>will invoke wait() thread-3-<david0>::invoked notifyall() and finished thread-0-<david2>has been actived thread-0-<david2>will invoke wait() thread-1-<david1>has been actived thread-1-<david1>will invoke wait() thread-2-<david0>has been actived thread-2-<david0>::invoked notifyall() and finished thread-0-<david1>has been actived thread-0-<david1>will invoke wait() thread-1-<david0>has been actived thread-1-<david0>::invoked notifyall() and finished thread-0-<david0>has been actived thread-0-<david0>::invoked notifyall() and finished
看到了吧,一旦调用notifyall()方法,所有的等待线程都会从调用wait()方法的地方继续运行起来。
这个运行结果可能每次都不一样,有时候只有两个线程运行完成而其余两个线程在等待其它线程调用notifyall()方法,有时候只有三个线程运行完成,而另一个还在等待中。
由于本文是讲解notify以及notifyall方法,所以对上面的原因不多加以解释。
然后是notify()方法的例子:
就是将wait类中的customizednotifyall()方法中的notifyall()方法换成notify()方法
运行结果:
thread-1-<david3>will invoke wait() thread-0-<david2>will invoke wait() thread-2-<david1>will invoke wait() thread-3-<david0>::invoked notifyall() and finished thread-1-<david2>has been actived thread-1-<david2>will invoke wait()
did you see that?所有的等待线程中,只有一个线程运行完成了,而其它的线程还在傻傻地等待,poor guys!
每次运行的结果会不一样,但是始终只有一个线程能够运行完成。
summary:
notify()方法只是让一个线程从wait中恢复过来,至于具体是哪个,那就得看那些线程的运气了(不设置优先级的情况下),继续执行后面的语句;
notifyall()方法是让所有的线程从wait中恢复过来,继续执行后面的语句。
以上所述是小编给大家介绍的java中的notyfy()和notifyall()的本质区别,希望对大家有所帮助
上一篇: Javascript中的一些基本概念
下一篇: 谈谈变量命名规范的重要性