自定义多线程分片处理
以下参数和方法都是测试用,仅供运行程序参考
主要是为了小型场景,进行多线程批量处理
package com.xie.minio;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
* @Description 多线程操作minio文件
* @Date 2022-07-21 10:51
* @Author xie
*/
public class MinioUtilThread {
private static Logger log = LoggerFactory.getLogger(MinioUtilThread.class);
/**
* minio 桶上传
*
* @param fileName 上传的 文件名
* @param fileUrl 文件路径
* @param minioPath minio 桶下的 文件夹名字
*/
public static void push(String fileName, String fileUrl, String minioPath) throws Exception {
//Thread.sleep(new Random().nextInt(1000));
Thread.sleep(100);
//log.info(fileUrl + "上传成功");
}
/**
* 根据 list数目 分配 任务
*
* @param list 文件夹下所有文件的全路径list
* @param minioPath 指定 minio 桶下的 文件夹名字
*/
public static void startPoll(List<String> list, String minioPath) {
long startTime = System.currentTimeMillis();
//-----------------------------多线程执行 测试 start-----------------------------------
int length = list.size();
//初始线程数
int num = 10;
//启动多线程
if (num > length) {
num = length;
}
int stepNum = length / num;
int remainderNum = length % num;
int end = 0;
Vector<Thread> threadVector = new Vector<>();
for (int i = 0; i < num; i++) {
int start = end;
end = start + stepNum;
if (i == (num - 1)) {
end = length;
}
// 把余数没地方分配的都先给分配下去
else if (i < remainderNum) {
end = end + 1;
}
HandleThread thread = new HandleThread("线程[" + (i + 1) + "] ", list, start, end, minioPath);
threadVector.add(thread);
thread.start();
}
//等待子线程 全部执行完毕 再执行
for (Thread thread : threadVector) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//-----------------------------多线程执行 测试 end-----------------------------------
//-----------------------------单线程执行 测试 start-----------------------------------
//list.forEach(fileName -> {
// try {
// push(fileName, fileName, minioPath);
// } catch (Exception e) {
// e.printStackTrace();
// }
//});
//-----------------------------单线程执行 测试 end-----------------------------------
long endTime = System.currentTimeMillis();
System.out.println("总程序运行时间:" + (endTime - startTime) + "ms");
System.out.println("子线程执行结束");
}
/**
* 任务处理类
*/
public static class HandleThread extends Thread {
private String threadName;
private List<String> list;
private int startIndex;
private int endIndex;
private String minioPath;
public HandleThread(String threadName, List<String> list, int startIndex, int endIndex, String minioPath) {
this.threadName = threadName;
this.list = list;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.minioPath = minioPath;
}
@Override
public void run() {
long startTime = System.currentTimeMillis();
List<String> subList = list.subList(startIndex, endIndex);
for (int i = startIndex; i < endIndex; i++) {
try {
push(list.get(i), list.get(i), minioPath);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(threadName + "处理了" + subList.size() + "条!startIndex:" + startIndex + "|endIndex:" + endIndex);
long endTime = System.currentTimeMillis();
System.out.println(threadName + "程序运行时间:" + (endTime - startTime) + "ms");
}
}
public static void main(String[] args) {
ArrayList<String> list = Lists.newArrayList();
for (int i = 1; i <= 200; i++) {
String prefix = "http://minio/test/";
list.add(prefix + i);
}
startPoll(list, "test");
}
}
欢迎一起来学习和指导,谢谢关注!
本文来自博客园,作者:xiexie0812,转载请注明原文链接:https://www.cnblogs.com/mask-xiexie/p/16502048.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 我与微信审核的“相爱相杀”看个人小程序副业
· DeepSeek “源神”启动!「GitHub 热点速览」
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库