多线程设计模式之八 workerthread模式 线程池模式

本文通过一个实例展示了如何使用Java实现线程池和生产者消费者模式。创建了一个合同队列,由多个施工队(线程)消费,确保每个合同仅被一个施工队处理,同时保证了线程安全。通过这种方式,优化了并发性能并避免了资源争抢。
摘要由CSDN通过智能技术生成

思考

这不就是线程池嘛,
组件: 任务队列、线程队列

呃任务队列需要被线程队列里面的线程消费,所以需要用到生产者和消费者模式

案例

比如有多个开发商买了土体合同,每个土地合同都需要施工队去开发建筑,
备注 : 一个合同只能有一个施工队去承保开发,且施工队同一时间只能承保一个合同直到结束

图示

在这里插入图片描述

合同

/**
 * 合同
 * @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();
        }
    }
}

总结

嗯嗯嗯, 不多说,线程池模式。。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值