AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

掌握這五種多線程方法,提高Java代碼效率

來源: 責(zé)編: 時間:2023-10-17 09:38:52 283觀看
導(dǎo)讀如果您的應(yīng)用程序與那些能夠同時處理多個任務(wù)的應(yīng)用程序相比表現(xiàn)不佳,很可能是因為它是單線程的。解決這個問題的方法之一是采用多線程技術(shù)。以下是一些可以考慮的方法:線程(Thread)并行流(Parallel Streams)ExecutorServic

如果您的應(yīng)用程序與那些能夠同時處理多個任務(wù)的應(yīng)用程序相比表現(xiàn)不佳,很可能是因為它是單線程的。解決這個問題的方法之一是采用多線程技術(shù)。ovN28資訊網(wǎng)——每日最新資訊28at.com

以下是一些可以考慮的方法:ovN28資訊網(wǎng)——每日最新資訊28at.com

  • 線程(Thread)
  • 并行流(Parallel Streams)
  • ExecutorService
  • ForkJoinPool
  • CompletableFuture

適當(dāng)?shù)厥褂眠@些方法,可以徹底改變您的應(yīng)用程序,并推動您的職業(yè)發(fā)展。下面我們來看看如何將您的應(yīng)用程序轉(zhuǎn)變?yōu)楦咝У亩嗑€程應(yīng)用。ovN28資訊網(wǎng)——每日最新資訊28at.com

ovN28資訊網(wǎng)——每日最新資訊28at.com

1. 線程(Thread)

第一種選擇是使用線程(Thread)類。通過這種方式,您可以直接控制線程的創(chuàng)建和管理。以下是一個示例:ovN28資訊網(wǎng)——每日最新資訊28at.com

CustomTask 每隔50毫秒從0數(shù)到 count - 1。ovN28資訊網(wǎng)——每日最新資訊28at.com

public class CustomTask implements Runnable {    private final String name;    private final int count;    CustomTask(String name, int count) {        this.name = name;        this.count = count;    }    @Override    public void run() {        for (int i = 0; i < count; i++) {            System.out.println(name + "-" + i + " from " +                    Thread.currentThread().getName());            try {                Thread.sleep(50);            } catch (InterruptedException e) {                throw new RuntimeException(e);            }        }    }}

a、b 和 c 是該類的三個實例。ovN28資訊網(wǎng)——每日最新資訊28at.com

Thread a = new Thread(new CustomTask("a", 5));Thread b = new Thread(new CustomTask("b", 10));Thread c = new Thread(new CustomTask("c", 5));

請注意,b 預(yù)計計數(shù)的次數(shù)是其他實例的兩倍。您希望在 a 和 c 順序運行的同時運行 b。ovN28資訊網(wǎng)——每日最新資訊28at.com

ovN28資訊網(wǎng)——每日最新資訊28at.com

您可以非常容易地實現(xiàn)這種行為。ovN28資訊網(wǎng)——每日最新資訊28at.com

// 首先啟動 a 和 b。a.start();b.start();//  a 完成后開始 c。a.join();c.start();

以下是結(jié)果:ovN28資訊網(wǎng)——每日最新資訊28at.com

a-0 from Thread-0b-0 from Thread-1b-1 from Thread-1a-1 from Thread-0b-2 from Thread-1a-2 from Thread-0b-3 from Thread-1a-3 from Thread-0b-4 from Thread-1a-4 from Thread-0b-5 from Thread-1c-0 from Thread-2b-6 from Thread-1c-1 from Thread-2b-7 from Thread-1c-2 from Thread-2b-8 from Thread-1c-3 from Thread-2b-9 from Thread-1c-4 from Thread-2

a 和 b 同時開始運行,輪流輸出。a 完成后,c 開始執(zhí)行。此外,它們?nèi)吭诓煌木€程中運行。通過手動創(chuàng)建 Thread 實例,您可以完全控制它們。ovN28資訊網(wǎng)——每日最新資訊28at.com

然而,請注意,低級線程處理也需要同步和資源管理,這可能更容易出錯和復(fù)雜。ovN28資訊網(wǎng)——每日最新資訊28at.com

2. 并行流(Parallel Streams)

