管程法
public class PCTest {
public static void main(String[] args) {
//建立一个容器
SynContainer synContainer = new SynContainer();
new Producer(synContainer).start();
new Consumer(synContainer).start();
}
}
//生产者只负责给容器中放入产品
class Producer extends Thread{
//需要向容器中加入产品
//定义一个容器
SynContainer container;
public Producer(SynContainer container){
this.container=container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//生产100次产品
//给容器添加产品
container.push(new Product(i));
System.out.println("生产者放入了产品编号为:"+i+"的产品");
}
}
}
//消费者只负责从容器中拿出产品
class Consumer extends Thread{
//通过容器操作
SynContainer container;
//通过有参构造传入容器
public Consumer(SynContainer container){
this.container=container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
//一次线程消费100次
//调用容器,取产品
Product product = container.pop();
System.out.println("消费者拿走了产品编号为:"+product.id+"的产品");
}
}
}
//缓冲区-----容器
class SynContainer{
//定义容器容量
Product[] products = new Product[10];
//定义产品索引,通过索引拿到产品
int index=0;
//生产者放入产品
public synchronized void push(Product product){
//1.如果容器满了,不再放入产品,等待消费者消费
if (index>=products.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//2.如果容器没有装满,生产者生产
products[index]=product;//给容器中放入产品
index++;
//装入产品后,通知消费者消费
this.notifyAll();//notifyAll()唤醒同一个对象上所有调用wait()方法的线程
}
//消费者拿走产品
public synchronized Product pop(){
// 如果容器为空,通知生产者生产,等待生产者放入产品
if (index<=0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//容器不为空,消费者消费
index--;
Product product = products[index];
//此时容器不满,通知生产者生产
this.notifyAll();
//将产品返回给消费者
return product;
}
}
//产品
class Product{
//产品编号
int id;
public Product(int id) {
this.id = id;
}
}
信号灯法
public class PCTest2 {
public static void main(String[] args) {
Blackboard blackboard = new Blackboard();
new Teacher(blackboard).start();
new Student(blackboard).start();
}
}
//生产者
class Teacher extends Thread{
Blackboard blackboard;
public Teacher(Blackboard blackboard){
this.blackboard=blackboard;
}
@Override
public void run() {
for (int i = 1; i < 50; i++) {
//老师在黑板上出题目
blackboard.setProblem(new Problem(i));
}
}
}
//信号灯
class Blackboard{
//定义一个信号灯标识
/*
flag为true老师出题,学生等待
flag为false学生做题,老师等待
*/
Boolean flag =true;
//没有容器,因此学生需要通过成员变量获取题目
Problem problem;
//出题
public synchronized void setProblem(Problem problem){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//老师出题
this.problem=problem;//将黑板上的题目更新为该方法传入的题目
System.out.println("老师在黑板上出了第"+problem.id+"道题");
//老师出完题,通知学生做题
this.notifyAll();
//必须反转标志位,否则下次唤醒调用该方法的线程后,该线程会一直处于等待状态
this.flag=!flag;
}
//做题
public synchronized Problem solveProblem(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//学生做完题,通知老师继续出题
this.notifyAll();
this.flag=!flag;
return problem;
}
}
//消费者
class Student extends Thread{
//学生看到黑板上的题目后做题
Blackboard blackboard;
public Student(Blackboard blackboard){
this.blackboard=blackboard;
}
@Override
public void run() {
//学生做题
for (int i = 1; i < 50; i++) {
Problem problem = blackboard.solveProblem();
System.out.println("学生做了第"+problem.id+"道题");
}
}
}
class Problem{
//问题编号
int id;
public Problem(int id) {
this.id = id;
}
}