注意:
java中的notifyAll和notify都是喚醒線程的操作,notify只會喚醒等待池中的某一個線程,但是不確定是哪一個線程,notifyAll是針對指定對象里面的所有線程執行喚醒操作,指定對象一旦喚醒成功。則會立即加入線程的資源爭奪中去。
線程yield、例如:
package TestThread.ThreadSynchronized;
public class TestWaitAll {
java delayqueue?public static void main(String[] args) {
Test1 test1 = new Test1();
Thread t = new Thread(test1, "線程1");
線程wait。Thread t1 = new Thread(test1, "線程2");
Thread t2 = new Thread(test1, "線程3");
Test2 test2 = new Test2(test1, "喚醒線程");
java callable。t.start();
t1.start();
t2.start();
多線程wait notify、try {
Thread.sleep(2000);
} catch (InterruptedException e) {
java callable接口和runnable、// TODO Auto-generated catch block
e.printStackTrace();
}
java implement。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() + "當前沒有被執行到!");
}
}
}
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();// 針對當前對象執行喚醒所有線程的操作
System.out.println(Thread.currentThread().getName() + ":喚醒線程執行成功!");
}
}
}
執行結果為:
以上所述是小編給大家介紹的Java線程中的notifyAll喚醒操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對聚米學院網站的支持!
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态