Function 與 CompletableFuture 的組合使用指南
前言
在Java編程領(lǐng)域,函數(shù)式接口和異步編程是提升代碼靈活性和性能的重要手段。Function作為函數(shù)式接口的代表,提供了簡潔的函數(shù)式編程風(fēng)格;CompletableFuture則為異步任務(wù)處理帶來了極大的便利。將Function與CompletableFuture結(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 詳解
CompletableFuture是Java 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); // 輸出: 10thenCompose方法:用于組合兩個(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); // 輸出: 10thenCombine方法:將兩個(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); // 輸出: 8Function 與 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è)CompletableFuture和Function。例如,先異步獲取訂單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);線程池管理:在使用supplyAsync和runAsync方法時(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)行扁平化處理。















 
 
 





 
 
 
 