- 题目:请利用生产者-消费者模式,实现模拟银行取号机生产订单,银行操作员消费订单的过程。
- 即为Producer-Consumer问题,最常见的多线程编码过程中线程安全问题。
- 生产者负责生产对象到对象资源池,消费者通过对象资源池拿出具体的对象时空行消费,大多数的生产消费场景,大多数都可以归结到该技术问题上。如银行订单、抢票、秒杀等场景。
- 代码实现
package com.tl.job005.thread;
import java.util.LinkedList;
public class ProducerAndConsumerQuestion {
public static int waittingTime = 2000;
static class CustomerProducer implements Runnable {
private DealResourcePool resourcePool;
private String producerName;
public CustomerProducer(String producerName,
DealResourcePool resourcePool) {
this.producerName = producerName;
this.resourcePool = resourcePool;
}
public DealPojo produce(String content) {
int serialID = resourcePool.getDealUniqID();
DealPojo dealPojo = new DealPojo(serialID, content);
return dealPojo;
}
@Override
public void run() {
// TODO Auto-generated method stub
String content = "个人银行业务";
for (int i = 1; i <= 5; i++) {
DealPojo dealPojo = produce(content);
resourcePool.add(dealPojo);
System.out.println(this.producerName + " 已生产完成订单="
+ dealPojo.toString());
try {
Thread.sleep(waittingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.producerName + "已生成完成所有任务,即将退出线程!");
}
}
static class DealPojo {
private int id;
private String content;
@Override
public String toString() {
return "DealPojo [id=" + id + ", content=" + content + "]";
}
public DealPojo(int id, String content) {
this.id = id;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
static class DealResourcePool {
public static int serialID = 0;
public static Object dealLock = new Object();
public static Object poolOperatorLock = new Object();
public static LinkedList<DealPojo> pojoList = new LinkedList<DealPojo>();
public int getDealUniqID() {
synchronized (dealLock) {
serialID++;
return serialID;
}
}
public boolean add(DealPojo pojo) {
boolean isAddFlag = false;
synchronized (poolOperatorLock) {
isAddFlag = pojoList.add(pojo);
}
return isAddFlag;
}
public DealPojo take() {
DealPojo pojo = null;
synchronized (poolOperatorLock) {
pojo = pojoList.poll();
}
return pojo;
}
}
static class BankEmployeeConsumer implements Runnable {
private String consumerName;
private DealResourcePool dealResourcePool;
public BankEmployeeConsumer(String consumerName,
DealResourcePool dealResourcePool) {
this.consumerName = consumerName;
this.dealResourcePool = dealResourcePool;
}
@Override
public void run() {
boolean isRunning = true;
int idleCounter = 0;
int maxIdleTimes = 3;
while (isRunning && idleCounter <= maxIdleTimes) {
DealPojo pojo = this.dealResourcePool.take();
if (pojo == null) {
System.out.println(this.consumerName + " 暂无订单,请等待!!");
idleCounter++;
} else {
System.out.println(this.consumerName + " 已消费订单"
+ pojo.toString());
idleCounter = 0;
}
try {
Thread.sleep(waittingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.consumerName + "已消费完成所有任务,即将退出线程!");
}
}
public static void main(String[] args) {
int producerNumber = 3;
int consumerNumber = 1;
DealResourcePool resourcePool = new DealResourcePool();
for (int i = 1; i <= producerNumber; i++) {
CustomerProducer pro1 = new CustomerProducer("生产者-" + i,
resourcePool);
new Thread(pro1).start();
}
for (int i = 1; i <= consumerNumber; i++) {
BankEmployeeConsumer con1 = new BankEmployeeConsumer("消费者-" + i,
resourcePool);
new Thread(con1).start();
}
System.out.println("done!!!");
}
}
天亮教育是一家从事大数据云计算、人工智能、教育培训、产品开发、咨询服务、人才优选为一体的综合型互联网科技公司。
公司由一批BAT等一线互联网IT精英人士创建,
以"快乐工作,认真生活,打造IT职业技能教育的一面旗帜"为愿景,胸怀"让天下没有难找的工作"使命,
坚持"客户第一、诚信、激情、拥抱变化"的价值观,
全心全意为学员赋能提效,践行技术改变命运的初心。
更多学习讨论, 请加入
官方-天亮大数据交流-366784928
群二维码:
官方-天亮web前端交流-972788995
群二维码:
欢迎关注天亮教育公众号,大数据技术资料与课程、招生就业动态、教育资讯动态、创业历程分享一站式分享,官方微信公众号二维码:
天亮教育大数据官方群318971238,
天亮教育web前端官方群318971238,
爬虫、nlp技术qq群320349384
hadoop & spark & hive技术群297585251
教育培训官网:http://myhope365.com
项目研发业务尚云科技官网:http://shangyuninfo.com/
天亮教育公开课-从小白到大佬修成记-全系列视频地址:http://myhope365.com/news/index?id=66