java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > notify()和notifyAll()方法区别

关于notify()和notifyAll()方法的用法与区别

作者:撞撞~

本文详细解释了Java中`notify()`和`notifyAll()`的区别,前者唤醒一个等待线程,后者唤醒所有等待线程,并提供了示例代码,展示了`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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文