生产者/消费者模式:需要使用到同步,以及线程,属于多并发行列
产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。 单单抽象出生产者和消费者,还够不上是生产者/消费者模式。该模式还需要有一个缓冲区处于生产者和消费者之间,作为一个中介。生产者把数据放入缓冲区,而消费者从缓冲区取出数据。
生产者:
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();
}
}
}