注意:
創(chuàng)新互聯(lián)是專業(yè)的睢陽(yáng)網(wǎng)站建設(shè)公司,睢陽(yáng)接單;提供成都做網(wǎng)站、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行睢陽(yáng)網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
java中的notifyAll和notify都是喚醒線程的操作,notify只會(huì)喚醒等待池中的某一個(gè)線程,但是不確定是哪一個(gè)線程,notifyAll是針對(duì)指定對(duì)象里面的所有線程執(zhí)行喚醒操作,指定對(duì)象一旦喚醒成功。則會(huì)立即加入線程的資源爭(zhēng)奪中去。
例如:
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() + "當(dāng)前沒有被執(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();// 針對(duì)當(dāng)前對(duì)象執(zhí)行喚醒所有線程的操作 System.out.println(Thread.currentThread().getName() + ":喚醒線程執(zhí)行成功!"); } } }
執(zhí)行結(jié)果為:
以上所述是小編給大家介紹的Java線程中的notifyAll喚醒操作,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!