當(dāng)您需要對大型集合中的所有元素應(yīng)用相同、重復(fù)且獨立的任務(wù)時,并行流非常有效。ovN28資訊網(wǎng)——每日最新資訊28at.com

例如,圖像調(diào)整大小是一個需要按順序運行的繁重任務(wù);當(dāng)您有多個圖像需要調(diào)整大小時,如果按順序執(zhí)行,將需要很長時間才能完成。在這種情況下,您可以使用并行流并行調(diào)整它們的大小,如下所示。ovN28資訊網(wǎng)——每日最新資訊28at.com

private static List<BufferedImage> resizeAll(List<BufferedImage> sourceImages,                                             int width, int height) {    return sourceImages            .parallelStream()            .map(source -> resize(source, width, height))            .toList();}

這樣,圖像將同時調(diào)整大小,節(jié)省了大量寶貴的時間。ovN28資訊網(wǎng)——每日最新資訊28at.com

3. ExecutorService

當(dāng)實現(xiàn)不需要精確的線程控制時,可以考慮使用 ExecutorService。ExecutorService 提供了更高層次的線程管理抽象,包括線程池、任務(wù)調(diào)度和資源管理。ovN28資訊網(wǎng)——每日最新資訊28at.com

ExecutorService 是一個接口,它最常見的用法是線程池。假設(shè)您有大量的異步任務(wù)堆積在一起,但是同時運行所有任務(wù)——每個任務(wù)占用一個線程——似乎太多了。線程池可以通過限制最大線程數(shù)來幫助您。ovN28資訊網(wǎng)——每日最新資訊28at.com

下面的示例中,我們使用 Executors.newFixedThreadPool() 實例化 ExecutorService 來使用 3 個線程運行 10 個任務(wù)。每個任務(wù)只打印一行。請注意,我們在之前的部分中重用了之前定義的 CustomTask。ovN28資訊網(wǎng)——每日最新資訊28at.com

ExecutorService executorService = Executors.newFixedThreadPool(3);for (int i = 0; i < 10; i++) {    executorService.submit(new CustomTask(String.valueOf(i), 1));}executorService.shutdown();

這將打印以下結(jié)果:ovN28資訊網(wǎng)——每日最新資訊28at.com

0-0 from pool-1-thread-12-0 from pool-1-thread-31-0 from pool-1-thread-24-0 from pool-1-thread-33-0 from pool-1-thread-25-0 from pool-1-thread-16-0 from pool-1-thread-17-0 from pool-1-thread-38-0 from pool-1-thread-29-0 from pool-1-thread-3

10 個任務(wù)在 3 個線程中運行。通過限制特定任務(wù)使用的線程數(shù),您可以根據(jù)優(yōu)先級分配線程數(shù):對于重要且頻繁的任務(wù)使用更多線程,對于瑣碎或偶爾的任務(wù)使用較少線程。ExecutorService 具有高效和簡潔的特點,是大多數(shù)多線程場景的首選選項。ovN28資訊網(wǎng)——每日最新資訊28at.com

如果您需要更多的控制和靈活性,請查看 ThreadPoolExecutor,它是 Executors.newFixedThreadPool() 返回的 ExecutorService 的實際實現(xiàn)。您可以直接創(chuàng)建其實例或?qū)⒎祷氐?nbsp;ExecutorService 實例轉(zhuǎn)換為 ThreadPoolExecutor 實例以獲得更多控制權(quán)。ovN28資訊網(wǎng)——每日最新資訊28at.com

4. ForkJoinPool

ForkJoinPool是另一種線程池,正如其名稱所示。雖然它在許多其他異步方法的底層使用中,但對于可以分解為較小且獨立子任務(wù)的任務(wù)來說,它也非常強大,這些任務(wù)可以通過分而治之的策略來解決。ovN28資訊網(wǎng)——每日最新資訊28at.com

其中一個任務(wù)是圖像調(diào)整大小。圖像調(diào)整大小是分而治之問題的一個很好的例子。使用ForkJoinPool,您可以將圖像分成兩個或四個較小的圖像,并同時調(diào)整它們的大小。以下是ImageResizeAction的示例,它將圖像調(diào)整為給定的大小。ovN28資訊網(wǎng)——每日最新資訊28at.com

