@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class TimeLimitedTransJobTest {
private static ExecutorService TIME_LIMITED_SEND_BACK_THREAD_POOL = new ThreadPoolExecutor(16, 16,
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5000));
@Test
public void test() {
CompletableFuture[] futures = IntStream.range(1, 10)
.mapToObj(this::syncSendByBackPartner)
.toArray(CompletableFuture[]::new);
CompletableFuture<Void> future = CompletableFuture.allOf(futures);
try {
future.get();
log.debug("all done");
} catch (Exception e) {
log.error("syncSendByBackPartner exception", e);
}
}
private CompletableFuture<Void> syncSendByBackPartner(int i) {
return CompletableFuture.runAsync(() -> {
sendBackPartner(i);
}, TIME_LIMITED_SEND_BACK_THREAD_POOL);
}
private void sendBackPartner(int i) {
log.info("do something for num {}...", i);
try {
Thread.sleep(RandomUtils.nextInt(100, 500));
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("num {} done", i);
}
}
CompletableFuture的异步批量
最新推荐文章于 2024-01-06 12:06:44 发布