Executor

Discussion in 'Java' started by Kenneth P. Turvey, Sep 19, 2005.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    I'm playing around with the new Executor class in the
    java.util.concurrent package and I've run into a bit of a problem. This
    is probably simple, but I don't immediately see how to do it.

    I send several runnables to the the Executor (on the order of a hundred
    or more) to be run with as many threads as the caller has specified in
    creating the class. So far, so good. Now I want to sit and wait until
    the Executor has finished it all up before I go onto the next step.

    Now, I know I could do this with a counter and notify, but that seems
    like it shouldn't be necessary. Does the java.util.concurrent package
    provide any way for me to be notified when the Executor is done with its
    work?

    Thanks of any help you can provide.


    Here's the relevant method. If you notice anything else that could use
    improvement here, please feel free to point it out. This is a small
    portion of a homework assignment, so no total rewrites please.



    private Chromosome[] oneIteration(final Chromosome population[],
    final double crossoverProbability,
    final double mutationProbability) {

    initializeSelection(population);

    Executor executor = Executors.newFixedThreadPool(threads);

    // Get the next population
    final Chromosome newPopulation[] = new Chromosome[population.length];

    for (int index = 0; index < population.length; index += 2) {
    // Tell the thread pool to handle each set of two Chromosomes.
    final int finalIndex = index;

    executor.execute(new Runnable() {
    public void run() {
    // Select
    newPopulation[finalIndex] = population[select()];
    newPopulation[finalIndex + 1] = population[select()];

    // Crossover
    if (generator.nextDouble() < crossoverProbability) {
    Chromosome children[] =
    newPopulation[finalIndex].crossover(
    newPopulation[finalIndex + 1]);
    newPopulation[finalIndex] = children[0];
    newPopulation[finalIndex+1] = children[1];
    }

    // Mutate
    newPopulation[finalIndex]
    = Chromosome.mutate(newPopulation[finalIndex],
    mutationProbability, generator);
    newPopulation[finalIndex+1]
    = Chromosome.mutate(newPopulation[finalIndex + 1],
    mutationProbability, generator);
    }});
    }

    // Replace
    return newPopulation;
    }


    Thanks again,

    - --
    Kenneth P. Turvey <>
    http://kt.squeakydolphin.com (not much there yet)
    Jabber IM:
    Phone: (314) 255-2199
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDLy6h3naBnF2rJNURAiR5AJ9O9hn8bd4V4NMJLfSci7JMo/j5VgCbB7S+
    Kkz3vweBND0lsyOZWpjUZ38=
    =JTV4
    -----END PGP SIGNATURE-----
     
    Kenneth P. Turvey, Sep 19, 2005
    #1
    1. Advertising

  2. Kenneth P. Turvey

    Roedy Green Guest

    On Mon, 19 Sep 2005 16:33:44 -0500, "Kenneth P. Turvey"
    <> wrote or quoted :

    > new Executor class

    It is not a class just a trivial interface with one method --
    execute. So there is obviously nothing in Executor itself that does
    anything interesting.

    So the place to look for goodies is in the classes that implement it,
    namely ScheduledThreadPoolExecutor and ThreadPoolExecutor

    The problem is, how does the Executor know you don't plan to feed it
    more tasks?

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 20, 2005
    #2
    1. Advertising

  3. Kenneth P. Turvey

    Roedy Green Guest

    On Mon, 19 Sep 2005 16:33:44 -0500, "Kenneth P. Turvey"
    <> wrote or quoted :

    >playing around with the new Executor class


    I thought of a an app that would be great for exercising Executor --
    spidering a website looking for links, and chasing the links to see
    which ones are broken.

    You give each page a Runnable , and the Executor keeps the number of
    threads to a dull roar, reusing threads.

    In particular I would like you to look for <APPLET tags, and make sure
    the corresponding jar in there and the class in the applet tag is in
    the jar.

    Xenu is spectacular at spidering links, but throws up its hands at
    <Applet.

    See http://mindprod.com/jgloss/xenu.html
    http://mindprod.com/projects/htmlbrokenlink.html



    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 20, 2005
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Tue, 20 Sep 2005 01:40:51 +0000, Roedy Green wrote:

    > On Mon, 19 Sep 2005 16:33:44 -0500, "Kenneth P. Turvey"
    > <> wrote or quoted :
    >
    >> new Executor class

    > It is not a class just a trivial interface with one method --
    > execute. So there is obviously nothing in Executor itself that does
    > anything interesting.


    What I was really talking about is the java.util.concurrent package. I'm
    specifically using the Executor interface.

    > So the place to look for goodies is in the classes that implement it,
    > namely ScheduledThreadPoolExecutor and ThreadPoolExecutor


    I'll look around some more.

    > The problem is, how does the Executor know you don't plan to feed it
    > more tasks?


    That's a good point, but certainly there should be a way to see if it is
    busy? Shouldn't there be a way to check if the queue is empty? Maybe not.

    - --
    Kenneth P. Turvey <>
    http://kt.squeakydolphin.com (not much there yet)
    Jabber IM:
    Phone: (314) 255-2199
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDL7gv3naBnF2rJNURAkTTAJ9jneECzPkAb7KmH9YlKGey2lg3QgCgi1Pp
    0fNQ+YUYMbfem5Xgm8PG9OY=
    =/rws
    -----END PGP SIGNATURE-----
     
    Kenneth P. Turvey, Sep 20, 2005
    #4
  5. Kenneth P. Turvey

    Roedy Green Guest

    On Tue, 20 Sep 2005 02:20:42 -0500, "Kenneth P. Turvey"
    <> wrote or quoted :

    >That's a good point, but certainly there should be a way to see if it is
    >busy? Shouldn't there be a way to check if the queue is empty? Maybe not.


    Go look. Have a look at http://mindprod.com/jgloss/queue.html
    first for an overview, then focus on the promising blocking classes.
    Don't forget to look for methods in superclasses. I think you will be
    pleasantly surprised how rich the methods are.

    These interact with Executor's ThreadPoolExecutor

    Remember that Queues are all also Collections and inherit a method to
    discover the size for example..
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 20, 2005
    #5
  6. Kenneth P. Turvey

    Guest

    One of the most important new 'inventions' in java.util.concurrent
    frame work is
    Future object. You can check Future object returned from Executor's
    method
    to know the task's status and to get the Cllable task's return value.
     
    , Sep 20, 2005
    #6
  7. Kenneth P. Turvey

    Chris Uppal Guest

    Kenneth P. Turvey wrote:

    > I send several runnables to the the Executor (on the order of a hundred
    > or more) to be run with as many threads as the caller has specified in
    > creating the class. So far, so good. Now I want to sit and wait until
    > the Executor has finished it all up before I go onto the next step.
    >
    > Now, I know I could do this with a counter and notify, but that seems
    > like it shouldn't be necessary. Does the java.util.concurrent package
    > provide any way for me to be notified when the Executor is done with its
    > work?


    It appears that java.util.concurrent.CountDownLatch is intended for this kind
    of thing.

    -- chris
     
    Chris Uppal, Sep 20, 2005
    #7
  8. Kenneth P. Turvey

    Oliver Wong Guest

    "Kenneth P. Turvey" <> wrote in message
    news:p...
    >
    > What I was really talking about is the java.util.concurrent package. I'm
    > specifically using the Executor interface.
    >
    > On Tue, 20 Sep 2005 01:40:51 +0000, Roedy Green wrote:
    >>
    >> The problem is, how does the Executor know you don't plan to feed it
    >> more tasks?


    Take a look at java.util.concurrent.ExecutorService. Here's an excerpt
    from the JavaDocs.

    <quote>
    void shutdown()
    Initiates an orderly shutdown in which previously submitted tasks are
    executed, but no new tasks will be accepted. Invocation has no additional
    effect if already shut down.

    boolean isShutdown()
    Returns true if this executor has been shut down.

    boolean isTerminated()
    Returns true if all tasks have completed following shut down. Note that
    isTerminated is never true unless either shutdown or shutdownNow was called
    first.
    </quote>

    Also, from the javadocs for Executor:

    <quote>
    The Executor implementations provided in this package implement
    ExecutorService, which is a more extensive interface.
    </quote>

    So if you've been using Sun's implementation of Executor, switching to
    ExecutorService should be no problem.

    - Oliver
     
    Oliver Wong, Sep 20, 2005
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Tue, 20 Sep 2005 20:25:30 +0000, Oliver Wong wrote:

    > Take a look at java.util.concurrent.ExecutorService. Here's an
    > excerpt from the JavaDocs.

    [Snip]
    > So if you've been using Sun's implementation of Executor,
    > switching to ExecutorService should be no problem.


    Thanks. That's exactly what I'm looking for (or very close, I'll know
    in a minute :) ).

    - --
    Kenneth P. Turvey <>
    http://kt.squeakydolphin.com (not much there yet)
    Jabber IM:
    Phone: (314) 255-2199
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDMHIF3naBnF2rJNURApgVAJ44F4f/Sg+EFlPYshBt3E0RRQjw0QCfbLcD
    p6RZVts8B1IpbuqQQtCcIPs=
    =EXr3
    -----END PGP SIGNATURE-----
     
    Kenneth P. Turvey, Sep 20, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal

    Cancelling an Executor thread

    iksrazal, Jun 21, 2004, in forum: Java
    Replies:
    0
    Views:
    811
    iksrazal
    Jun 21, 2004
  2. Prafulla T
    Replies:
    5
    Views:
    585
    Chris Uppal
    Feb 7, 2007
  3. howa
    Replies:
    8
    Views:
    2,615
  4. Rakesh
    Replies:
    1
    Views:
    612
    Knute Johnson
    Oct 13, 2008
  5. Tom Anderson
    Replies:
    12
    Views:
    709
    Daniel Pitts
    Jan 21, 2010
Loading...

Share This Page