notify() 和 notifyAll()方法的使用和区别

一、区别

notify()和notifyAll()都是用来用来唤醒调用wait()方法进入等待锁资源队列的线程,区别在于:
notify()
唤醒正在等待此对象监视器的单个线程。 如果有多个线程在等待,则选择其中一个随机唤醒(由调度器决定),唤醒的线程享有公平竞争资源的权利
notifyAll()
唤醒正在等待此对象监视器的所有线程,唤醒的所有线程公平竞争资源

二、示例

notify()
public class ThreadDemo implements Runnable{
	static Object lock = new Object();

	public static void main(String[] args) {
		Thread thread1 = new Thread(new ThreadDemo(), "Thread-a");
		Thread thread2 = new Thread(new ThreadDemo(), "Thread-b");
		thread1.start();
		thread2.start();
		try {
			TimeUnit.SECONDS.sleep(1); // 睡会,让走到子线程
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		synchronized (lock) {
			System.out.println(Thread.currentThread().getName() + " 获得了锁");
			System.out.println("notify前" + thread1.getName() + "状态是" + thread1.getState());
			System.out.println("notify前" + thread2.getName() + "状态是 " + thread2.getState());
			lock.notify(); // 唤醒一个等待lock锁的线程
		}
		try {
			TimeUnit.MILLISECONDS.sleep(300); // 睡会,让被唤醒的子线程走完
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("notify后" + thread1.getName() + "状态是" + thread1.getState());
		System.out.println("notify后" + thread2.getName() + "状态是" + thread2.getState());
	}

	@Override
	public void run() {
		synchronized (lock) {
			System.out.println(Thread.currentThread().getName() + " 获得了锁");
			try {
				lock.wait(); // 等待被唤醒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " end");
		}
	}
}

结果:
可以看到,子线程在wait()后释放了锁并进入WAITTINT状态,主线程获得锁后调用notify(),这时候其中一个线程(栗子中是Thread-b,也有可能是a)被唤醒并获取到锁继续执行到TERMINATED,而另一个线程a一直WAITTINT状态

Thread-b 获得了锁
Thread-a 获得了锁
main 获得了锁
notify前Thread-a状态是WAITING
notify前Thread-b状态是 WAITING
Thread-b end
notify后Thread-a状态是WAITING
notify后Thread-b状态是TERMINATED
notifyAll()

上面的栗子如果使用notifyAll(),看下结果

public class ThreadDemo implements Runnable{
	static Object lock = new Object();

	public static void main(String[] args) {
		Thread thread1 = new Thread(new ThreadDemo(), "Thread-a");
		Thread thread2 = new Thread(new ThreadDemo(), "Thread-b");
		thread1.start();
		thread2.start();
		try {
			TimeUnit.SECONDS.sleep(1); // 睡会,让走到子线程
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("哈哈哈哈");
		synchronized (lock) {
			System.out.println(Thread.currentThread().getName() + " 获得了锁");
			System.out.println("notify前" + thread1.getName() + "状态是" + thread1.getState());
			System.out.println("notify前" + thread2.getName() + "状态是 " + thread2.getState());
			lock.notifyAll(); // 唤醒所有等待lock锁的线程
		}
		try {
			TimeUnit.MILLISECONDS.sleep(300); // 睡会,让被唤醒的子线程走完
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("notify后" + thread1.getName() + "状态是" + thread1.getState());
		System.out.println("notify后" + thread2.getName() + "状态是" + thread2.getState());
	}

	@Override
	public void run() {
		synchronized (lock) {
			System.out.println(Thread.currentThread().getName() + " 获得了锁");
			try {
				lock.wait(); // 等待被唤醒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " end");
		}
	}
}

结果,可以看到所有子线程都被唤醒并再次公平竞争锁直到线程终止

Thread-b 获得了锁
Thread-a 获得了锁
哈哈哈哈
main 获得了锁
notify前Thread-a状态是WAITING
notify前Thread-b状态是 WAITING
Thread-a end
Thread-b end
notify后Thread-a状态是TERMINATED
notify后Thread-b状态是TERMINATED
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值