Executor

K

Kenneth P. Turvey

-----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 <[email protected]>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: (e-mail address removed)
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDLy6h3naBnF2rJNURAiR5AJ9O9hn8bd4V4NMJLfSci7JMo/j5VgCbB7S+
Kkz3vweBND0lsyOZWpjUZ38=
=JTV4
-----END PGP SIGNATURE-----
 
R

Roedy Green

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?
 
R

Roedy Green

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
 
K

Kenneth P. Turvey

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

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 <[email protected]>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: (e-mail address removed)
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDL7gv3naBnF2rJNURAkTTAJ9jneECzPkAb7KmH9YlKGey2lg3QgCgi1Pp
0fNQ+YUYMbfem5Xgm8PG9OY=
=/rws
-----END PGP SIGNATURE-----
 
R

Roedy Green

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..
 
H

HGA03630

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.
 
C

Chris Uppal

Kenneth said:
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
 
O

Oliver Wong

Kenneth P. Turvey said:
What I was really talking about is the java.util.concurrent package. I'm
specifically using the Executor interface.

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
 
K

Kenneth P. Turvey

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

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 <[email protected]>
http://kt.squeakydolphin.com (not much there yet)
Jabber IM: (e-mail address removed)
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDMHIF3naBnF2rJNURApgVAJ44F4f/Sg+EFlPYshBt3E0RRQjw0QCfbLcD
p6RZVts8B1IpbuqQQtCcIPs=
=EXr3
-----END PGP SIGNATURE-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top