[concurrency-interest] Help with ThreadPools

Jeffrey Elrod jeffrey.elrod at complexsive.com
Wed Jan 23 16:42:02 EST 2008


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();
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20080123/9c369bae/attachment.html 


More information about the Concurrency-interest mailing list