生产者消费者模型
以下是其中一种通过synchronize的实现:
import java.util.Queue;
import java.util.LinkedList;
public class ProducerAndConsumer {
private final int MAX_LEN = 10;
private Queue<Integer> queue = new LinkedList<>();
class Producer extends Thread {
@Override
public void run() {
producer();
}
private void producer() {
// 循环限制:无限
while (true) {
// 加锁对象:queue
synchronized (queue) {
// producer()的停止(暂停)条件
while (queue.size() == MAX_LEN) {
queue.notify();
System.out.println("当前队列满");
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.add(1);
queue.notify();
System.out.println("生产者生产一个任务,当前队列长度为: " + queue.size());
// 执行完休眠(停顿)500毫秒
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Consumer extends Thread {
@Override
public void run() {
consumer();
}
private void consumer() {
while (true) {
synchronized (queue) {
while (queue.size() == 0) {
queue.notify();
System.out.println("当前队列空");
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.poll();
queue.notify();
System.out.println("消费者消费一条任务, 当前队列长度为: " + queue.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ProducerAndConsumer pc = new ProducerAndConsumer();
Producer p = pc.new Producer();
Consumer c = pc.new Consumer();
p.start();
c.start();
}
}
两线程交替打印1-100
我们基于上面这种生产者消费者模型,重写成让两个线程交替打印1-100,
交替是绝对的,所以定义boolean state,每次打印完就翻转为false/true来实现交替。当state=false时,producer1不执行,当state=true,producer2不执行。
从1开始打印到100停止,我们定义一个int count=1,每次打印完count就让count+1。循环限制也不再是无限了,而是到count = 100时停止(while (count < 100) { … })。
public class printOneToHundred {
private static volatile boolean state = true;
private static volatile int count = 1;
private static final Object lock = new Object();
class Producer1 extends Thread {
@Override
public void run() {
producer1();
}
private void producer1() {
while (count < 100) {
synchronized (lock) {
while (!state) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程1打印:" + count);
count++;
state = false;
lock.notify();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Producer2 extends Thread {
@Override
public void run() {
producer2();
}
private void producer2() {
while (count < 100) {
synchronized (lock) {
while (state) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程2打印:" + count);
count++;
state = true;
lock.notify();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
printOneToHundred print = new printOneToHundred();
Producer1 p = print.new Producer1();
Producer2 c = print.new Producer2();
p.start();
c.start();
}
}
输出:
线程1打印:1
线程2打印:2
线程1打印:3
线程2打印:4
线程1打印:5
线程2打印:6
线程1打印:7
线程2打印:8
线程1打印:9
线程2打印:10
线程1打印:11
线程2打印:12
线程1打印:13
线程2打印:14
线程1打印:15
线程2打印:16
线程1打印:17
线程2打印:18
线程1打印:19
线程2打印:20
线程1打印:21
线程2打印:22
线程1打印:23
线程2打印:24
线程1打印:25
线程2打印:26
线程1打印:27
线程2打印:28
线程1打印:29
线程2打印:30
线程1打印:31
线程2打印:32
线程1打印:33
线程2打印:34
线程1打印:35
线程2打印:36
线程1打印:37
线程2打印:38
线程1打印:39
线程2打印:40
线程1打印:41
线程2打印:42
线程1打印:43
线程2打印:44
线程1打印:45
线程2打印:46
线程1打印:47
线程2打印:48
线程1打印:49
线程2打印:50
线程1打印:51
线程2打印:52
线程1打印:53
线程2打印:54
线程1打印:55
线程2打印:56
线程1打印:57
线程2打印:58
线程1打印:59
线程2打印:60
线程1打印:61
线程2打印:62
线程1打印:63
线程2打印:64
线程1打印:65
线程2打印:66
线程1打印:67
线程2打印:68
线程1打印:69
线程2打印:70
线程1打印:71
线程2打印:72
线程1打印:73
线程2打印:74
线程1打印:75
线程2打印:76
线程1打印:77
线程2打印:78
线程1打印:79
线程2打印:80
线程1打印:81
线程2打印:82
线程1打印:83
线程2打印:84
线程1打印:85
线程2打印:86
线程1打印:87
线程2打印:88
线程1打印:89
线程2打印:90
线程1打印:91
线程2打印:92
线程1打印:93
线程2打印:94
线程1打印:95
线程2打印:96
线程1打印:97
线程2打印:98
线程1打印:99
线程2打印:100
Process finished with exit code 0