需求介绍
有近2w 商品,获取到全部商品详细信息,
存在2 个接口,
一个是分页获取商品列表
一个是根据列表页包含的商品编号,获取商品系那个IQ哪个
采用多线程线程池方式获取商品详情
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
/**
* 第一次发送请求获取商品列表
* 再遍历商品列表,获取到商品编号
* 第二次根据商品编号发送,获取到商品详情
* @Author buyingfei
*/
public class SingleThreadGetGoods {
public static void main(String[] args) throws InterruptedException, ExecutionException {
getSingleGoodsData();
}
public static void getSingleGoodsData() throws InterruptedException, ExecutionException {
int count = 50000;// 模拟有10w 条数据要去请求
int pageSize = 100;
for (int i =0;i<count;i+= pageSize){
// 第一次发送请求获取商品列表
GoodsList goodsList = new GoodsList(i);
List<Future<String>> tasklist = null;
tasklist = new ArrayList<>();
// 再遍历商品列表
for (Goods goods :goodsList.getGoodsList(pageSize)) {
// 第二次根据商品编号发送,获取到商品详情
FutureTask<String> futureTask = new FutureTask<>(new Goods(goods.getGoodsNum()));
ThreadPoolManager.getInstance().executor(futureTask);
tasklist.add(futureTask);
}
// 获取所有结果
for (Future<String> futureTask : tasklist){
System.out.println(futureTask.get());
}
}
}
}
/**
* 返回一个字符串,代表拿到结果
*/
class Goods implements Callable<String>{
private String GoodsNum;
public Goods(String goodsNum) {
GoodsNum = goodsNum;
}
// 模拟获取商品详情,花费时间2s
public String getGoodsNum() throws InterruptedException {
Thread.sleep(2);
// System.out.println("getGoodsNum");
return GoodsNum;
}
public void setGoodsNum(String goodsNum) {
GoodsNum = goodsNum;
}
@Override
public String call() throws Exception {
// System.out.println(Thread.currentThread().getName()+"《1》" + this.getGoodsNum());
return "result "+Thread.currentThread().getName()+"《》" + this.getGoodsNum();
}
}
class GoodsList{
private int curPage = 0;
private List<Goods> goodsList;
public GoodsList(Integer curPage) {
this.curPage = curPage;
goodsList= new ArrayList<>();
}
// 模拟请求接口,获取商品 列表,时间1s
public List<Goods> getGoodsList(int pageSize) throws InterruptedException {
for (int i = 0; i < pageSize; i++) {
goodsList.add(new Goods(UUID.randomUUID().toString()));
}
Thread.sleep(1000);
System.out.println("current page "+ this.curPage);
return goodsList;
}
}
/**
* 线程池
*/
class ThreadPoolManager {
// cpu 核心数
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
// 核心线程数
private static final int CORE_POOL_SIZE = CPU_COUNT +1;
// 最大核心线程数
private static final int MAX_CORE_POOL_SIZE = CPU_COUNT * 2 +1;
// 非核心线程超时时间
private static final int KEEP_ALIVE = 1;
//最大阻塞对垒长度
private static final int MAX_BLOCK_QUEUE_LENGTH = 1024;
//采用单例模式
private static ThreadPoolManager threadPoolManager;
private ThreadPoolManager(){}
// 可重入锁实现单例
private static ReentrantLock reentrantLock = new ReentrantLock();
// 生成类单例对象
public static ThreadPoolManager getInstance(){
reentrantLock.lock();
try
{
if (threadPoolManager == null) {
threadPoolManager = new ThreadPoolManager();
}
}finally {
reentrantLock.unlock();
}
return threadPoolManager;
}
// 线程池对象
private ThreadPoolExecutor executor;
// 将线程放入池子
public void executor(Runnable runnable){
if(executor == null){
System.out.println("最大线程数"+MAX_CORE_POOL_SIZE);
executor = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAX_CORE_POOL_SIZE,KEEP_ALIVE, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(MAX_BLOCK_QUEUE_LENGTH),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
}
executor.execute(runnable);
}
// 取消队列
public void cancel(Runnable runnable){
if(runnable != null){
executor.getQueue().remove(runnable);
}
}
}