package com.example.demo.thread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
*@author liuxingying
*@description
*@since 2022/6/8
*/
@Service
public class PushProcessServiceImpl{
private final static Logger logger = LoggerFactory.getLogger(PushProcessServiceImpl.class);
//每个线程每次查询的条数
private static final Integer LIMIT = 5000;
//核心线程数:设置为操作系统CPU数乘以2
//cpu 密集型 :corePoolSize = cpu核数+1;
//IO 密集型:corePoolSize = CPU核数 * 2;
private static final Integer CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
//最大线程数:设置为和核心线程数相同
private static final Integer MAXIMUM_POOl_SIZE = CORE_POOL_SIZE;
//创建线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOl_SIZE * 2, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(Integer.MAX_VALUE));
public void pushData() throws ExecutionException, InterruptedException {
//计数器,需要保证线程安全
int count = 0;
//未推送数据总数
Integer total = 100000;
logger.info("未推送数据条数:{}", total);
int threadCount = LIMIT;
//计算需要多少轮
int num = total % threadCount == 0 ? total / threadCount : total / threadCount + 1;
logger.info("要经过的轮数:{}, 每轮发送 {}条", num, threadCount);
//统计总共推送成功的数据条数
int totalSuccessCount = 0;
for (int i = 0; i < num; i++) {
//接收线程返回结果
List<Future<Integer>> futureList = new ArrayList<>(32);
int start = count * LIMIT;
count++;
//提交线程,用数据起始位置标识线程
Future<Integer> future = pool.submit(new PushDataTask(start, LIMIT, i));
//先不取值,防止阻塞,放进集合
futureList.add(future);
}
//统计本轮推送成功数据
for (Future<Integer> f : futureList) {
totalSuccessCount = totalSuccessCount + f.get();
}
//关闭线程池
// pool.shutdown();
//更新推送标志
// pushProcessMapper.updateAllState(1);
logger.info("推送数据完成,需推送数据:{},推送成功:{}", total, totalSuccessCount);
}
/**
* 推送数据线程类
*/
class PushDataTask implements Callable<Integer> {
int start;
int limit;
PushDataTask(int start, int limit, int threadNo) {
this.start = start;
this.limit = limit;
}
@Override
public Integer call() throws Exception {
//设置线程名字
Thread.currentThread().setName("Thread" + start);
int count = 0;
//推送的数据
List<Integer> pushProcessList = getId(start, limit);
if (CollectionUtils.isEmpty(pushProcessList)) {
return count;
}
logger.info("推送区间:[{},{}]数据", start, start + limit);
for (Integer process : pushProcessList) {
if (process>=0) { //推送成功
//更新推送标识
// pushProcessMapper.updateFlagById(process.getId(), 1);
count++;
} else { //推送失败
// pushProcessMapper.updateFlagById(process.getId(), 2);
}
}
logger.info("推送区间[{},{}]数据,推送成功{}条!", start, start + limit, count);
return count;
}
}
private List<Integer> getId(int start,int limit){
List<Integer> list = new LinkedList<>();
for (int i = 0; i < limit; i++) {
start = start + i ;
list.add(start);
}
return list;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
PushProcessServiceImpl p = new PushProcessServiceImpl();
p.pushData();
}
}
多线程跑数据练习
最新推荐文章于 2024-07-13 03:13:11 发布