wait()方法表示,放弃当前对资源的占有权,等啊等啊,一直等到有人通知我,我才会运行后面的代码。
notify()方法表示,当前的线程已经放弃对资源的占有,
通知等待的线程来获得对资源的占有权,但是只有一个线程能够从wait状态中恢复,
然后继续运行wait()后面的语句;
notifyAll()方法表示,当前的线程已经放弃对资源的占有,
通知所有的等待线程从wait()方法后的语句开始运行。
读出什么区别没有?
上例子,先是一个nofiyAll()的例子:
Java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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(); } } |
运行的结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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()方法
运行结果:
1
2
3
4
5
6
|
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()的本质区别,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/hao_kkkkk/article/details/54923844