并发编程的艺术 11章代码 生产者-消费者

并发编程的艺术 11章代码 生产者-消费者

执行结果
采集excelID--0
采集excelID--1
处理excelID:0---:内容0
采集excelID--2
处理excelID:1---:内容1
采集excelID--3
处理excelID:2---:内容2
采集excelID--4
处理excelID:3---:内容3
采集excelID--5
处理excelID:4---:内容4
采集excelID--6
处理excelID:5---:内容5
FastExcel
package com.cartugly.excel;

import java.util.concurrent.*;

public class FastExcel {
    //线程池
    private final ThreadPoolExecutor threadsPool;

    //队列
    private final BlockingQueue<ExcelEntity> excelQueue;

    public FastExcel() {
        //队列
        excelQueue = new LinkedBlockingQueue<>();

        //核数*2
        int corePoolSize = Runtime.getRuntime().availableProcessors() * 2;

        //初始化线程池
        threadsPool = new ThreadPoolExecutor(corePoolSize, corePoolSize, 100,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(2000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

    }

    public void offerQueue() {
        //入队列
        ExcelOfferQueue excelOfferQueue = new ExcelOfferQueue(excelQueue);
        new Thread(excelOfferQueue).start();
    }

    public void consumer() {
        try {
            while (true) {
                // 两秒内取不到就退出
                ExcelEntity excelEntity = excelQueue.poll(2, TimeUnit.SECONDS);
                if (excelEntity == null) {
                    break;
                }
                Thread job = new Thread(new ExcelJob(excelEntity));
                threadsPool.submit(job);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FastExcel fastExcel = new FastExcel();
        //采集队列
        fastExcel.offerQueue();
        //出队列消费
        fastExcel.consumer();
    }
}
ExcelService
package com.cartugly.excel;

import java.util.ArrayList;
import java.util.List;

public class ExcelService {
    public ExcelService() {
    }

    /**
     * 查询出所有的数据
     * @return
     */
    public List<ExcelEntity> queryAllExcels() {
        ArrayList<ExcelEntity> excelEntities = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ExcelEntity excelEntity = new ExcelEntity();
            excelEntity.setId(i + "");
            excelEntity.setContent("内容" + i);
            excelEntities.add(excelEntity);
        }
        return excelEntities;
    }

}
ExcelEntity
package com.cartugly.excel;

public class ExcelEntity {
    private String id;
    private String content;

    public ExcelEntity() {
    }

    public String getId() {
        return id;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
ExcelOfferQueue
package com.cartugly.excel;

import java.util.List;
import java.util.concurrent.BlockingQueue;

public class ExcelOfferQueue implements Runnable {

    private ExcelService excelService;

    private BlockingQueue<ExcelEntity> excelQueue;

    public ExcelOfferQueue(BlockingQueue excelQueue) {
        excelService = new ExcelService();
        this.excelQueue = excelQueue;
    }

    @Override
    public void run() {
        List<ExcelEntity> excelEntities = excelService.queryAllExcels();
        if (excelEntities == null) {
            return;
        }
        if (excelEntities.size() > 0) {
            for (ExcelEntity excelEntity : excelEntities) {
                try {
                    Thread.sleep(200);
                    System.out.println("采集excelID--" + excelEntity.getId());
                    excelQueue.offer(excelEntity);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
ExcelJob
package com.cartugly.excel;

public class ExcelJob implements Runnable {

    private ExcelEntity excelEntity;

    public ExcelJob(ExcelEntity excelEntity) {
        this.excelEntity = excelEntity;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("处理excelID:" + excelEntity.getId() + "---:" + excelEntity.getContent());
    }
}
posted @ 2021-04-25 15:38  z_先生  阅读(57)  评论(0编辑  收藏  举报