spingboot整合CompletableFuture

@SpringBootConfiguration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer
{
    @Override
    @Bean
    public Executor getAsyncExecutor()
    {
        ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(5);
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setQueueCapacity(25);
        taskExecutor.setKeepAliveSeconds(300);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler()
    {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}
@Component
public class AsyncService
{
    @Async
    public CompletableFuture<String> add(int i)
    {
        try
        {
            i+=2;
            Thread.sleep(1000);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("add return :"+i);
    }

    @Async
    public CompletableFuture<String> sub(int i)
    {
        try
        {
            i-=2;
            Thread.sleep(1000);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("sub return :"+i);
    }
    
    @Async
    public CompletableFuture<String> mul(int i)
    {
        try
        {
            i*=2;
            Thread.sleep(1000);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("mul return :"+i);
    }

    @Async
    public CompletableFuture<String> div(int i)
    {
        try
        {
            i/=2;
            Thread.sleep(1000);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("div return :"+i);
    }
}
@SpringBootApplication
public class Application implements CommandLineRunner
{
    @Autowired
    private AsyncService asyncService;
    
    public static void main(String[] args)
    {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.run(args);
    }

    @Override
    public void run(String... args) throws Exception
    {
        testAsyncReturnCompletableFuture();
    }
    
    public void testAsyncReturnCompletableFuture() throws Exception
    {
        long start=System.currentTimeMillis();
        CompletableFuture<String> future1= asyncService.add(1);
        CompletableFuture<String> future2= asyncService.sub(2);
        CompletableFuture<String> future3= asyncService.mul(3);
        CompletableFuture<String> future4= asyncService.div(4);
        CompletableFuture.allOf(future1,future2,future3).join();
        System.out.println("completablefuture elapsed time:"+(System.currentTimeMillis()-start));
        System.out.println(future1.get());
        System.out.println(future2.get());
        System.out.println(future3.get());
        System.out.println(future4.get());
        System.out.println("completablefuture elapsed time:"+(System.currentTimeMillis()-start));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36203787/article/details/85013343