理论支持,点我点点我
代码有四部分:
Storage类:
/*
* @Author: robot-cmy
* @Date: 2021-11-21 18:47:47
* @Last Modified by: robot-cmy
* @Last Modified time: 2021-11-22 10:34:46
*/
//https://www.cnblogs.com/wkfvawl/p/11529681.html
package myOperationSystem.ProducerAndConsumer;
public class Storage {
private int[] mutex = { 1 }; // 互斥信号量mutex
private int[] empty = { 10 }; // 同步信f非号量,表示空闲缓冲区的数量
private int[] full = { 0 }; // 同步信号量,表示空闲缓冲区的数量
public int[] count = { 0 }; // 计数器,结束线程
private int huowu = 0; // 可用缓冲区的数量
// p操作
public synchronized void P(int[] num) {
num[0]--;
if (num[0] >= 0) {
return;
} else {
try {
System.out.println("\t" + Thread.currentThread().getName() + " 被阻塞");
wait();
System.out.println("\t" + Thread.currentThread().getName() + " 被唤醒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// v操作
public synchronized void V(int[] num) {
num[0]++;
if (num[0] > 0) {
return;
} else {
notify();
}
}
// 生产者方法
public synchronized void produce() {
P(empty);
P(mutex);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
huowu++; // 生产完后货物++
System.out.println(Thread.currentThread().getName() + " 把新产品放入缓冲区,库存还有: " + huowu);
V(mutex);
V(full);
}
// 消费者方法
public synchronized void consume() {
P(full);
P(mutex);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
huowu--; // 消费完后货物--
System.out.println(Thread.currentThread().getName() + " 从缓冲区取走了物品,库存还有: " + huowu);
V(mutex);
V(empty);
}
}
Consumer类:
/*
* @Author: robot-cmy
* @Date: 2021-11-21 19:13:50
* @Last Modified by: robot-cmy
* @Last Modified time: 2021-11-22 10:16:05
*/
package myOperationSystem.ProducerAndConsumer;
public class Consumer implements Runnable {
private Storage storage;
public Consumer() {
}
public Consumer(Storage storage) {
this.storage = storage;
}
@Override
public void run() {
while (true) {
storage.count[0]++;
if (storage.count[0] > 40) {
break;
}
storage.consume();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Producer类:
/*
* @Author: robot-cmy
* @Date: 2021-11-21 19:13:50
* @Last Modified by: robot-cmy
* @Last Modified time: 2021-11-22 10:16:05
*/
package myOperationSystem.ProducerAndConsumer;
public class Consumer implements Runnable {
private Storage storage;
public Consumer() {
}
public Consumer(Storage storage) {
this.storage = storage;
}
@Override
public void run() {
while (true) {
storage.count[0]++;
if (storage.count[0] > 40) {
break;
}
storage.consume();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Main类(主类):
/*
* @Author: robot-cmy
* @Date: 2021-11-22 11:16:17
* @Last Modified by: robot-cmy
* @Last Modified time: 2021-11-22 11:51:50
*/
//https://www.cnblogs.com/wkfvawl/p/11538431.html
package myOperationSystem.ReaderAndWriter;
public class Main {
public static void main(String[] args) {
// 三个读者一个写者
Book book = new Book();
Reader reader1 = new Reader(book);
Reader reader2 = new Reader(book);
Reader reader3 = new Reader(book);
Writer writer = new Writer(book);
Thread r1 = new Thread(reader1, "读者1:");
Thread r2 = new Thread(reader2, "读者2:");
Thread r3 = new Thread(reader3, "读者3:");
Thread w = new Thread(writer, "写者:");
r1.start();
r2.start();
r3.start();
w.start();
}
}