Java多线程CompletableFuture使用

news2024/9/23 5:31:09

引言

一个接口可能需要调用N个其他服务的接口,这在项目开发中非常常见。如果是串行执行的话,接口的响应速度会很慢。考虑到这些接口之间有大部分都是无前后顺序关联的,可以并行执行。就比如说调用获取商品详情的时候,可以同时调用获取物流信息,通过并行执行多个任务的方式,接口的响应速度会得到大幅度优化。

在这里插入图片描述

对于存在前后顺序关系的接口调用,可以进行编排,如下所示
在这里插入图片描述

  • 获取用户信息之后,才能调用商品详情和物流信息接口。
  • 成功获取商品详情和物流信息之后,才能调用商品推荐接口。

对于Java程序来说,Java8 引入的CompletableFuture可以帮助我们做多个任务的编排,功能非常强大。

Future介绍

Future类是异步思想的典型运用,主要用在一些需要执行耗时任务的场景,避免程序一直原地等待耗时任务执行完成。执行效率太低。具体来说:当我们执行一个耗时的任务时,可以将这个耗时任务交给一个子线程去异步执行,同时,我们可以干点其他事情,不用等待耗时任务执行完成。等我们其他事情做完,再通过Future类获取耗时任务的执行结果。 这样一来,程序的执行效率就明显提高了。

这其实就是多线程中经典的Future模式。核心思想是异步调用,主要用在多线程的领域。

在Java中,Future类只是一个泛型接口,位于java.util.concurrent 包下,其中定义了 5 个方法,主要包括下面这 4 个功能:

  • 取消任务
  • 判断任务是否被取消
  • 判断任务是否已经执行完成
  • 获取任务执行结果
// V 代表了Future执行的任务返回值的类型
public interface Future<V> {
    // 取消任务执行
    // 成功取消返回 true,否则返回 false
    boolean cancel(boolean mayInterruptIfRunning);
    // 判断任务是否被取消
    boolean isCancelled();
    // 判断任务是否已经执行完成
    boolean isDone();
    // 获取任务执行结果
    V get() throws InterruptedException, ExecutionException;
    // 指定时间内没有返回计算结果就抛出 TimeOutException 异常
    V get(long timeout, TimeUnit unit)
     throws InterruptedException, ExecutionException, TimeoutExceptio
}

CompletableFuture介绍

Future在实际使用过程中存在一些局限性比如不支持异步任务的编排组合、获取计算结果的get()方法为阻塞调用。

Java8 引入了CompletableFuture类可以解决Future的这些缺陷。CompletableFuture除了提供更为好用和强大的Future特性之外,还提供了函数式编程、异步任务编排组合(可以将多个任务串联起来,组成一个完整的链式调用)等能力。

public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
}

CompletableFuture同时实现了Future和CompletionStage接口。CompletionStage接口描述了一个异步计算的阶段,很多计算可以分成多个阶段或步骤。此时可以通过他将所有步骤组合起来,形成异步计算的流水线。

CompletionStage接口中的方法比较多,CompletableFuture的函数式能力就是这个接口赋予的,从这个接口的方法参数可以发现其大量使用了Java8 引入的函数式编程。

在这里插入图片描述

CompletableFuture常见操作

创建CompletableFuture

常见的常见CompletableFuture对象的方法如下:

  1. 通过new关键字
  2. 基于CompletableFuture自带的静态工厂方法:runAsync()、supplyAsync()。

new 关键字

