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