生产者、消费者模式的简单实现

概述

目录
在这里插入图片描述
一个生产者类,一个消费者类,一个仓库类,一个pojo类,和一个主类。

仓库类

仓库类有一个容器,容器的读取都进行加锁。

package com.concurrent;

import java.util.LinkedList;
import java.util.Vector;

public class Repository {
    
    
    private LinkedList<Student> students = new LinkedList<>();
    private int size;

    public Repository() {
    
    
    }

    public Repository(int size) {
    
    
        this.size = size;
    }

    public synchronized void push (Student student, String threadName) {
    
    
        while(this.size == students.size()) {
    
    
            try {
    
    
                System.out.println("仓库已满,无法再添加学生");
                this.wait();

            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        // 解锁
        this.notifyAll();
        students.addLast(student);
        System.out.println("线程"+threadName+"生产了一个对象,"+student.toString());
        try {
    
    
            Thread.sleep(1000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }

    public synchronized Student pop( String threadName) {
    
    
        while(0 == students.size()) {
    
    
            try {
    
    
                System.out.println("仓库已空,无法再使用学生");
                this.wait();

            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        // 解锁
        this.notifyAll();
        Student student = students.pop();
        System.out.println("线程"+threadName+"消耗了一个对象,"+student.toString());
        return student;
    }
}

生产者

public class Producer implements Runnable {
    
    
    private Repository repository ;
    private int number = 0;
    public Producer(Repository repository) {
    
    
        this.repository = repository;
    }

    @Override
    public void run() {
    
    
        while (true) {
    
    
            repository.push(new Student("张三",number++),Thread.currentThread().getName());
        }
    }
}

消费者

package com.concurrent;

public class Consumer implements Runnable {
    
    
    private Repository repository ;
    public Consumer(Repository repository) {
    
    
        this.repository = repository;
    }
    @Override
    public void run() {
    
    
        while (true) {
    
    
            repository.pop(Thread.currentThread().getName());
        }
    }
}

pojo类

package com.concurrent;

public class Student {
    
    
    private String name;
    private Integer age;

    public Student(String name, Integer age) {
    
    
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

主类

package com.concurrent;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Repository repository = new Repository(5);
        Producer p1 = new Producer(repository);
        Producer p2 = new Producer(repository);
        Producer p3 = new Producer(repository);

        Consumer c1 = new Consumer(repository);
        Consumer c2 = new Consumer(repository);


        Thread thread = new Thread(p1);
        thread.start();
        Thread thread2 = new Thread(p2);
        thread2.start();
        Thread thread3 = new Thread(p3);
        thread3.start();
        Thread thread4 = new Thread(c1);
        thread4.start();
        Thread thread5 = new Thread(c2);
        thread5.start();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41948178/article/details/105351048
今日推荐