代码示例

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureExample {
    public static void main(String[] args) {
        // 创建一个CompletableFuture对象,初始值为null
        CompletableFuture<String> resultFuture = new CompletableFuture<>();
        // 模拟异步任务
        new Thread(() -> {
            // 这里模拟一个耗时操作
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 异步任务完成,设置结果到CompletableFuture中
            resultFuture.complete("Hello, CompletableFuture!");
        }).start();

        // 当异步任务完成时调用该方法
        resultFuture.thenAccept(result -> System.out.println("异步任务完成,结果为: " + result));
        // 等待异步任务完成并获取结果
        try {
            String result = resultFuture.get();
            System.out.println("获取到异步任务的结果: " + result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

说明
通过new关键字创建CompletableFuture对象这种使用方式可以看作是将CompletableFuture当作Future来使用。可以把resultFuture当作是一部运算结果的载体。

 CompletableFuture<String> resultFuture = new CompletableFuture<>();

假设在某个时刻,我们得到了最终的结果,这时,我们可以调用complete()方法为其传入结果,这表示resultFuture已经计算完成。

 resultFuture.complete("Hello, CompletableFuture!");

也可以通过isDone()方法来检查是否已经完成。

public boolean isDone() {
    return result != null;
}

获取异步计算的结果也非常简单,调用get()方法即可。调用get()方法的线程会阻塞直到CompletableFuture完成计算。

String result = resultFuture.get();

静态工厂方法

这两个方法可以帮助我们封装计算逻辑

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier);
// 使用自定义线程池(推荐)
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor);
static CompletableFuture<Void> runAsync(Runnable runnable);
// 使用自定义线程池(推荐)
static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor);

runAsync()方法接受的参数是Runnable,这是一个函数式接口,不允许返回值。当你需要异步操作且不关心返回结果时候使用。

supplyAsync()方法接受的参数是Supplier< U >,这也是一个函数式接口,U是返回结果值的类型。当需要异步操作且关心返回结果的时候,可以使用supplyAsync()方法。

使用示例:

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> System.out.println("hello!"));
future.get();// 输出 "hello!"
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "hello!");
assertEquals("hello!", future2.get());

处理异步计算结果

当我们获取到异步计算的结果之后,还可以对其进行进一步的处理,比较常用的方法如下:

  • thenApply() : 当CompletableFuture完成时,将异步计算结果作为参数传递给指定的函数,并返回一个新的CompletableFuture作为异步结果。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10);
CompletableFuture<String> resultFuture = future.thenApply(i -> "Result: " + i);
System.out.println(resultFuture.get()); // 输出 "Result: 10"
  • thenAccept():当CompletableFuture完成时,将异步计算结果作为参数传递给指定的消费者函数,不返回任何结果。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10);
future.thenAccept(i -> System.out.println("Result: " + i));
  • thenRun():当CompletableFuture完成时,执行指定的Runnable动作,不接受异步计算结果。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10);
future.thenRun(() -> System.out.println("Done!"));
  • whenComplete():当CompletableFuture完成时,指定指定的动作,无论异步任务是否发生异常都会执行该操作
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10 / 0);
future.whenComplete((result, exception) -> {
    if (exception != null) {
        System.out.println("Exception occurred: " + exception.getMessage());
    } else {
        System.out.println("Result: " + result);
    }
});

1. thenApply()

方法接受一个Function实例,用它来处理结果。

// 沿用上一个任务的线程池
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) {
    return uniApplyStage(null, fn);
}

//使用默认的 ForkJoinPool 线程池(不推荐)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) {
    return uniApplyStage(defaultExecutor(), fn);
}
// 使用自定义线程池(推荐)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) {
    return uniApplyStage(screenExecutor(executor), fn);
}

使用示例如下:
实例一:

CompletableFuture<String> future = CompletableFuture.completedFuture("hello!")
        .thenApply(s -> s + "world!");
assertEquals("hello!world!", future.get());
// 这次调用将被忽略。
future.thenApply(s -> s + "nice!");
assertEquals("hello!world!", future.get());

实例二:还可以进行流式调用

CompletableFuture<String> future = CompletableFuture.completedFuture("hello!")
        .thenApply(s -> s + "world!").thenApply(s -> s + "nice!");
assertEquals("hello!world!nice!", future.get());

如果不需要从回调函数中获取返回结果,可以使用thenAccept()或者thenRun()。这两个方法的区别在于thenRun不能访问异步计算的结果。

2. thenAccpet()
方法的参数是 Consumer<? super T> action,将异步计算结果作为参数传递给指定的消费者函数。

public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {
    return uniAcceptStage(null, action);
}

public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {
    return uniAcceptStage(defaultExecutor(), action);
}

public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,
                                               Executor executor) {
    return uniAcceptStage(screenExecutor(executor), action);
}

3. thenRun()
方法参数是Runnable,不能访问异步计算的结果,只能在异步计算完成后,继续其他操作。

