最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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 + "個(gè)線程");
        }
        System.out.println("完成");
    }

執(zhí)行結(jié)果:

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)用線程計(jì)數(shù)器-1
                countDownLatch.countDown();
            });
            System.out.println("第" + i + "個(gè)線程");
        }
        //喚醒主線程
        countDownLatch.await();
        System.out.println("完成");
    }

執(zhí)行結(jié)果:

3.對for循環(huán)內(nèi)數(shù)據(jù)啟用多線程執(zhí)行,主線程在所有子線程執(zhí)行完成之后執(zhí)行

 public static void main(String[] args) throws InterruptedException {
        // 線程個(gè)數(shù)
        int N = 10;
        // 實(shí)例化一個(gè)倒計(jì)數(shù)器,N指定計(jì)數(shù)個(gè)數(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() + "休眠結(jié)束");
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                    	// 計(jì)數(shù)減一
                        countDownLatch.countDown(); 
                    }
                }
            }).start();
        }
        // 阻塞,等待當(dāng)計(jì)數(shù)減到0時(shí),執(zhí)行后面的代碼
        countDownLatch.await();
        System.out.println("結(jié)束");
    }

執(zhí)行結(jié)果:

4. JAVA多線程10個(gè)線程處理1000個(gè)數(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();
        // 查看結(jié)果
        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í)行結(jié)果:

總結(jié)

到此這篇關(guān)于JAVA多線程處理for循環(huán)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)JAVA處理for循環(huán)數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程

    2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程

    這篇文章主要介紹了2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Spring使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對象

    Spring使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對象

    這篇文章主要為大家詳細(xì)介紹了Spring如何使用Jackson實(shí)現(xiàn)轉(zhuǎn)換XML與Java對象,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • Java實(shí)現(xiàn)冒泡排序示例介紹

    Java實(shí)現(xiàn)冒泡排序示例介紹

    冒泡排序是一種簡單的排序算法,通過不斷比較相鄰兩個(gè)元素的大小,將較大的元素向后移動,最終將最大的元素放到了數(shù)組的末尾。Java中的實(shí)現(xiàn)方式是通過嵌套兩層循環(huán),外層循環(huán)控制比較的次數(shù),內(nèi)層循環(huán)控制每次比較時(shí)相鄰元素的比較和交換
    2023-04-04
  • 詳解java開發(fā)webservice的幾種方式

    詳解java開發(fā)webservice的幾種方式

    webservice的應(yīng)用已經(jīng)越來越廣泛了,下面介紹幾種在Java體系中開發(fā)webservice的方式,有興趣的可以了解一下。
    2016-11-11
  • 簡單談?wù)凧ava垃圾回收

    簡單談?wù)凧ava垃圾回收

    本文是看了James Gosling的<<Java程序設(shè)計(jì)語言>>后結(jié)合自己的一些項(xiàng)目經(jīng)驗(yàn),簡單總結(jié)下關(guān)于java的垃圾回收問題的看法,有需要的小伙伴可以參考下
    2016-05-05
  • Spring實(shí)現(xiàn)處理跨域請求代碼詳解

    Spring實(shí)現(xiàn)處理跨域請求代碼詳解

    這篇文章主要介紹了Spring實(shí)現(xiàn)處理跨域請求代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • 教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答

    教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答

    這篇文章主要介紹了教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問一答,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java微信公眾平臺開發(fā)(11) 微信三大平臺的關(guān)聯(lián)

    Java微信公眾平臺開發(fā)(11) 微信三大平臺的關(guān)聯(lián)

    這篇文章主要介紹了Java微信公眾平臺開發(fā)第十一步,微信開發(fā)中微信公眾平臺、開放平臺和商戶平臺的關(guān)聯(lián),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Java 使用Filter實(shí)現(xiàn)用戶自動登陸

    Java 使用Filter實(shí)現(xiàn)用戶自動登陸

    這篇文章主要介紹了Java 使用Filter實(shí)現(xiàn)用戶自動登陸的方法,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-05-05
  • 一文詳解springboot中的熱啟動配置方案

    一文詳解springboot中的熱啟動配置方案

    SpringBoot的熱啟動主要通過 **`spring-boot-devtools`** 模塊實(shí)現(xiàn),能在代碼修改后自動重啟應(yīng)用,大幅提升開發(fā)效率,下面小編就來和大家講講詳細(xì)配置和使用指南
    2025-07-07

最新評論

淮安市| 大渡口区| 大石桥市| 台湾省| 澜沧| 青川县| 青岛市| 涞水县| 贵定县| 乐清市| 平定县| 乐昌市| 台山市| 宁都县| 绥江县| 宽城| 宜兰市| 湖口县| 和平县| 青河县| 娱乐| 甘肃省| 芜湖市| 运城市| 庆阳市| 始兴县| 新兴县| 迭部县| 诸暨市| 泊头市| 清苑县| 乐平市| 马关县| 兴仁县| 沿河| 荔浦县| 瑞丽市| 昂仁县| 威信县| 闻喜县| 通州市|