多线程,线程池,异步代码

//首先要有线程池的config
 private CustomThreadPoolExecutor threadPool;
 //下面代码是是使用线程池提交任务,这是异步操作
  threadPool.execute(() -> {
            TimeInterval allTime = new TimeInterval();
            analysisStrategy.analysis(unzipFilePath);
        });
  public static void main(String[] args) {
        CompletableFuture future = CompletableFuture.runAsync(() -> {
            // 异步执行的代码逻辑
            System.out.println("Async code is running");
        });

        // 定义异步操作完成后的回调
        future.thenRun(() -> {
            System.out.println("Async code completed");
        });

        // 主线程继续执行其他任务

    }
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AsyncExample {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();

        executor.submit(() -> {
            // 异步执行的代码逻辑
            System.out.println("Async code is running");
        });

        // 关闭线程池
        executor.shutdown();

        // 主线程继续执行其他任务

    }
}
//异步执行代码
new Thread(()->{accountGradingTaskService.calculate();}).start();

多线程,线程池,异步代码_第1张图片

你可能感兴趣的:(java)