public CompletableFuture<Void> thenRun(Runnable action) {
    return uniRunStage(null, action);
}

public CompletableFuture<Void> thenRunAsync(Runnable action) {
    return uniRunStage(defaultExecutor(), action);
}

public CompletableFuture<Void> thenRunAsync(Runnable action,
                                            Executor executor) {
    return uniRunStage(screenExecutor(executor), action);
}

4. whenComplete()
whenComplete() 的方法的参数是 BiConsumer<? super T, ? super Throwable> 。

public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action) {
    return uniWhenCompleteStage(null, action);
}
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) {
    return uniWhenCompleteStage(defaultExecutor(), action);
}
// 使用自定义线程池(推荐)
public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, 
Executor executor) {
    return uniWhenCompleteStage(screenExecutor(executor), action);
}

BiConsumer可以接受两个输入对象然后进行消费,BiConsumer<? super T, ? super Throwable>
第一个参数

?super T : 父类为CompletableFuture创建时使用泛型T 定义的返回结果类型。传入异步计算结果。
?super Throwable:父类为Throwable,即异步计算结果抛出异常时,该字段不为空。

处理异常

可以通过handle()方法来处理任务执行过程中可能出现的异常情况。

public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) {
    return uniHandleStage(null, fn);
}

public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) {
    return uniHandleStage(defaultExecutor(), fn);
}

public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, 
Executor executor) {
    return uniHandleStage(screenExecutor(executor), fn);
}

示例代码:

CompletableFuture<String> future= CompletableFuture.supplyAsync(() -> {
    if (true) {
        throw new RuntimeException("Computation error!");
    }
    return "hello!";
}).handle((res, ex) -> {
    // res 代表返回的结果
    // ex 的类型为 Throwable ,代表抛出的异常
    return res != null ? res : "world!";
});
assertEquals("world!", future.get());

还可以通过 exceptionally() 方法来处理异常情况。

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
    if (true) {
        throw new RuntimeException("Computation error!");
    }
    return "hello!";
}).exceptionally(ex -> {
    System.out.println(ex.toString());// CompletionException
    return "world!";
});
assertEquals("world!", future.get());

如果想让CompletableFuture结果就是异常的话,可以使用completeExceptionally()为其赋值。

CompletableFuture<String> completableFuture = new CompletableFuture<>();
// ...
completableFuture.completeExceptionally(
  new RuntimeException("Calculation failed!"));
// ...
completableFuture.get(); // ExecutionException

组合CompletableFuture

可以使用thenCompose() 按顺序链接两个completableFuture对象,实现异步的任务链。他的作用是将前一个任务的返回结果作为下一个任务的输入参数,从而形成一个依赖关系。

public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) {
    return uniComposeStage(null, fn);
}

public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) {
    return uniComposeStage(defaultExecutor(), fn);
}

public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn,
    Executor executor) {
    return uniComposeStage(screenExecutor(executor), fn);
}

使用示例:

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello!")
        .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + "world!"));
assertEquals("hello!world!", future.get());

在实际开发中,这个方法还是非常有用的,比如,task1和task2都是异步执行,但是task1必须执行完成后才能开始执行task2(task2依赖task1的执行结果)。
和thenCompose()类似的还有thenCombine(),他同样可以组合两个CompletableFuture对象。

CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "hello!")
        .thenCombine(CompletableFuture.supplyAsync( () -> "world!"), (s1, s2) -> s1 + s2)
        .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + "nice!"));
assertEquals("hello!world!nice!", completableFuture.get());

thenCompose()和thenCombine()有什么区别

  • thenCompose()可以链接两个completableFuture对象,并将前一个任务的结果作为下一个任务的参数,他们之间存在先后关系。
  • thenCombine() 会在两个任务都执行完成后,把两个任务的结果合并。两个任务是并行执行的。他们之间没有先后依赖关系。

除了thenCompose()、thenCombine() 之外,还有一些其他的组合completableFuture方法用于实现不同的效果,满足不同的业务需求。

例如,如果我们想要实现task1和task2中的任意一个任务后就可以执行task3的话,可以使用acceptEither().

public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) {
    return orAcceptStage(null, other, action);
}

public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) {
    return orAcceptStage(asyncPool, other, action);
}

示例程序:

