• java 生产者消费者简单实现demo


    第一种方式 使用BlockingQueue 阻塞队列

    public class Threads {


    public static void main(String[] args) {
    final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);
    produce produce=new produce(queue);
    consumer consumer=new consumer(queue);
    consumer.start();
    produce.start();

    }

    static class produce extends Thread{
    final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;

    public produce(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
    this.integerArrayBlockingQueue = integerArrayBlockingQueue;
    }

    @Override
    public void run() {
    while (true){
    Integer random=new Random().nextInt(10);
    integerArrayBlockingQueue.add(random);
    System.out.println("shen chan shu ju"+random);
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    static class consumer extends Thread{
    final ArrayBlockingQueue<Integer> integerArrayBlockingQueue;

    public consumer(ArrayBlockingQueue<Integer> integerArrayBlockingQueue) {
    this.integerArrayBlockingQueue = integerArrayBlockingQueue;
    }

    @Override

    public void run() {
    while (true){
    try {
    Integer element=integerArrayBlockingQueue.take();
    System.out.println("xiao fei shu ju "+element);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }

    }
    第二种方法利用 wait()和 notifyAll()
    public class Threads {
    private static String lock = "lock";

    public static void main(String[] args) {
    final List<Integer> list = new ArrayList<>(10);
    final Integer max = 10;
    produce produce1 = new produce(list, max);
    produce produce2 = new produce(list, max);
    consumer consumer = new consumer(list, max);
    consumer.start();
    produce1.start();
    produce2.start();
    }

    static class produce extends Thread {
    final List<Integer> list;
    final Integer max;

    public produce(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override
    public void run() {
    while (true) {
    synchronized (lock) {
    while (list.size() > max) {
    try {
    lock.wait();
    System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = new Random().nextInt(10);
    list.add(random);
    lock.notifyAll();
    System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
    }
    }
    }
    }

    static class consumer extends Thread {
    final List<Integer> list;
    final Integer max;

    public consumer(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override

    public void run() {
    while (true) {
    synchronized (lock) {
    while (list.isEmpty()) {
    try {
    lock.wait();
    System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = list.remove(0);
    lock.notifyAll();
    System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
    }

    }
    }
    }

    }
    第三种方法 ReentrantLock await() 和 signal() 实现
    public class Threads {

    private Lock lock=new ReentrantLock();
    final Condition notfull=lock.newCondition();
    final Condition notempty=lock.newCondition();
    public static void main(String[] args) {
    final List<Integer> list = new ArrayList<>(10);
    final Integer max = 10;
    Threads threads = new Threads();
    produce produce1 = threads.new produce(list, max);
    produce produce2 = threads.new produce(list, max);
    consumer consumer = threads.new consumer(list, max);
    consumer.start();
    produce1.start();
    produce2.start();
    }

    class produce extends Thread {
    final List<Integer> list;
    final Integer max;

    public produce(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override
    public void run() {
    while (true) {
    lock.lock();
    while (list.size() > max) {
    try {
    notfull.await();
    System.out.println("生产数据已满 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = new Random().nextInt(10);
    list.add(random);
    notempty.signal();
    System.out.println("线程" + Thread.currentThread().getName() + "正在生产数据" + random);
    }
    }
    }

    class consumer extends Thread {
    final List<Integer> list;
    final Integer max;

    public consumer(List<Integer> list, Integer max) {
    this.list = list;
    this.max = max;
    }

    @Override

    public void run() {

    while (true) {
    lock.lock();
    while (list.isEmpty()) {
    try {
    notempty.await();
    System.out.println("消费数据已空 线程" + Thread.currentThread().getName() + "已停止");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    int random = list.remove(0);
    notfull.signal();
    System.out.println("线程" + Thread.currentThread().getName() + "正在消费数据" + random);
    }

    }
    }

    }


  • 相关阅读:
    noip模拟赛 花
    noip模拟赛 柜(暴力)
    noip模拟赛 读
    Java基础知识强化47:StringBuffer类之StringBuffer的三个面试题
    Java基础知识强化46:StringBuffer类之判断一个字符串是否对称案例
    Java基础知识强化45:StringBuffer类之字符串反转的案例
    Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例
    Java基础知识强化43:StringBuffer类之StringBuffer和String的相互转化
    Java基础知识强化42:StringBuffer类之StringBuffer的截取功能
    Java基础知识强化41:StringBuffer类之StringBuffer的反转功能
  • 原文地址:https://www.cnblogs.com/prettrywork/p/10233428.html
Copyright © 2020-2023  润新知