生產者、消費者模式的簡單實現

概述

目錄
在這裏插入圖片描述
一個生產者類,一個消費者類,一個倉庫類,一個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();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章