Last active
March 22, 2024 14:34
-
-
Save gbzarelli/275e03275e1586b4a06fee73b8edf72e to your computer and use it in GitHub Desktop.
Await termination many threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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