Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Executor

Reply
Thread Tools

Executor

 
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      09-19-2005
-----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 <kt->
http://kt.squeakydolphin.com (not much there yet)
Jabber IM:
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDLy6h3naBnF2rJNURAiR5AJ9O9hn8bd4V4NMJLfSci7 JMo/j5VgCbB7S+
Kkz3vweBND0lsyOZWpjUZ38=
=JTV4
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2005
On Mon, 19 Sep 2005 16:33:44 -0500, "Kenneth P. Turvey"
<kt-> 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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2005
On Mon, 19 Sep 2005 16:33:44 -0500, "Kenneth P. Turvey"
<kt-> 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.
 
Reply With Quote
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      09-20-2005
-----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"
> <kt-> 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 <kt->
http://kt.squeakydolphin.com (not much there yet)
Jabber IM:
Phone: (314) 255-2199
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDL7gv3naBnF2rJNURAkTTAJ9jneECzPkAb7KmH9YlKG ey2lg3QgCgi1Pp
0fNQ+YUYMbfem5Xgm8PG9OY=
=/rws
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2005
On Tue, 20 Sep 2005 02:20:42 -0500, "Kenneth P. Turvey"
<kt-> 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.
 
Reply With Quote
 
HGA03630@nifty.ne.jp
Guest
Posts: n/a
 
      09-20-2005
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.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      09-20-2005
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


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      09-20-2005
"Kenneth P. Turvey" <kt-> wrote in message
newsan.2005.09.20.07.20.41.849806@squeakydolphin .com...
>
> 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


 
Reply With Quote
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      09-20-2005
-----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 <kt->
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-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An Executor-like structure providing more than threads Tom Anderson Java 12 01-21-2010 11:51 PM
Is Executor class thread-safe Rakesh Java 1 10-13-2008 02:07 AM
Parallel processing using Executor? howa Java 8 08-15-2007 10:37 AM
Looking for Program Executor class in java Prafulla T Java 5 02-07-2007 07:05 PM
Cancelling an Executor thread iksrazal Java 0 06-21-2004 10:19 PM



Advertisments