CompletableFuture<String> task = CompletableFuture.supplyAsync(() -> {
    System.out.println("任务1开始执行,当前时间:" + System.currentTimeMillis());
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("任务1执行完毕,当前时间:" + System.currentTimeMillis());
    return "task1";
});

CompletableFuture<String> task2 = CompletableFuture.supplyAsync(() -> {
    System.out.println("任务2开始执行,当前时间:" + System.currentTimeMillis());
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("任务2执行完毕,当前时间:" + System.currentTimeMillis());
    return "task2";
});

task.acceptEitherAsync(task2, (res) -> {
    System.out.println("任务3开始执行,当前时间:" + System.currentTimeMillis());
    System.out.println("上一个任务的结果为:" + res);
});

// 增加一些延迟时间,确保异步任务有足够的时间完成
try {
    Thread.sleep(2000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

输出:

任务1开始执行,当前时间:1695088058520
任务2开始执行,当前时间:1695088058521
任务1执行完毕,当前时间:1695088059023
任务3开始执行,当前时间:1695088059023
上一个任务的结果为:task1
任务2执行完毕,当前时间:1695088059523

任务组合操作acceptEitherAsync()会在异步任务1和异步任务2中的任意一个完成时出发执行任务3.但需要注意,这个触发时机不确定。

并行运行多个CompletableFuture

可以通过CompletableFuture的allOf()这个静态方法来并行运行多个CompletableFuture。

实际项目中,我们经常需要并行运行多个互不相关的任务。这些任务之间没有依赖关系,可以互相独立的运行。

比如,我们需要读取处理6个文件,这6个任务都是没有执行顺序依赖的任务,但是我们需要返回给用户的时候将这几个文件的处理结果进行统计整理,这种情况我们可以使用并行运行多个CompletableFuture来处理。

示例如下:

CompletableFuture<Void> task1 = CompletableFuture.supplyAsync(()->{
    //自定义业务操作
  });
......
CompletableFuture<Void> task6 = CompletableFuture.supplyAsync(()->{
    //自定义业务操作
  });
......
 CompletableFuture<Void> headerFuture=CompletableFuture.allOf(task1,.....,task6);
  try {
    headerFuture.join();
  } catch (Exception ex) {
    ......
  }
System.out.println("all done. ");

经常和allOf()方法对比的是anyOf()方法。

allOf()方法会等到所有的CompletableFuture都运行完成之后在返回。调用join方法让task1。。。。task6都运行完之后在继续执行
anyOf方法不会等待所有的CompletableFuture都运行完成之后在返回,只要有一个执行完成即可。

CompletableFuture使用建议

使用自定义线程池

CompletableFuture默认使用ForkJoinPool.commonPool()作为执行器,这个线程池是全局共享的,可能会被其他任务占用,导致性能下降或者饥饿。建议使用自定义的线程池来执行CompletableFuture的异步任务,可以提高并发度和灵活性。

private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10,
        0L, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>());

CompletableFuture.runAsync(() -> {
     //...
}, executor);

尽量避免使用get()

CompletableFuture的get()方法是阻塞的,尽量避免使用。如果必须使用的话,需要添加超时时间,否则可能会导致主线程一直等待,无法执行其他任务。

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(10_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Hello, world!";
    });

    // 获取异步任务的返回值,设置超时时间为 5 秒
    try {
        String result = future.get(5, TimeUnit.SECONDS);
        System.out.println(result);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        // 处理异常
        e.printStackTrace();
    }
}

上面这段代码在调用get()时抛出TimeoutException,我们可以在异常处理中进行相应的操作,如取消任务、重试任务、记录日志等。

正确进行异常处理

使用 CompletableFuture的时候一定要以正确的方式进行异常处理,避免异常丢失或者出现不可控问题。

  • 使用whenComplete方法可以在任务完成时触发回调函数,并正确处理异常,而不是让异常被吞噬或丢失。
  • 使用exceptionally方法可以处理异常并重新抛出,以便异常能够传播到后续阶段,而不是让异常被忽略或者终止。
  • 使用handle方法可以处理正常的返回结果和异常,并返回一个新的结果,而不是让异常影响整体的逻辑。
  • 使用CompletableFuture.allOf方法可以组合多个CompletableFuture,并统一处理所有任务的异常,而不是让异常处理过于冗长或者重复。

合理组合多个异步任务

正确使用thenCompose()、thenCombine()、acceptFilter()、allOf()、anyOf()等方法来组合多个异步任务,以满足实际业务需求,提高程序执行效率。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1813781.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

高效换热管

绕管式高效换热器 绕管换热器是一种结构紧凑&#xff0c;传热效率高的新型高效换热器。换热管按螺旋线形状交替缠绕在芯筒与外筒之间&#xff0c;相邻两层螺旋状换热管旋向相反&#xff0c;并采用一定形状的定距元件使之保持一定间距。层与层间换热管反向缠绕&#xff0c;极大…

解决python import时ModuleNotFoundError异常

文章目录 前言 一 import索引机制二 ModuleNotFoundError原因及其解决办法1. 模块&#xff0c;包名字错误2. 模块&#xff0c;包未导入或未安装3. 模块&#xff0c;包命名冲突4. 模块&#xff0c;包路径不在import索引范围内(常见且重点) 前言 在学习和使用python import xxx时…

“改进型”Howland 电流泵电路

“改进型”Howland 电流泵电路 “改进型”Howland 电流泵是一种使用差分放大器在分流电阻器 (Rs) 上施加电压的电路&#xff0c;从而产生能够驱动大范 围负载电阻的双极性&#xff08;拉电流或灌电流&#xff09;压控电流源。 设计注释 确保运算放大器的输入端&#xff08;V…

Spring应用如何打印access日志和out日志(用于分析请求总共在服务耗费多长时间)

我们经常会被问到这样一个问题。你接口返回的好慢呀&#xff0c;能不能提升一下接口响应时间啊&#xff1f;这个时候我们就需要去分析&#xff0c;为什么慢&#xff0c;慢在哪。而这首先应该做的就是确定接口返回时间过长确实是在服务内消耗的时间。而不是我们将请求发给网关或…

STM32高级篇—按键FIFO

想成问一名非常优秀的嵌入式软件工程师&#xff0c;是需要掌握很多知识的。 完成STM32的基础内容的学习后&#xff0c;我们也进入到学习STM32高级的内容上。 本人也是一名嵌入式的初级入坑人&#xff0c;写的内容可能会有错误&#xff0c;或者不正确的地方也请大家多多指教&…

1.0 Android中Activity的基础知识

一&#xff1a;Activity的定义 Activity是一个应用组件&#xff0c;它提供了一个用户界面&#xff0c;允许用户执行一个单一的、明确的操作&#xff0c;用户看的见的操作都是在activity中执行的。Activity的实现需要在manifest中进行定义&#xff0c;不让会造成程序报错。 1.…

【动态规划】| 路径问题之不同路径 力扣62

&#x1f397;️ 主页&#xff1a;小夜时雨 &#x1f397;️ 专栏&#xff1a;动态规划 &#x1f397;️ 如何活着&#xff0c;是我找寻的方向 目录 1. 题目解析2. 代码 1. 题目解析 题目链接: https://leetcode.cn/problems/unique-paths/description/ 通常动态规划的题目有…

【Linux】解决由于 network和 NetworkManager不兼容,导致的网络服务错误

今天尝试启动在虚拟机中启动一些中间件环境&#xff08;用docker管理&#xff09;&#xff0c;使用ssh连接虚拟机&#xff0c;却始终无法连接 我怀疑是不是虚拟机的ip地址改变了&#xff0c;但是之前我的虚拟机ip已经在 /etc/sysconfig/network-scripts 目录下&#xff0c;创…

PCA与LDA

共同点 降维方法&#xff1a; PCA和LDA都是数据降维的方式&#xff0c;它们都能通过某种变换将原始高维数据投影到低维空间。 数学原理&#xff1a; 两者在降维过程中都使用了矩阵特征分解的思想&#xff0c;通过对数据的协方差矩阵或类间、类内散度矩阵进行特征分解&#xff…

Vue项目实践:使用滚动下拉分页优化大数据展示页面【通过防抖加标志位进行方案优化】

