Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active March 22, 2024 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbzarelli/275e03275e1586b4a06fee73b8edf72e to your computer and use it in GitHub Desktop.
Save gbzarelli/275e03275e1586b4a06fee73b8edf72e to your computer and use it in GitHub Desktop.
Await termination many threads
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws InterruptedException {
for(var arg : args){
pool.submit(() -> {
try {
Thread.sleep(5000); //TODO
} catch (Exception e) {}
});
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.HOUR);
}
@When("I waiting for finish the job")
fun i_waiting_for_finish_the_job() {
newScheduledThreadPool(1).apply {
scheduleWithFixedDelay({
statusJobResponseDTO = cercJobsBatchClient.getStatus(startJobResponseDTO.jobId)
if (startJobResponseDTO.status == JobStatus.COMPLETED || startJobResponseDTO.status == JobStatus.FAILED) {
shutdown()
}
}, 10, 10, TimeUnit.SECONDS)
}.also {
it.shutdown()
it.awaitTermination(10, TimeUnit.MINUTES)
}
}
@Then("the job should be finished")
fun the_job_should_be_finished() {
assertNotNull(statusJobResponseDTO)
assertEquals(JobStatus.COMPLETED, statusJobResponseDTO.status)
}
Collapse
public static void main(String[] args) throws InterruptedException {
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(() -> {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
System.out.println("5s");
});
pool.submit(() -> {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("1s");
});
pool.shutdown();
System.out.println("run forest run");
boolean terminate = pool.awaitTermination(1, TimeUnit.SECONDS);
System.out.println("1terminate = "+terminate);
boolean terminate2 = pool.awaitTermination(6, TimeUnit.SECONDS);
System.out.println("2terminate = "+terminate2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment