偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Function 與 CompletableFuture 的組合使用指南

開發(fā) 前端
在Java編程領(lǐng)域,函數(shù)式接口和異步編程是提升代碼靈活性和性能的重要手段。Function作為函數(shù)式接口的代表,提供了簡潔的函數(shù)式編程風(fēng)格;CompletableFuture則為異步任務(wù)處理帶來了極大的便利。

前言

Java編程領(lǐng)域,函數(shù)式接口和異步編程是提升代碼靈活性和性能的重要手段。Function作為函數(shù)式接口的代表,提供了簡潔的函數(shù)式編程風(fēng)格;CompletableFuture則為異步任務(wù)處理帶來了極大的便利。將FunctionCompletableFuture結(jié)合使用,能夠在異步操作中實(shí)現(xiàn)強(qiáng)大的數(shù)據(jù)轉(zhuǎn)換和流程控制。

Function 接口詳解

Function接口是Java 8引入的函數(shù)式接口,位于java.util.function包中。它代表了一個(gè)接受一個(gè)參數(shù)并返回一個(gè)結(jié)果的函數(shù),其定義如下:

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}

其中,T是輸入?yún)?shù)的類型,R是返回結(jié)果的類型。apply方法是該接口的抽象方法,用于執(zhí)行具體的函數(shù)邏輯。

基本使用

通過實(shí)現(xiàn)apply方法,可以定義具體的函數(shù)操作。例如,將一個(gè)字符串轉(zhuǎn)換為大寫形式:

public class FunctionExample {
    public static void main(String[] args) {
        Function<String, String> toUpperCaseFunction = String::toUpperCase;
        String result = toUpperCaseFunction.apply("hello");
        System.out.println(result); // 輸出: HELLO
    }
}

接口的組合

andThen方法:先執(zhí)行當(dāng)前函數(shù),再將結(jié)果作為參數(shù)傳遞給另一個(gè)函數(shù):

Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> addOne = x -> x + 1;
Function<Integer, Integer> composedFunction = square.andThen(addOne);
int result = composedFunction.apply(2);
System.out.println(result); // 輸出: 5(先平方得4,再加1)

compose方法:先執(zhí)行傳入的函數(shù),再將結(jié)果作為參數(shù)傳遞給當(dāng)前函數(shù):

Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> addOne = x -> x + 1;
Function<Integer, Integer> composedFunction = square.compose(addOne);
int result = composedFunction.apply(2);
System.out.println(result); // 輸出: 9(先加1得3,再平方)

CompletableFuture 詳解

CompletableFutureJava 8引入的用于異步編程的類,它實(shí)現(xiàn)了Future接口和CompletionStage接口,提供了豐富的方法來處理異步任務(wù)的創(chuàng)建、組合和結(jié)果獲取。

基本使用

使用supplyAsync方法創(chuàng)建有返回值的異步任務(wù):

public class CompletableFutureExample {
    public static void main(String[] args) {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000); // 模擬耗時(shí)操作
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello, CompletableFuture!";
        });
        // 異步獲取結(jié)果
        future.thenAccept(System.out::println);
    }
}

使用runAsync方法創(chuàng)建無返回值的異步任務(wù):

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Task completed without return value");
});

結(jié)果處理與組合

thenApply方法:在異步任務(wù)完成后,對結(jié)果進(jìn)行轉(zhuǎn)換:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 5)
       .thenApply(x -> x * 2);
future.thenAccept(System.out::println); // 輸出: 10

thenCompose方法:用于組合兩個(gè)CompletableFuture,將前一個(gè)任務(wù)的結(jié)果作為后一個(gè)任務(wù)的輸入:

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 5);
CompletableFuture<Integer> future2 = future1.thenCompose(x -> CompletableFuture.supplyAsync(() -> x * 2));
future2.thenAccept(System.out::println); // 輸出: 10

thenCombine方法:將兩個(gè)CompletableFuture的結(jié)果進(jìn)行合并處理:

CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 5);
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 3);
CompletableFuture<Integer> combinedFuture = future1.thenCombine(future2, (x, y) -> x + y);
combinedFuture.thenAccept(System.out::println); // 輸出: 8

Function 與 CompletableFuture 的結(jié)合應(yīng)用

在 thenApply 中使用 Function

thenApply方法接受一個(gè)Function作為參數(shù),在異步任務(wù)完成后對結(jié)果進(jìn)行轉(zhuǎn)換。例如,從數(shù)據(jù)庫異步獲取用戶ID,然后根據(jù)ID查詢用戶信息:

class User {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class CombinedExample {
    public static CompletableFuture<Integer> getUserIdAsync() {
        return CompletableFuture.supplyAsync(() -> 1);
    }

    public static User getUserById(int id) {
        return new User(id, "John Doe");
    }

