java线程中的notifyAll唤醒操作
生活随笔
收集整理的這篇文章主要介紹了
java线程中的notifyAll唤醒操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
注意:
java中的notifyAll和notify都是喚醒線程的操作,notify只會喚醒等待池中的某一個線程,但是不確定是哪一個線程,notifyAll是針對指定對象里面的所有線程執(zhí)行喚醒操作,指定對象一旦喚醒成功。則會立即加入線程的資源爭奪中去。
例如:
package TestThread.ThreadSynchronized;public class TestWaitAll {public static void main(String[] args) {Test1 test1 = new Test1();Thread t = new Thread(test1, "線程1");Thread t1 = new Thread(test1, "線程2");Thread t2 = new Thread(test1, "線程3");Test2 test2 = new Test2(test1, "喚醒線程");t.start();t1.start();t2.start();try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}test2.start();} }class Test1 implements Runnable {public void run() {synchronized (this) {try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "當前沒有被執(zhí)行到!");}} }class Test2 extends Thread {private Test1 test1;String name;public Test2(Test1 test1, String name) {super(name);this.name = name;this.test1 = test1;}public void run() {synchronized (test1) {test1.notifyAll();// 針對當前對象執(zhí)行喚醒所有線程的操作System.out.println(Thread.currentThread().getName() + ":喚醒線程執(zhí)行成功!");}} }?執(zhí)行結(jié)果為:
?
轉(zhuǎn)載于:https://www.cnblogs.com/chaiyesong/p/6610110.html
總結(jié)
以上是生活随笔為你收集整理的java线程中的notifyAll唤醒操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 霍尔探头对高斯计测量的影响
- 下一篇: php网址变量怎么输出,【PHP网站】如