[concurrency-interest] Help with ThreadPools

Joe Bowbeer joe.bowbeer at gmail.com
Wed Jan 23 17:32:00 EST 2008


The following response is based on your description and not on your code:

1. Look at ExecutorService.invokeAll().  This would force AA to wait
until its subtasks have completed.

2. Alternatively, look at ExecutorCompletionService.

--Joe

On Jan 23, 2008 1:42 PM, Jeffrey Elrod <jeffrey.elrod at complexsive.com> wrote:
>
> I'm hoping someone could point me in the right direction.  You will probably
> figure out that I'm new to Java concurrency.
>
> I have a virtual world with a fixed number of autonomous agents (AA) and
> each agent has the ability to do numerous tasks (AAT) simultaneously.  Seems
> like it keeps executing the AAs again before each AA's AATs completes.  I
> was thinking about using a custom ThreadPoolExecutor and a CountDownLatch to
> not execute the AAs again until their AATs complete but can't see how.  Just
> point me in the right direction.  Thanks.  Jeff
>
> Here is my example:
>
> public abstract class AAT implements Runnable {
>     public AAT() {}
>
>     // run method in subclasses
> }
>
> public class AA implements Runnable {
>     private Vector<AAT> aats;
>     private Executor executor;
>
>     public AA() {
>         executor = Executors.newCachedThreadPool();
>     }
>
>     public void addAAT(AAT aat) {
>         aats.add(aat);
>     }
>
>     public void run() {
>         for (AAT aat: aats) executor.execute(aat);
>     }
> }
>
> public class VirtualWorld {
>     private Vector<AA> aas;
>
>     private Executor executor;
>
>     public VirtualWorld(Vector<AA> aas) {
>         this.aas = aas;
>         executor = Executors.newCachedThreadPool();
>     }
>
>      public void step() {
>         for (AA aa: aas) executor.execute(aa);
>     }
> }
>
> public VirtualWorldTest {
>     public static void main(String[] args) {
>         world = new VirtualWorld(Vector<AA> aas);
>         while (// no reason to stop) world.step();
>     }
> }
>


More information about the Concurrency-interest mailing list