    public static void main(String[] args) {
        Function<Integer, User> idToUserFunction = CombinedExample::getUserById;
        CompletableFuture<User> userFuture = getUserIdAsync()
               .thenApply(idToUserFunction);
        userFuture.thenAccept(user -> System.out.println("User: " + user.getName()));
    }
}

組合使用

在更復(fù)雜的場景中,可能需要組合多個(gè)CompletableFutureFunction。例如,先異步獲取訂單ID,再根據(jù)訂單ID獲取訂單詳情,最后對訂單詳情進(jìn)行處理:

class Order {
    private int id;
    private String details;

    public Order(int id, String details) {
        this.id = id;
        this.details = details;
    }
}

class OrderService {
    public static CompletableFuture<Integer> getOrderIdAsync() {
        return CompletableFuture.supplyAsync(() -> 1);
    }

    public static Order getOrderById(int id) {
        return new Order(id, "Order details for " + id);
    }

    public static String processOrder(Order order) {
        return"Processed: " + order.getDetails();
    }
}

public class ComplexCombinedExample {
    public static void main(String[] args) {
        Function<Integer, Order> idToOrderFunction = OrderService::getOrderById;
        Function<Order, String> orderToProcessedFunction = OrderService::processOrder;

        CompletableFuture<String> processedOrderFuture = OrderService.getOrderIdAsync()
               .thenApply(idToOrderFunction)
               .thenApply(orderToProcessedFunction);

        processedOrderFuture.thenAccept(System.out::println);
    }
}

批量使用

class UserService {
    public static CompletableFuture<UserDetail> getUserDetailAsync(int userId) {
        return CompletableFuture.supplyAsync(() -> {
            // 模擬耗時(shí)操作,如數(shù)據(jù)庫查詢
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 假設(shè)根據(jù)userId生成用戶詳情
            return new UserDetail(userId, "user" + userId, "user" + userId + "@example.com");
        });
    }
}

public class Main {
    public static void main(String[] args) {
        List<Integer> userIds = new ArrayList<>();
        userIds.add(1);
        userIds.add(2);
        userIds.add(3);

        // 定義一個(gè)Function,將用戶ID轉(zhuǎn)換為獲取用戶詳情的CompletableFuture
        Function<Integer, CompletableFuture<UserDetail>> pipeline = UserService::getUserDetailAsync;

        List<CompletableFuture<UserDetail>> tasks = userIds.stream()
               .map(pipeline)
               .collect(Collectors.toList());

        CompletableFuture<List<UserDetail>> resultFuture =
                CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0]))
                       .thenApply(v -> tasks.stream().map(CompletableFuture::join).toList());

        // 等待所有任務(wù)完成并獲取結(jié)果
        resultFuture.join().forEach(System.out::println);
    }
}

注意事項(xiàng)

異常處理:在CompletableFuture的異步操作中,需要注意異常處理??梢允褂?/span>exceptionally方法來處理異步任務(wù)中的異常:

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    throw new RuntimeException("Something went wrong");
}).exceptionally(ex -> {
    if (ex instanceof TimeoutException) return "TIMEOUT";
    if (ex instanceof BusinessException) return "BUSINESS_FAIL";
    return "UNKNOWN";
});
future.thenAccept(System.out::println);

線程池管理:在使用supplyAsyncrunAsync方法時(shí),可以指定自定義的線程池,以更好地管理異步任務(wù)的執(zhí)行:

public class ThreadPoolExample {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            // 異步任務(wù)邏輯
        }, executorService);
        // 關(guān)閉線程池
        executorService.shutdown();
    }
}

避免過度嵌套:在組合多個(gè)CompletableFuture時(shí),要避免過度嵌套,以免代碼變得難以閱讀和維護(hù)??梢允褂?/span>thenCompose等方法進(jìn)行扁平化處理。

責(zé)任編輯:武曉燕 來源: 一安未來
相關(guān)推薦

2022-09-29 09:07:08

DataGrip數(shù)據(jù)倉庫數(shù)據(jù)庫

2012-12-26 12:41:14

Android開發(fā)WebView

2011-07-21 14:57:34

jQuery Mobi

2021-07-27 10:09:27

鴻蒙HarmonyOS應(yīng)用

2010-09-06 14:24:28

ppp authent

2009-12-28 17:40:10

WPF TextBox

2025-07-02 09:00:24

ES運(yùn)維JSON

2009-12-31 17:17:45

Silverlight

2021-01-12 15:19:23

Kubernetes

2017-01-04 15:22:57

TrimPath模板引擎

2010-08-04 15:37:31

Flex圖表

2010-08-05 15:40:21

FlexBuilder

2010-06-03 17:27:36

Hadoop命令

2025-06-10 08:20:00

IDEA編碼開發(fā)

2009-11-30 13:15:27

PHP模板Smarty

2010-12-12 11:27:00

PGP使用指南

2023-10-25 14:51:38

MySQL數(shù)據(jù)庫JSON

2019-11-13 12:39:26

Python 開發(fā)編程語言

2024-02-04 00:00:00

Loki性能查詢

2010-08-04 14:28:01

Flex組件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號