思考
这不就是线程池嘛,
组件: 任务队列、线程队列
呃任务队列需要被线程队列里面的线程消费,所以需要用到生产者和消费者模式
案例
比如有多个开发商买了土体合同,每个土地合同都需要施工队去开发建筑,
备注 : 一个合同只能有一个施工队去承保开发,且施工队同一时间只能承保一个合同直到结束
图示
合同
/**
* 合同
* @Author: puhaiguo
* @Date: 2022-07-12 00:41
* @Version 1.0
*/
public class Contract {
/**合同立定时间*/
private Date signDate;
/**建筑的小区名字*/
private String arcName;
/*商户名称*/
private String merchantName;
public Contract(String arcName, String merchantName) {
this.signDate = new Date();
this.arcName = arcName;
this.merchantName = merchantName;
}
@Override
public String toString() {
return "Contract{" +
"signDate=" + signDate +
", arcName='" + arcName + '\'' +
", merchantName='" + merchantName + '\'' +
'}';
}
}
合同队列
/**
* 合同文件夹队列
* @Author: puhaiguo
* @Date: 2022-07-12 00:46
* @Version 1.0
*/
public class ContractFolder {
/*合同文件夹队列*/
private final Contract[] contracts;
/*施工队集合*/
private ConstructionTeam[] constructionTeams;
private int head;
private int tail;
private final int CONTRACT_LENGTH;
public ContractFolder(int contractLength, int countTeam) {
this.contracts = new Contract[contractLength];
this.CONTRACT_LENGTH = contractLength;
init(countTeam);
}
/**
* 初始化施工队集合
* @author: puhg_sinosoft
* @date: 2022/7/12 0:58
* @param
*/
private void init(int count){
constructionTeams = new ConstructionTeam[count];
for (int i = 0; i < count; i++) {
constructionTeams[i] = new ConstructionTeam(this, "施工队 " + i +"对");
constructionTeams[i].start();
}
}
/**
* 添加合同
* @author: puhg_sinosoft
* @date: 2022/7/12 0:51
* @param contract
*/
public synchronized void putContract(Contract contract) {
try {
while ((tail + 1) % CONTRACT_LENGTH == head) {
wait();
}
contracts[tail] = contract;
tail = (tail+1) % CONTRACT_LENGTH;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
notifyAll();
}
}
/**
* 获取合同
* @author: puhg_sinosoft
* @date: 2022/7/12 0:52
* @param
* @return Contract
*/
public synchronized Contract takeContract() {
Contract contract = null;
try {
while (head == tail) {
wait();
}
contract = contracts[head];
contracts[head] = null;
head = (head+1) % CONTRACT_LENGTH;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
notifyAll();
}
return contract;
}
}
施工队
/**
* 施工队
* @Author: puhaiguo
* @Date: 2022-07-12 00:55
* @Version 1.0
*/
public class ConstructionTeam extends Thread{
private ContractFolder contractFolder ;
public ConstructionTeam(ContractFolder contractFolder, String nameTeam) {
super(nameTeam);
this.contractFolder = contractFolder;
}
@Override
public void run() {
while (true){
Contract contract = contractFolder.takeContract();
System.out.println(Thread.currentThread().getName() + " 承包了 " + contract);
}
}
}
测试类
/**
*
* @Author: puhaiguo
* @Date: 2022-07-12 01:01
* @Version 1.0
*/
public class Test {
public static void main(String[] args) {
ContractFolder contractFolder = new ContractFolder(10, 5);
for (int i = 0; i < 20; i++) {
int finalI = i;
new Thread(()->{
contractFolder.putContract(new Contract("小区" + finalI, Thread.currentThread().getName()+ finalI));
}, "恒大").start();
}
}
}
总结
嗯嗯嗯, 不多说,线程池模式。。。。。。