[concurrency-interest] Help with ThreadPools

J Nguyen jnguyen at automotive.com
Wed Jan 23 17:16:55 EST 2008


You could use Future waiting for the job done.  

 

"Seems like it keeps executing the AAs again before each AA's AATs
completes."

This was caused by calling step(0 in the loop.   "for (AA aa: aas)
executor.execute(aa);" executes quickly and return the control back to
the while loop in VirtualWorld

 

 

________________________________

From: concurrency-interest-bounces at cs.oswego.edu
[mailto:concurrency-interest-bounces at cs.oswego.edu] On Behalf Of Jeffrey
Elrod
Sent: Wednesday, January 23, 2008 1:42 PM
To: concurrency-interest at cs.oswego.edu
Subject: [concurrency-interest] Help with ThreadPools

 

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/25ddee7f/attachment-0001.html 


More information about the Concurrency-interest mailing list