java多线程 读取list--截取的方式
1.需要一个线程类
package com.tc.testautomation.control.conf; import org.apache.commons.lang.ArrayUtils; import java.util.ArrayList; import java.util.List; public class HandleThread extends Thread{ private String threadName; private List<String> data; public HandleThread(String threadName, List<String> data) { this.threadName = threadName; this.data = data; } public void run() { // TODO 这里处理数据 // data.subList(start, end).add("^&*"); System.out.println(threadName); System.out.println(data); } }
2.需要一个调用类
package com.tc.testautomation.control.conf; import io.swagger.models.auth.In; import java.util.ArrayList; import java.util.List; public class Test_4 { /** * 多线程处理list * * @param data 数据list * @param threadNum 线程数 */ public synchronized void handleList(List<String> data, int threadNum) { int len=data.size(); // System.out.println((double)len/threadNum); int interval= (int)Math.ceil((double)len/threadNum); System.out.println("interval:"+interval); for (int i = 0; i < threadNum; i++) { // System.out.println("AA:"+i*interval); // System.out.println("BB:"+(interval*(i+1)-1)); int end=interval*(i+1); HandleThread thread = new HandleThread("线程[" + (i + 1) + "] ", data.subList(i*interval,end > len? len:end)); thread.start(); } } public static void main(String[] args) { Test_4 test = new Test_4(); // 准备数据 List<String> data = new ArrayList<String>(); for (int i = 0; i < 50; i++) { data.add("item" + i); } test.handleList(data, 6); // System.out.println(ArrayUtils.toString(data)); } }
也可以参考这种方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; public class Test_4 { /** * 多线程处理list * * @param data 数据list * @param threadNum 线程数 */ public synchronized void handleList(List< string > data, int threadNum) {</ string > int length = data.size(); int tl = length % threadNum == 0 ? length / threadNum : (length / threadNum + 1); for ( int i = 0; i < threadNum; i++) { int end = (i + 1) * tl; HandleThread thread = new HandleThread( "线程[" + (i + 1) + "] " , data, i * tl, end > length ? length : end); thread.start(); } } class HandleThread extends Thread { private String threadName; private List< string > data;</ string > private int start; private int end; public HandleThread(String threadName, List< string > data, int start, int end) {</ string > this .threadName = threadName; this .data = data; this .start = start; this .end = end; } public void run() { // TODO 这里处理数据 data.subList(start, end).add( "^&*" ); System. out .println(threadName) } } public static void main(String[] args) { Test_4 test = new Test_4(); // 准备数据 List< string > data = new ArrayList< string >();</ string ></ string > for ( int i = 0; i < 5000; i++) { data.add( "item" + i); } test.handleList(data, 5); System. out .println(ArrayUtils.toString(data)); } } 好文要顶 |
分类:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)