import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.*; /** * Created by 45033535 on 2021/5/19. */ public class MultithreadingListSum { private int sum = 0; /** * @Description: 多個線程求和 * @Version: 1.0 */ public static class ShareData implements Callable<Integer> { private volatile int startIndex ; private volatile int endIndex ; private volatile int sum ; private volatile List<JavaBean> list; private volatile int remaider; //(先計算出餘數) private volatile int num; //然後是商 private volatile int offset = 0;//偏移量 public ShareData(int startIndex,int endIndex,List<JavaBean> list){ this.startIndex = startIndex; this.endIndex = endIndex; this.list = list; } public ShareData(){} /** * 累加和 * @return * @throws Exception */ @Override public Integer call() throws Exception { System.out.println("子線程名稱:"+Thread.currentThread().getName()+"List 正在進行計算startIndex=="+ startIndex+"到 endIndex="+endIndex); List<JavaBean> arr = list.subList(startIndex,endIndex); System.out.println("arr"+arr); for(JavaBean bean : arr){ sum +=bean.getAge(); System.out.println("當前線程名稱:"+Thread.currentThread().getName()+"的和爲:"+sum); } //爲了空格方便好看 System.out.println(); System.out.println(); System.out.println(); return sum; } /** * 多線程求和startIndex~endIndex * @param list * @param threadNumber * @return */ public Integer getDataSum(List<JavaBean> list ,int threadNumber){ List<Future<Integer>> taskList = new CopyOnWriteArrayList<>(); ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);//固定指定線程數量 try { remaider = list.size() % threadNumber; //(先計算出餘數) num = list.size() / threadNumber; //然後是商 offset = 0;//偏移量 for (int i = 0; i < threadNumber; i++) { if (remaider > 0) { startIndex = i * num + offset; endIndex = (i + 1) * num + offset + 1; remaider--; offset++; System.out.println("第"+i+",組索引:"+startIndex+","+endIndex); Future future = executorService.submit(new MultithreadingListSum.ShareData(startIndex,endIndex,list)); //把每個子線程計算結果放到集合中 taskList.add(future); } else { startIndex = i * num + offset; endIndex = (i + 1) * num + offset; System.out.println("第"+i+",組索引:"+startIndex+","+endIndex); Future future = executorService.submit(new MultithreadingListSum.ShareData(startIndex,endIndex,list)); //把每個子線程計算結果放到集合中 taskList.add(future); } } }catch (Exception e){ e.printStackTrace(); }finally { executorService.shutdown(); } //把各個集合中的值遍歷求和 sum = getFutureSum(taskList); return sum; } /** * 遍歷結果集,取數 * @param taskList * @return */ public Integer getFutureSum(List<Future<Integer>> taskList) { int sumNumber = 0; if (taskList.size() == 0 ) return 0; try { for (Future<Integer> task: taskList){ sumNumber += task.get(); } } catch (Exception e) { e.printStackTrace(); } return sumNumber; } } public static void main(String[] args) { List<JavaBean> list = new CopyOnWriteArrayList<JavaBean>(); list = Arrays.asList( new JavaBean(1l,"","",18,"[email protected]"), new JavaBean(1l,"","",19,"[email protected]"), new JavaBean(1l,"","",20,"[email protected]"), new JavaBean(1l,"","",21,"[email protected]"), new JavaBean(1l,"","",22,"[email protected]"), new JavaBean(1l,"","",23,"[email protected]"), new JavaBean(1l,"","",24,"[email protected]") ); // TODO Auto-generated method stub MultithreadingListSum.ShareData shareData = new MultithreadingListSum.ShareData(); int sum = shareData.getDataSum(list,2); System.out.println("最終運行的結果:"+sum); } }
多線程處理list 求和
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.