【Java互联网架构学习----002--01】多线程设计模式之生产者-消费者模式

生产者/消费者模式:需要使用到同步,以及线程,属于多并发行列

产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。 单单抽象出生产者和消费者,还够不上是生产者/消费者模式。该模式还需要有一个缓冲区处于生产者和消费者之间,作为一个中介。生产者把数据放入缓冲区,而消费者从缓冲区取出数据。

生产者:

package com.neo.study002.radio01;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liyy
 * @date 2020/5/13 22:52
 */
public class Provider implements Runnable {
    //共享缓存区
    private LinkedBlockingQueue<Data> queue;

    private volatile boolean isRunning = true;

    //计数器
    private static AtomicInteger count = new AtomicInteger();
    //随机对象
    private static Random r = new Random();

    public Provider(LinkedBlockingQueue<Data> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(r.nextInt(1000));
                int id = count.incrementAndGet();
                Data data = new Data(id, "数据" + id);
                System.out.println("当前线程:" + Thread.currentThread().getName() + ",获取了数据,id为:" + id + ", 进行装入公共缓冲区中...");
                if (!this.queue.offer(data, 2, TimeUnit.SECONDS)) {
                    System.out.println("提交到缓冲区失败...");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stop() {
        this.isRunning = false;
    }
}

消费者:

package com.neo.study002.radio01;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
/**
 * @author liyy
 * @date 2020/5/13 22:53
 */
public class Customer implements Runnable{
    //共享缓存区
    private LinkedBlockingQueue<Data> queue;

    //随机对象
    private static Random r = new Random();

    public Customer(LinkedBlockingQueue<Data> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true){

            try {
                Data data = queue.take();
                Thread.sleep(r.nextInt(1000));
                System.out.println("当前线程:" + Thread.currentThread().getName() + ",消费成功,消费数据id为:"+data.getId());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

数据对象:

package com.neo.study002.radio01;

/**
 * @author liyy
 * @date 2020/5/6 21:11
 */
public class Data {
    private int id;
    private String name;

    public Data(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

测试类:

package com.neo.study002.radio01;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author liyy
 * @date 2020/5/13 22:53
 */
public class TestProviderCustomer {
    public static void main(String[] args) {
        //内存缓冲区
        LinkedBlockingQueue<Data> queue = new LinkedBlockingQueue<Data>(10);

        Provider p1 = new Provider(queue);
        Provider p2 = new Provider(queue);
        Provider p3 = new Provider(queue);

        Customer c1 = new Customer(queue);
        Customer c2 = new Customer(queue);
        Customer c3 = new Customer(queue);

        ExecutorService pool = Executors.newCachedThreadPool();
        pool.execute(p1);
        pool.execute(p2);
        pool.execute(p3);
        pool.execute(c1);
        pool.execute(c2);
        pool.execute(c3);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        p1.stop();
        p2.stop();
        p3.stop();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值