高并发编程:14、线程的waitSet

public class Thread16 {
	
	private static final Object LOCK = new Object();

	public static void main(String[] args) {
		for(int i = 0; i < 10; i++) {
			new Thread("thread"+i) {
				@Override
				public void run() {
					synchronized(LOCK) {
						try {
							System.out.println(Thread.currentThread().getName() + " wait ");
							LOCK.wait();
							System.out.println(Thread.currentThread().getName() + " active ");
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();
		}
		
		try {
			Thread.sleep(3*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		for(int i = 0; i < 10; i++) {
			synchronized(LOCK) {
				LOCK.notify();
			}
		}
	}
}
thread0 wait 
thread2 wait 
thread1 wait 
thread3 wait 
thread4 wait 
thread5 wait 
thread6 wait 
thread8 wait 
thread7 wait 
thread9 wait 
thread0 active 
thread5 active 
thread4 active 
thread3 active 
thread1 active 
thread2 active 
thread9 active 
thread7 active 
thread8 active 
thread6 active 
  • 所有对象都会有一个waitSet,用来存放调用了该对象wait方法之后进入block状态线程。
  • 线程被notify之后,不一定立即得到执行,状态变为runnable状态,等待cpu调度。
  • 线程从waitSet中被唤醒顺序不一定是FIFO(先进先出)。
  • 线程被唤醒后,必须从新获取锁。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值