Worker environments are defined in the gololang.concurrent.workers.WorkerEnvironment
class /
module.
You can directly pass an instance of java.util.concurrent.ExecutorService
to its constructor, or
you may go through its builder object and call either of the following static methods:
withCachedThreadPool()
uses a cached thread pool,
withFixedThreadPool(size)
uses a fixed number of threads in a pool,
withFixedThreadPool()
uses a pool with 1 thread per processor core,
withSingleThreadExecutor()
uses a single executor thread.
In most scenarios withCachedThreadPool()
is a safe choice, but as usual, your mileage varies. If
you have many concurrent tasks to perform and they are not IO-bound, then withFixedThreadPool()
is
probably a better option. You should always measure, and remember that you can always pass a
fine-tuned executor to the WorkerEnvironment()
constructor.
Worker environments also provide delegate methods to their internal executor. It is important to
call shutdown()
to close the workers environment and release the threads pool. You can also call
the awaitTermination
, isShutdown
and isTerminated
methods whose semantics are exactly those of
java.util.concurrent.ExecutorService
.