Vue项目实践&#xff1a;使用滚动下拉分页优化大数据展示页面 前言 传统的分页机制通过点击页码来加载更多内容&#xff0c;虽然直观&#xff0c;但在处理大量数据时可能会导致用户体验不佳。相比之下&#xff0c;滚动下拉分页能够在用户滚动到页面底部时自动加载更多内容&…

实现JWT认证与授权的Spring Boot项目详解

我们将详细介绍如何使用JWT&#xff08;JSON Web Tokens&#xff09;结合Spring Boot框架实现用户认证和授权系统。此方案将包括用户注册、登录以及通过JWT令牌进行后续请求的身份验证过程。我们将从引入必要的依赖开始&#xff0c;然后逐步构建项目的各个部分&#xff0c;包括…

如何查看当前的gruop_id 的kafka 消费情况 这个可以查看到是否存在消费阻塞问题

如何查看当前的gruop_id 的kafka 消费情况 这个可以查看到是否存在消费阻塞问题 命令如下: /kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --group GWW --describe 其中 127.0.0.1 为zookeeper 服务器ip GWW 为对应要查看的group_id 如下…

从零到一建设数据中台(番外篇)- 数据中台UI欣赏

番外篇 - 数据中台 UI 欣赏 话不多说&#xff0c;直接上图。 数据目录的重要性&#xff1a; 数据目录是一种关键的信息管理工具&#xff0c;它为组织提供了一个全面的、集中化的数据资产视图。 它不仅记录了数据的存储位置&#xff0c;还详细描述了数据的结构、内容、来源、使…

800W-2300W-4500W-7000W线绕电阻器的选型参考

EAK线绕电阻器将普通电阻器材料的高脉冲稳定性与优化的导热和高度保护相结合。安装在导热表面上可进一步改善散热并提高稳定性。 EAK提供各种外壳设计和材料&#xff08;如铝和钢&#xff09;的导线电阻器。它们符合 UL508 的要求&#xff0c;在用作制动、充电、放电或加热电阻…

Java面试八股之静态变量和实例变量的区别有哪些

Java静态变量和实例变量的区别有哪些 存储位置和生命周期&#xff1a; 静态变量&#xff1a;静态变量属于类级别&#xff0c;存储在Java的方法区&#xff08;或称为类区&#xff0c;随JVM实现而异&#xff0c;现代JVM中通常在元数据区内&#xff09;&#xff0c;并且在类首次…

Rocky Linux 9.4 部署Zabbix 7.0

文章目录 Zabbix基本概念zabbix介绍zabbix特性zabbix结构 安装Zabbix主机名配置配置Zabbix-Server(1)禁用EPEL提供的Zabbix软件包(2)安装Zabbix Server、Web前端、Agent(3)创建初始数据库(4)Zabbix server配置数据库(5)为Zabbix前端配置PHP(6)启动Zabbix server和agent进程(7)放…

Windows电脑清理C盘内存空间

ps&#xff1a;过程截图放在篇末 一、%tmp%文件 win R键呼出运行窗口&#xff0c;输入 %tmp% 自动进入tmp文件夹&#xff0c;ctrl A全选删除 遇到权限不足&#xff0c;正在运行&#xff0c;丢失的文件直接跳过即可 二、AppData文件夹 1、pipcache 在下列路径下面&…

Amortized bootstrapping via Automorphisms

参考文献&#xff1a; [MS18] Micciancio D, Sorrell J. Ring packing and amortized FHEW bootstrapping. ICALP 2018: 100:1-100:14.[GPV23] Guimares A, Pereira H V L, Van Leeuwen B. Amortized bootstrapping revisited: Simpler, asymptotically-faster, implemented. …

代理设计模式之JDK动态代理CGLIB动态代理原理与源码剖析

代理设计模式 代理模式(Proxy),为其它对象提供一种代理以控制对这个对象的访问。如下图 从上面的类图可以看出,通过代理模式,客户端访问接口时的实例实际上是Proxy对象,Proxy对象持有RealSubject的引用,这样一来Proxy在可以在实际执行RealSubject前后做一些操作,相当于…

MTK烧录USB驱动下载

下载链接 https://www.catalog.update.microsoft.com/Search.aspx?qMediaTek%20USB%20Port 驱动安装教程 https://miuiver.com/install-official-mediatek-driver/