ThreadPoolTaskExecutor 提交future任务
时间: 2023-10-12 13:03:55 浏览: 85
spring线程池ThreadPoolExecutor配置以及FutureTask的使用
您可以使用 `ThreadPoolTaskExecutor` 提交 `Future` 任务,以下是一个示例代码:
```java
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class TaskExecutorExample {
private ThreadPoolTaskExecutor taskExecutor;
public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void executeTask() {
Future<String> future = taskExecutor.submit(() -> {
// 在这里编写您的任务逻辑
return "任务执行结果";
});
try {
String result = future.get();
System.out.println("任务执行结果:" + result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们首先注入了一个 `ThreadPoolTaskExecutor` 实
阅读全文