JAVA多線程處理for循環(huán)數(shù)據(jù)詳細講解
1.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程與子線程無先后順序
public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 5; i++) { ThreadUtil.execAsync(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程" + Thread.currentThread().getName() + "執(zhí)行完"); }); System.out.println("第" + i + "個線程"); } System.out.println("完成"); }
執(zhí)行結果:
2.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程在所有子線程執(zhí)行完成之后執(zhí)行
public static void main(String[] args) throws InterruptedException { //初始化線程數(shù)量 CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(5); for (int i = 0; i < 5; i++) { ThreadUtil.execute(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程" + Thread.currentThread().getName() + "執(zhí)行完"); //調(diào)用線程計數(shù)器-1 countDownLatch.countDown(); }); System.out.println("第" + i + "個線程"); } //喚醒主線程 countDownLatch.await(); System.out.println("完成"); }
執(zhí)行結果:
3.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程在所有子線程執(zhí)行完成之后執(zhí)行
public static void main(String[] args) throws InterruptedException { // 線程個數(shù) int N = 10; // 實例化一個倒計數(shù)器,N指定計數(shù)個數(shù) CountDownLatch countDownLatch = new CountDownLatch(N); for (int i = 0; i < N; i++) { new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); System.out.println("子線程" + Thread.currentThread().getName() + "休眠結束"); } catch (Exception e) { e.printStackTrace(); } finally { // 計數(shù)減一 countDownLatch.countDown(); } } }).start(); } // 阻塞,等待當計數(shù)減到0時,執(zhí)行后面的代碼 countDownLatch.await(); System.out.println("結束"); }
執(zhí)行結果:
4. JAVA多線程10個線程處理1000個數(shù)據(jù)
public static void main(String[] args) throws Exception { List<Integer> idList = new ArrayList<>(); for (int i = 1; i <= 1000; i++) { idList.add(i); } int threadNum = 10; ExecutorService executorService = Executors.newFixedThreadPool(threadNum); CountDownLatch countDownLatch = new CountDownLatch(threadNum); int perSize = idList.size() / threadNum; // 定義接受數(shù)據(jù)集合 多線程情況下,使用線程安全集合 List<Integer> resultList = Collections.synchronizedList(new ArrayList()); for (int i = 0; i < threadNum; i++) { MultiThread thread = new MultiThread(); thread.setIdList(idList.subList(i * perSize, (i + 1) * perSize)); thread.setCountDownLatch(countDownLatch); thread.setResultList(resultList); executorService.submit(thread); } countDownLatch.await(); executorService.shutdown(); // 查看結果 System.out.println(resultList.size()); System.out.println(resultList.stream().sorted().collect(Collectors.toList())); } } class MultiThread extends Thread { private List<Integer> idList; private CountDownLatch countDownLatch; private List<Integer> result; public void setResultList(List<Integer> result) { this.result = result; } public void setIdList(List<Integer> idList) { this.idList = idList; } public void setCountDownLatch(CountDownLatch countDownLatch) { this.countDownLatch = countDownLatch; } @Override public void run() { try { // 數(shù)據(jù)處理 for (Integer integer : idList) { if (integer % 2 == 0) { result.add(integer); } } } catch (Exception e) { e.printStackTrace(); } finally { if (countDownLatch != null) { countDownLatch.countDown(); } } }
執(zhí)行結果:
總結
到此這篇關于JAVA多線程處理for循環(huán)數(shù)據(jù)的文章就介紹到這了,更多相關JAVA處理for循環(huán)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Elasticsearch查詢之Match Query示例詳解
這篇文章主要為大家介紹了Elasticsearch查詢之Match查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04SpringBoot集成antlr實現(xiàn)詞法和語法分析
Antlr4 是一款強大的語法生成器工具,可用于讀取、處理、執(zhí)行和翻譯結構化的文本或二進制文件,基本上是當前 Java 語言中使用最為廣泛的語法生成器工具,本文給大家介紹了SpringBoot集成antlr實現(xiàn)詞法和語法分析,需要的朋友可以參考下2024-06-06Spring?循環(huán)依賴之AOP實現(xiàn)詳情
這篇文章主要介紹了Spring?循環(huán)依賴之AOP實現(xiàn)詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的盆友可以參考一下2022-07-07Java使用synchronized修飾方法來同步線程的實例演示
synchronized下的方法控制多線程程序中的線程同步非常方便,這里就來看一下Java使用synchronized修飾方法來同步線程的實例演示,需要的朋友可以參考下2016-06-06阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式
本文主要介紹了阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03