package multithreading;import java.awt.image.BufferedImage;import java.util.concurrent.RecursiveAction;public class ImageResizeAction extends RecursiveAction {    private static final int THRESHOLD = 100;    private final BufferedImage sourceImage;    private final BufferedImage targetImage;    private final int startRow;    private final int endRow;    private final int targetWidth;    private final int targetHeight;    public ImageResizeAction(BufferedImage sourceImage,                             BufferedImage targetImage,                             int startRow, int endRow,                             int targetWidth, int targetHeight) {        this.sourceImage = sourceImage;        this.targetImage = targetImage;        this.startRow = startRow;        this.endRow = endRow;        this.targetWidth = targetWidth;        this.targetHeight = targetHeight;    }    @Override    protected void compute() {        if (endRow - startRow <= THRESHOLD) {            resizeImage();        } else {            int midRow = startRow + (endRow - startRow) / 2;            invokeAll(                    new ImageResizeAction(sourceImage, targetImage,                            startRow, midRow, targetWidth, targetHeight),                    new ImageResizeAction(sourceImage, targetImage,                            midRow, endRow, targetWidth, targetHeight)            );        }    }    private void resizeImage() {        int sourceWidth = sourceImage.getWidth();        double xScale = (double) targetWidth / sourceWidth;        double yScale = (double) targetHeight / sourceImage.getHeight();        for (int y = startRow; y < endRow; y++) {            for (int x = 0; x < sourceWidth; x++) {                int targetX = (int) (x * xScale);                int targetY = (int) (y * yScale);                int rgb = sourceImage.getRGB(x, y);                targetImage.setRGB(targetX, targetY, rgb);            }        }    }}

請注意,ImageResizeAction繼承了RecursiveAction。RecursiveAction用于定義遞歸的調(diào)整大小操作。在此示例中,圖像被分成兩半并并行調(diào)整大小。ovN28資訊網(wǎng)——每日最新資訊28at.com

您可以使用以下代碼運行ImageResizeAction:ovN28資訊網(wǎng)——每日最新資訊28at.com

public static void main(String[] args) throws IOException {    String sourceImagePath = "source_image.jpg";    String targetImagePath = "target_image.png";    int targetWidth = 300;    int targetHeight = 100;    BufferedImage sourceImage = ImageIO.read(new File(sourceImagePath));    BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight,            BufferedImage.TYPE_INT_RGB);    ForkJoinPool forkJoinPool = new ForkJoinPool();    forkJoinPool.invoke(new ImageResizeAction(sourceImage, targetImage,            0, sourceImage.getHeight(), targetWidth, targetHeight));    ImageIO.write(targetImage, "png", new File(targetImagePath));    System.out.println("圖像調(diào)整大小成功!");}

借助ForkJoinPool的幫助,您現(xiàn)在能夠更高效地調(diào)整圖像的大小,具有更好的可伸縮性,并最大程度地利用資源。ovN28資訊網(wǎng)——每日最新資訊28at.com

5. CompletableFuture

通過CompletableFuture,您可以完全發(fā)揮Future的功能,并擁有許多額外的特性。其中最突出的功能是它能夠鏈?zhǔn)降剡B接異步操作,使您能夠構(gòu)建復(fù)雜的異步管道。ovN28資訊網(wǎng)——每日最新資訊28at.com

public static void main(String[] args) {    CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {        System.out.println(Thread.currentThread().getName());        return "Hyuni Kim";    }).thenApply((data) -> {        System.out.println(Thread.currentThread().getName());        return "我的名字是" + data;    }).thenAccept((data) -> {        System.out.println(Thread.currentThread().getName());        System.out.println("結(jié)果:" + data);    });    future.join();}

上述代碼展示了CompletableFuture的一個關(guān)鍵方面:鏈?zhǔn)讲僮?。通過CompletableFuture.supplyAsync(),首先創(chuàng)建并運行一個返回字符串結(jié)果的CompletableFuture。thenApply()接受前一個任務(wù)的結(jié)果,并執(zhí)行其他操作,本例中是添加一個字符串。最后,thenAccept()打印生成的數(shù)據(jù)。結(jié)果如下所示:ovN28資訊網(wǎng)——每日最新資訊28at.com

ForkJoinPool.commonPool-worker-1ForkJoinPool.commonPool-worker-1ForkJoinPool.commonPool-worker-1Result: My name is Hyuni Kim

