最近在重新学习有关 java 多线程方面的知识。然后使用内部锁的机制,想简单模拟一下生产者和消费者。 代码逻辑大概是这样的:每个生产者线程产生 10 个数据,然后供一个消费者消费。由于在 main 线程中设置了每个生产者线程的名字,所以想在生产者生成数据的逻辑中打印当前线程名。 我的疑惑:
代码如下与日志输出如下:
package org.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WaitAndNotify {
	private final Object lock = new Object();
	private final List<Integer> list = new ArrayList<>();
	private final Random random = new Random();
	public void producer() throws InterruptedException {
		synchronized (lock) {
			while (list.size() == 10) {
				lock.wait();
			}
			list.add(random.nextInt(100));
			if (list.size() == 10) {
				log.info("thread-{},{}", Thread.currentThread().getName(),
						Arrays.toString(list.toArray(new Integer[0])));
			}
			lock.notifyAll();
		}
	}
	public void consumer() throws InterruptedException {
		synchronized (lock) {
			while (list.size() < 10) {
				lock.wait();
			}
			list.clear();
			lock.notifyAll();
		}
	}
	static class MyProducer implements Runnable {
		private final WaitAndNotify waitAndNotify;
		public MyProducer(WaitAndNotify waitAndNotify) {
			this.waitAndNotify = waitAndNotify;
		}
		@SneakyThrows
		@Override
		public void run() {
			int i = 10;
			while (i > 0) {
				waitAndNotify.producer();
				i--;
			}
		}
	}
	static class MyConsumer implements Runnable {
		private final WaitAndNotify waitAndNotify;
		public MyConsumer(WaitAndNotify waitAndNotify) {
			this.waitAndNotify = waitAndNotify;
		}
		@SneakyThrows
		@Override
		public void run() {
			while (true) {
				waitAndNotify.consumer();
			}
		}
	}
	public static void main(String[] args) {
		WaitAndNotify waitAndNotify = new WaitAndNotify();
		Runnable runnable = new MyProducer(waitAndNotify);
		for (int i = 0; i < 20; i++) {
			Thread producer = new Thread(runnable, "producer-" + i);
			producer.start();
		}
		Thread consumer = new Thread(new MyConsumer(waitAndNotify), "consumer");
		consumer.start();
	}
}
2022-03-21 17:32:42,014 INFO  [learning-concurrency][producer-0] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-0,[49, 96, 0, 43, 23, 6, 79, 44, 28, 21]
2022-03-21 17:32:42,019 INFO  [learning-concurrency][producer-17] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-17,[92, 56, 9, 43, 77, 24, 53, 74, 44, 85]
2022-03-21 17:32:42,019 INFO  [learning-concurrency][producer-1] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-1,[86, 11, 10, 58, 5, 86, 3, 68, 73, 24]
2022-03-21 17:32:42,020 INFO  [learning-concurrency][producer-17] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-17,[10, 9, 97, 41, 81, 35, 84, 0, 86, 12]
2022-03-21 17:32:42,020 INFO  [learning-concurrency][producer-3] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-3,[13, 90, 30, 45, 1, 8, 43, 68, 49, 26]
2022-03-21 17:32:42,020 INFO  [learning-concurrency][producer-17] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-17,[81, 19, 34, 58, 89, 66, 74, 76, 20, 5]
2022-03-21 17:32:42,021 INFO  [learning-concurrency][producer-4] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-4,[48, 60, 0, 36, 97, 9, 42, 44, 67, 81]
2022-03-21 17:32:42,021 INFO  [learning-concurrency][producer-16] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-16,[77, 38, 17, 63, 71, 7, 80, 64, 61, 19]
2022-03-21 17:32:42,021 INFO  [learning-concurrency][producer-6] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-6,[47, 56, 50, 7, 89, 45, 32, 91, 97, 66]
2022-03-21 17:32:42,022 INFO  [learning-concurrency][producer-16] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-16,[27, 96, 24, 85, 3, 28, 23, 21, 93, 72]
2022-03-21 17:32:42,022 INFO  [learning-concurrency][producer-7] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-7,[4, 95, 19, 95, 42, 9, 49, 5, 54, 8]
2022-03-21 17:32:42,022 INFO  [learning-concurrency][producer-13] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-13,[43, 81, 97, 19, 40, 65, 48, 32, 61, 77]
2022-03-21 17:32:42,023 INFO  [learning-concurrency][producer-14] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-14,[67, 81, 84, 11, 73, 71, 65, 26, 78, 45]
2022-03-21 17:32:42,023 INFO  [learning-concurrency][producer-12] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-12,[54, 48, 67, 88, 0, 78, 99, 12, 49, 84]
2022-03-21 17:32:42,023 INFO  [learning-concurrency][producer-7] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-7,[58, 66, 53, 39, 14, 64, 2, 57, 27, 91]
2022-03-21 17:32:42,023 INFO  [learning-concurrency][producer-11] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-11,[91, 79, 70, 36, 34, 28, 63, 67, 17, 77]
2022-03-21 17:32:42,024 INFO  [learning-concurrency][producer-10] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-10,[2, 76, 42, 92, 91, 87, 64, 20, 25, 3]
2022-03-21 17:32:42,024 INFO  [learning-concurrency][producer-9] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-9,[46, 57, 98, 0, 28, 68, 63, 71, 97, 54]
2022-03-21 17:32:42,024 INFO  [learning-concurrency][producer-11] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-11,[26, 66, 10, 77, 69, 41, 77, 80, 21, 55]
2022-03-21 17:32:42,024 INFO  [learning-concurrency][producer-11] org.example.ch5._1_1.WaitAndNotify producer(33): thread-producer-11,[94, 97, 84, 87, 85, 53, 77, 88, 79, 84]
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.