详解Java多线程处理List数据
程序员文章站
2024-02-25 16:29:51
实例1:
解决问题:如何让n个线程顺序遍历含有n个元素的list集合
import java.util.arraylist;
import java.util...
实例1:
解决问题:如何让n个线程顺序遍历含有n个元素的list集合
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) { 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; private int start; private int end; public handlethread(string threadname, list<string> data, int start, int end) { this.threadname = threadname; this.data = data; this.start = start; this.end = end; } public void run() { list<string> sublist = data.sublist(start, end)/*.add("^&*")*/; system.out.println(threadname+"处理了"+sublist.size()+"条!"); } } public static void main(string[] args) { test_4 test = new test_4(); // 准备数据 list<string> data = new arraylist<string>(); for (int i = 0; i < 6666; i++) { data.add("item" + i); } test.handlelist(data, 5); system.out.println(arrayutils.tostring(data)); } }
实例2:
list多线程并发读取读取现有的list对象
//测试读取list的线程类,大概34秒 package com.thread.list; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; public class main { public static void main(string[] args) { list<string> list = new arraylist<string>(); map<long,integer> map = new hashmap<long,integer>(); for(int i = 0;i<1000;i++){ list.add(""+i); } int pcount = runtime.getruntime().availableprocessors(); long start = system.currenttimemillis(); for(int i=0;i<pcount;i++){ thread t = new mythread1(list,map); map.put(t.getid(),integer.valueof(i)); t.start(); try { t.join(); } catch (interruptedexception e) { e.printstacktrace(); } // system.out.println(list.get(i)); } system.out.println("----"+(system.currenttimemillis() - start)); } } //线程类 package com.thread.list; import java.util.list; import java.util.map; public class mythread1 extends thread { private list<string> list; private map<long,integer> map; public mythread1(list<string> list,map<long,integer> map){ this.list = list; this.map = map; } @override public void run() { int pcount = runtime.getruntime().availableprocessors(); int i = map.get(thread.currentthread().getid()); for(;i<list.size();i+=pcount){ system.out.println(list.get(i)); } } }
实例3:
多线程分段处理list集合
场景:大数据list集合,需要对list集合中的数据同标准库中数据进行对比,生成新增,更新,取消数据
解决方案:
- list集合分段,
- 动态创建线程池newfixedthreadpool
- 将对比操作在多线程中实现
public static void main(string[] args) throws exception { // 开始时间 long start = system.currenttimemillis(); list<string> list = new arraylist<string>(); for (int i = 1; i <= 3000; i++) { list.add(i + ""); } // 每500条数据开启一条线程 int threadsize = 500; // 总数据条数 int datasize = list.size(); // 线程数 int threadnum = datasize / threadsize + 1; // 定义标记,过滤threadnum为整数 boolean special = datasize % threadsize == 0; // 创建一个线程池 executorservice exec = executors.newfixedthreadpool(threadnum); // 定义一个任务集合 list<callable<integer>> tasks = new arraylist<callable<integer>>(); callable<integer> task = null; list<string> cutlist = null; // 确定每条线程的数据 for (int i = 0; i < threadnum; i++) { if (i == threadnum - 1) { if (special) { break; } cutlist = list.sublist(threadsize * i, datasize); } else { cutlist = list.sublist(threadsize * i, threadsize * (i + 1)); } // system.out.println("第" + (i + 1) + "组:" + cutlist.tostring()); final list<string> liststr = cutlist; task = new callable<integer>() { @override public integer call() throws exception { system.out.println(thread.currentthread().getname() + "线程:" + liststr); return 1; } }; // 这里提交的任务容器列表和返回的future列表存在顺序对应的关系 tasks.add(task); } list<future<integer>> results = exec.invokeall(tasks); for (future<integer> future : results) { system.out.println(future.get()); } // 关闭线程池 exec.shutdown(); system.out.println("线程任务执行结束"); system.err.println("执行任务消耗了 :" + (system.currenttimemillis() - start) + "毫秒"); }
以上所述是小编给大家介绍的java多线程处理list数据详解整合,希望对大家有所帮助