当前位置: 首页 > 工具软件 > Notify.js > 使用案例 >

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

公良鸿畅
2023-12-01

一、区别

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
 类似资料: