Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to hand over a method call on a Master to n Workers and return?
    primarykey
    data
    text
    <p>Let's assume we have n Workers which do some computation. The computation may take a while and n computations may run in parallel. Each Worker needs some data structure (not shared between Workers) to do the work. </p> <p>I thought about setting up each Worker during some initialization of the Master and handing over the required data structure to the Worker's constructor. </p> <pre><code>public class Master { public Master() { // initialize n Workers and "register" them "somewhere" } public boolean doCompute(int someInput) throws NoIdleWorkerException { // check if there is an idle Worker, otherwise throw NoIdleWorkerException // call the idle Worker, hand over someInput and wait for the result // synchronously return the result } } </code></pre> <p>A Worker may implement Runnable and then be handed over to a Thread. An instance of Worker may be reused.</p> <pre><code>public class Worker implements Runnable { private SomeDataStructure ds; public Worker(SomeDataStructure ds) { this.ds = ds; } public void run() { // may call doCompute, but run() doesn't has a return type } public boolean doCompute(int someInput) { // do the computation an return } } </code></pre> <p>What is the best way to manage the Worker instances? I was thinking about using ThreadFactory which returns a Thread only if a Worker instance is idle, otherwise null. Using this approach, I would have to manage Worker instances in some data structure.</p> <p>Also, since Master.doCompute(int someInput) has a return value but its computation is done by a Thread, thus asynchronously, I may have to use Futures. Are there any alternatives?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload