Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .awaitTermination() establish happens-before with work done in the executor?

Question I've had for years: In this pseudocode,

ExecutorService svc = Executors.newFixedThreadPool(3);
svc.submit(new Runnable() { /* code A */ });
svc.shutdown();
if(svc.awaitTermination(...)) {
    // code B

.awaitTermination() is not documented as establishing happens-before between code A & B. Is there a reason it isn't ?

The ExecutorService and concurrent package javadocs define happens-before between the tasks and work done before they were submitted, but not between executor tasks and code after a successful .awaitTermination() call.

Note, I'm not asking for design critiques on how to restructure my code to leverage documented happens-before relationships. My question here is, is there a reason the docs don't mention happens-before in this situation?

(Note that this is not a duplicate of 22665198 despite the very apt title.)

like image 620
tariksbl Avatar asked Jun 02 '15 19:06

tariksbl


1 Answers

ExecutionService, are you sure? Did you mean ExecutorService? Also, there is no ExecutorService.awaitTermination() method without parameters. The ExecutorService.awaitTermination(long, TimeUnit) method receives the amount of time to wait until timeout. Obviously, if it returns because of the timeout, it cannot guarantee the happens-before relationship, thus it cannot advertise this guarantee in its contract, because it is not there for all cases.

like image 133
Sergei Avatar answered Sep 21 '22 06:09

Sergei