今天就跟大家聊聊有關實現(xiàn)Java多線程處理List數(shù)據(jù)的方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
十多年的漢源網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整漢源建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“漢源網(wǎng)站設計”,“漢源網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
解決問題:如何讓n個線程順序遍歷含有n個元素的List集合
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; public class Test_4 { /** * 多線程處理list * * @param data 數(shù)據(jù)list * @param threadNum 線程數(shù) */ 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(); // 準備數(shù)據(jù) 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多線程并發(fā)讀取讀取現(xiàn)有的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集合
場景:大數(shù)據(jù)List集合,需要對List集合中的數(shù)據(jù)同標準庫中數(shù)據(jù)進行對比,生成新增,更新,取消數(shù)據(jù)
解決方案:
List集合分段,
動態(tài)創(chuàng)建線程池newFixedThreadPool
將對比操作在多線程中實現(xiàn)
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條數(shù)據(jù)開啟一條線程 int threadSize = 500; // 總數(shù)據(jù)條數(shù) int dataSize = list.size(); // 線程數(shù) int threadNum = dataSize / threadSize + 1; // 定義標記,過濾threadNum為整數(shù) boolean special = dataSize % threadSize == 0; // 創(chuàng)建一個線程池 ExecutorService exec = Executors.newFixedThreadPool(threadNum); // 定義一個任務集合 List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); Callable<Integer> task = null; List<String> cutList = null; // 確定每條線程的數(shù)據(jù) 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列表存在順序?qū)年P系 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("線程任務執(zhí)行結(jié)束"); System.err.println("執(zhí)行任務消耗了 :" + (System.currentTimeMillis() - start) + "毫秒"); }
看完上述內(nèi)容,你們對實現(xiàn)Java多線程處理List數(shù)據(jù)的方法有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁題目:實現(xiàn)Java多線程處理List數(shù)據(jù)的方法
網(wǎng)頁URL:http://bm7419.com/article16/gijjgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站導航、網(wǎng)站收錄、品牌網(wǎng)站設計、微信小程序、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)