[concurrency-interest] Re: Help, how to design a pool of threads

Andrej Navodnik andrejnavodnik at yahoo.com
Sat Nov 5 12:01:15 EST 2005


Hello Joe,

thank you very much for your reply. I will carefuly
consider all your suggestions.

Back to my homework...
Andrei

--- Joe Bowbeer <joe.bowbeer at gmail.com> wrote:

> First, I'm trying to understand the threading
> scheme.  It looks like
> there is a producer that submits worker tasks to the
> pool and a
> consumer that processes the results.  I suggest you
> move the producer
> and consumer into the same thread.  Perhaps this
> could be the main
> thread.  That is, after you create the service, do
> the producer thing
> in the main thread next, submitting the worker tasks
> to the service;
> then do the consumer thing, processing the results. 
> If the main
> thread won't do, move the producer and consumer to a
> single background
> thread.
> 
> Moving the producer and consumer into a single
> thread leaves the
> thread pool dedicated to the worker tasks.  Easier
> to manage that
> way...
> 
> > My questions are:
> 
> > - how should I design my application so that the
> tasks
> > in the pool would be canceled/stopped after the
> > optimal element is found except for the task
> > that has found that "optimal element" - I
> wouldn't
> > like to see the message �Task is still doing
> useless
> > things� because it's actually using resources
> that
> > should be used fior preparing the final result;
> 
> FutureTasks are cancelled by calling
> task.cancel(true);
> 
> If the task is still in the queue, this will prevent
> the task from
> ever running.  If the task is already actively
> running, this will also
> interrupt the thread that's running the task.
> 
> But the task must be responsive to interrupts.  That
> is, when
> interrupted, the task should promptly quit.  If the
> task could be
> waiting on a lock when it is interrupted, I
> recommend using
> lockInterruptibly().  If the task sleeps, then don't
> catch the
> InterruptedException, just let the task (ie,
> Callable.call) throw
> InterruptedException.  Finally, if the task is
> looping without
> blocking then it can check isCancelled() to see if
> it's been
> cancelled.
> 
> Your cancelRunnables method removes tasks from the
> service's queue but
> does not attempt to cancel running tasks.  Would it
> work better to
> cancel all the tasks in the workers list?  If the
> service is dedicated
> to workers, though, then service.shutdownNow will do
> this for you.  It
> cancels all active tasks.
> 
> > - should I concern about this problem or not?
> 
> Yes.
> 
> > - are there any other constructs that I could
> > use and achieve the same result?
> >
> 
> Use a completion service work for the consumer
> thing?
> 
> Joe.



	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com


More information about the Concurrency-interest mailing list