java多线程(十三)Condition精准通知唤醒

Posted 500年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程(十三)Condition精准通知唤醒相关的知识,希望对你有一定的参考价值。

java多线程(十三)Condition精准通知唤醒

在这里插入图片描述

1、synchronized锁(随机唤醒)

public class ConditionTest {

	public static void main(String[] args) {

		Data data = new Data();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				try {
					data.increment();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "A").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				try {
					data.decrement();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "B").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				try {
					data.increment();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "C").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				try {
					data.decrement();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "D").start();
	}

}

class Data {
	private int num = 0;

	// +1
	public synchronized void increment() throws InterruptedException {
		while (num != 0) {
			this.wait();
		}
		num++;
		System.out.println(Thread.currentThread().getName() + " =>" + num);
		this.notifyAll();
	}

	// -1
	public synchronized void decrement() throws InterruptedException {
		while (num == 0) {
			this.wait();
		}
		num--;
		System.out.println(Thread.currentThread().getName() + " =>" + num);
		this.notifyAll();
	}
}

2、lock锁(Condition精准通知唤醒)

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest2 {

	public static void main(String[] args) {

		Data2 data = new Data2();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				data.printA();
			}
		}, "A").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				data.printB();
			}
		}, "B").start();

		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				data.printC();
			}
		}, "C").start();

	}

}

class Data2 {
	private int num = 1;

	Lock lock = new ReentrantLock();
	Condition condition1 = lock.newCondition();
	Condition condition2 = lock.newCondition();
	Condition condition3 = lock.newCondition();

	public void printA() {
		lock.lock();
		try {
			while (num != 1) {
				try {
					condition1.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + " AAAAAAA");
			num = 2;
			condition2.signal();
		} finally {
			lock.unlock();
		}
	}

	public void printB() {
		lock.lock();
		try {
			while (num != 2) {
				try {
					condition2.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + " BBBBBBBBBBBB");
			num = 3;
			condition3.signal();
		} finally {
			lock.unlock();
		}
	}

	public void printC() {
		lock.lock();
		try {
			while (num != 3) {
				try {
					condition3.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(Thread.currentThread().getName() + " CCCCCCCC");
			num = 1;
			condition1.signal();
		} finally {
			lock.unlock();
		}
	}
}

输出结果

A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC
A AAAAAAA
B BBBBBBBBBBBB
C CCCCCCCC

特别鸣谢:狂神说Java老师的无私奉献

.
.
.
上一篇 java多线程(十二)死锁及死锁问题排查
下一篇 java多线程(十四)八锁、CopyOnWriteArrayList、CopyOnWriteArraySet、ConcurrentHashMap、Callable

以上是关于java多线程(十三)Condition精准通知唤醒的主要内容,如果未能解决你的问题,请参考以下文章

Java——多线程高并发系列之Condition接口中的await()signal()signAll()方法

Java多线程:BlockingQueue实现原理(Condition原理)

Java多线程:BlockingQueue实现原理(Condition原理)

多线程之Condition的await和signalsignalAll等待/通知机制

多线程-Condition

java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)