有3個任務(wù)沒有在主線程中運行,這表明它們與主邏輯并行運行。當(dāng)您有具有結(jié)果并需要鏈接的任務(wù)時,CompletableFuture將是一個很好的選擇。ovN28資訊網(wǎng)——每日最新資訊28at.com

6. 總結(jié)

多線程是一種強大的工具,可以幫助開發(fā)人員優(yōu)化性能、提升用戶體驗、增強并發(fā)處理能力,并充分利用計算機(jī)的資源。ovN28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-13639-0.html掌握這五種多線程方法,提高Java代碼效率

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 實戰(zhàn)!用 Arthas 定位 Spring Boot 接口的超時問題,讓應(yīng)用起飛

下一篇: 8000字+22張圖探秘SpringCloud配置中心的核心原理

標(biāo)簽:
  • 熱門焦點
  • 0糖0卡0脂 旭日森林仙草烏龍茶優(yōu)惠:15瓶到手29元

    旭日森林無糖仙草烏龍茶510ml*15瓶平時要賣為79.9元,今日下單領(lǐng)取50元優(yōu)惠券,到手價為29.9元。產(chǎn)品規(guī)格:0糖0卡0脂,添加草本仙草汁,清涼爽口,富含茶多酚,保留
  • 自動化在DevOps中的力量:簡化軟件開發(fā)和交付

    自動化在DevOps中扮演著重要角色,它提升了DevOps的效能。通過自動化工具和方法,DevOps團(tuán)隊可以實現(xiàn)以下目標(biāo):消除手動和重復(fù)性任務(wù)。簡化流程。在整個軟件開發(fā)生命周期中實現(xiàn)更
  • 一個注解實現(xiàn)接口冪等,這樣才優(yōu)雅!

    場景碼猿慢病云管理系統(tǒng)中其實高并發(fā)的場景不是很多,沒有必要每個接口都去考慮并發(fā)高的場景,比如添加住院患者的這個接口,具體的業(yè)務(wù)代碼就不貼了,業(yè)務(wù)偽代碼如下:圖片上述代碼有
  • 小紅書1周漲粉49W+,我總結(jié)了小白可以用的N條漲粉筆記

    作者:黃河懂運營一條性教育視頻,被54萬人&ldquo;珍藏&rdquo;是什么體驗?最近,情感博主@公主是用鮮花做的,火了!僅僅憑借一條視頻,光小紅書就有超過128萬人,為她瘋狂點贊!更瘋狂的是,這
  • 共享單車的故事講到哪了?

    來源丨??素斀?jīng)與共享充電寶相差不多,共享單車已很久沒有被國內(nèi)熱點新聞關(guān)照到了。除了一再漲價和用戶直呼用不起了。近日多家媒體再發(fā)報道稱,成都、天津、鄭州等地多個共享單
  • 阿里大調(diào)整

    來源:產(chǎn)品劉有媒體報道稱,近期淘寶天貓集團(tuán)啟動了近年來最大的人力制度改革,涉及員工績效、層級體系等多個核心事項,目前已形成一個初步的&ldquo;征求意見版&rdquo;:1、取消P序列
  • 小米公益基金會捐贈2500萬元馳援北京、河北暴雨救災(zāi)

    8月2日消息,今日小米科技創(chuàng)始人雷軍在其微博上發(fā)布消息稱,小米公益基金會宣布捐贈2500萬元馳援北京、河北暴雨救災(zāi)。攜手抗災(zāi),京冀安康!以下為公告原文
  • 三星Galaxy Z Fold5官方渲染圖曝光:13.4mm折疊厚度依舊感人

    據(jù)官方此前宣布,三星將于7月26日在韓國首爾舉辦Unpacked活動,屆時將帶來帶來包括Galaxy Buds 3、Galaxy Watch 6、Galaxy Tab S9、Galaxy Z Flip 5、
  • 英特爾Xe HPG游戲顯卡:擁有512EU,單風(fēng)扇版本

    據(jù)10 月 30 日外媒 TheVerge 消息報道,英特爾 Xe HPG Arc Alchemist 的正面實被曝光,不僅擁有 512 EU 版顯卡,還擁有 128EU 的單風(fēng)扇版本。另外,這款顯卡 PCB
Top