java notifyall 唤醒顺序_Java线程中的notifyAll唤醒操作(推荐)

注意:

java中的notifyall和notify都是唤醒线程的操作,notify只会唤醒等待池中的某一个线程,但是不确定是哪一个线程,notifyall是针对指定对象里面的所有线程执行唤醒操作,指定对象一旦唤醒成功。则会立即加入线程的资源争夺中去。

例如:

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() + "当前没有被执行到!");

}

}

}

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() + ":唤醒线程执行成功!");

}

}

}

执行结果为:

47e386b6e20fb41cd9edefd4ad62f002.png

以上所述是小编给大家介绍的java线程中的notifyall唤醒操作,希望对大家有所帮助

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值