欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA多線程處理for循環(huán)數(shù)據(jù)詳細講解

 更新時間:2023年07月28日 11:35:59   作者:二拾三  
這篇文章主要給大家介紹了關于JAVA多線程處理for循環(huán)數(shù)據(jù)的相關資料,我們在代碼中經(jīng)常需要使用for循環(huán)這個操作來達到目的,而當for循環(huán)的次數(shù)過多時我們會發(fā)現(xiàn)執(zhí)行效率會變的很低,整體耗時非常多,需要的朋友可以參考下

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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 批量將現(xiàn)有Jar包上傳到Maven私服

    批量將現(xiàn)有Jar包上傳到Maven私服

    今天小編就為大家分享一篇關于批量將現(xiàn)有Jar包上傳到Maven私服,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • java關于并發(fā)模型中的兩種鎖知識點詳解

    java關于并發(fā)模型中的兩種鎖知識點詳解

    在本篇文章了小編給大家整理的是一篇關于java關于并發(fā)模型中的兩種鎖知識點詳解內(nèi)容,有興趣的朋友們可以學習下。
    2021-04-04
  • Java中的IO讀寫原理詳解

    Java中的IO讀寫原理詳解

    這篇文章主要介紹了Java中的IO讀寫原理,IO是指輸入和輸出操作的技術,它提供了一組用于讀取和寫入數(shù)據(jù)的類,以及用于處理字符和字節(jié)數(shù)據(jù)的接口,這些類和接口可以用于讀取和寫入文件、網(wǎng)絡流、內(nèi)存緩沖區(qū)等各種數(shù)據(jù)源和目標,需要的朋友可以參考下
    2023-08-08
  • Elasticsearch查詢之Match Query示例詳解

    Elasticsearch查詢之Match Query示例詳解

    這篇文章主要為大家介紹了Elasticsearch查詢之Match查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • SpringBoot集成antlr實現(xiàn)詞法和語法分析

    SpringBoot集成antlr實現(xiàn)詞法和語法分析

    Antlr4 是一款強大的語法生成器工具,可用于讀取、處理、執(zhí)行和翻譯結構化的文本或二進制文件,基本上是當前 Java 語言中使用最為廣泛的語法生成器工具,本文給大家介紹了SpringBoot集成antlr實現(xiàn)詞法和語法分析,需要的朋友可以參考下
    2024-06-06
  • Spring?循環(huán)依賴之AOP實現(xiàn)詳情

    Spring?循環(huán)依賴之AOP實現(xiàn)詳情

    這篇文章主要介紹了Spring?循環(huán)依賴之AOP實現(xiàn)詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的盆友可以參考一下
    2022-07-07
  • Java使用synchronized修飾方法來同步線程的實例演示

    Java使用synchronized修飾方法來同步線程的實例演示

    synchronized下的方法控制多線程程序中的線程同步非常方便,這里就來看一下Java使用synchronized修飾方法來同步線程的實例演示,需要的朋友可以參考下
    2016-06-06
  • 阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式

    阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式

    本文主要介紹了阿里nacos+springboot+dubbo2.7.3統(tǒng)一處理異常的兩種方式,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java生成唯一ID的三種方法總結

    Java生成唯一ID的三種方法總結

    單機環(huán)境下,可以使用AtomicLong來生成唯一ID;而在需要非純數(shù)字形式的場景中,可以通過UUID結合哈希函數(shù)如MD5或SHA-1轉(zhuǎn)換成數(shù)字,但需注意哈希碰撞的低概率風險;對于分布式系統(tǒng),模擬Snowflake算法是一種復雜但有效的方法,每種方法都有其適用場景和潛在問題
    2024-09-09
  • Java中的final關鍵字深入理解

    Java中的final關鍵字深入理解

    這篇文章主要介紹了Java中的final關鍵字深入理解的相關資料,需要的朋友可以參考下
    2017-02-02

最新評論