java list 替换 多线程_Java多线程处理List数据
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
public class Test_4 {
/**
* 多線程處理list
*
* @param data 數(shù)據(jù)list
* @param threadNum 線程數(shù)
*/
public synchronized void handleList(List 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 data;
private int start;
private int end;
public HandleThread(String threadName, List data, int start, int end) {
this.threadName = threadName;
this.data = data;
this.start = start;
this.end = end;
}
public void run() {
//這里處理數(shù)據(jù)
List subList = data.subList(start, end);
for(int a=0; a
System.out.println(threadName+"處理了 "+subList.get(a)+" !");
//System.out.println(threadName+"處理了"+subList.size()+"條!");
}
}
}
public static void main(String[] args) {
Test_4 test = new Test_4();
// 準(zhǔn)備數(shù)據(jù)
List data = new ArrayList();
for (int i = 0; i < 1517; i++) {
data.add("item" + i);
}
test.handleList(data, 6);
System.out.println(ArrayUtils.toString(data));
}
}
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的java list 替换 多线程_Java多线程处理List数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java hashtable排序_jav
- 下一篇: java语言for模板_java版的模板