nyx*_*yxz 5 java spring multithreading asynchronous nonblocking
有人能告诉我有没有一种方法可以在@Async不阻塞/等待结果的情况下使用 Spring 框架的注释?这是一些代码来澄清我的问题:
@Service
public class AsyncServiceA {
@Autowired
private AsyncServiceB asyncServiceB;
@Async
public CompletableFuture<String> a() {
ThreadUtil.silentSleep(1000);
return asyncServiceB.b();
}
}
@Service
public class AsyncServiceB {
@Async
public CompletableFuture<String> b() {
ThreadUtil.silentSleep(1000);
return CompletableFuture.completedFuture("Yeah, I come from another thread.");
}
}
Run Code Online (Sandbox Code Playgroud)
和配置:
@SpringBootApplication
@EnableAsync
public class Application implements AsyncConfigurer {
private static final Log LOG = LogFactory.getLog(Application.class);
private static final int THREAD_POOL_SIZE = 1;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
final AsyncServiceA bean = ctx.getBean(AsyncServiceA.class);
bean.a().whenComplete(LOG::info);
};
}
@Override
@Bean(destroyMethod = "shutdown")
public ThreadPoolTaskExecutor getAsyncExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(THREAD_POOL_SIZE);
executor.setMaxPoolSize(THREAD_POOL_SIZE);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
// omitted
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,执行程序经过调用AsyncServiceA.a()并离开,但它仍然保留池中等待该CompletableFuture.get()方法的线程。由于池中只有一个线程,AsyncServiceB.b()因此无法执行。我期待的是该线程在执行后返回到池中AsyncServiceA.a(),然后可用于执行AsyncServiceB.b().
有没有办法做到这一点?
注1:我也试过,ListenableFuture但结果是一样的。
注 2:我已经成功地手动(没有@Async)通过将执行程序提供给每个方法,如下所示:
异步服务A
public CompletableFuture<String> manualA(Executor executor) {
return CompletableFuture.runAsync(() -> {
LOG.info("manualA() working...");
ThreadUtil.silentSleep(1000);
}, executor)
.thenCompose(x -> asyncServiceB.manualB(executor));
}
Run Code Online (Sandbox Code Playgroud)
异步服务B
public CompletableFuture<String> manualB(Executor executor) {
return CompletableFuture.runAsync(() -> {
LOG.info("manualB() working...");
ThreadUtil.silentSleep(1000);
}, executor)
.thenCompose(x -> CompletableFuture
.supplyAsync(() -> "Yeah, I come from another thread.", executor));
}
Run Code Online (Sandbox Code Playgroud)
这是ThreadUtil如果有人想知道。
public class ThreadUtil {
public static void silentSleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:添加了非阻塞异步注释的问题https://jira.spring.io/browse/SPR-15401