[concurrency-interest] ThreadPoolTask

Peter Veentjer alarmnummer at gmail.com
Sat Jul 8 15:02:00 EDT 2006


Just for this purpose I created the Repeater (interface) and the
ThreadPoolRepeater (one of the implementations). The repeater keeps
repeating a single task over and over untill a new task is set or the
task is set to null. The ThreadPoolRepeater maintains a pool of
threads that keep repeating it. The pool can be increased or
decreased. And the ThreadPoolRepeater can be strict or non strict:
a strict repeater only has a single runnable that is run bij threads.
If a new runnable is set, it block untill all threads are finished
running the runnable.
The non strict repeater allows a new runnable to be set even while
other threads are still working on the old runnable.
And the setting of a new task can also be controller by providing
timeout pararameters (you don't want to block forever in some cases).

And one of the things I'm thinking about is making it easy to work
with delays/periods for those threads. And providing some kind of
context instead of using a threadlocal where threads can store state..

So I certainly think there are better ways to realise it :) Btw: this
library is going to be opensource in a few months,



On 7/8/06, Jean Morissette <jean.morissette at gmail.com> wrote:
> Hi,
>
> I need to create a pool of threads that run only one thead-safe task
> forever.  The task, which is a Runnable, must be passed in the
> constructor.  Also, the thread pool size should be resizable and have
> some lifecycling capabilities.
>
> I was thinking to create it on top of ThreadPoolExecutor, by setting
> corePoolSize to zero and by using a custom queue that is always full
> of the same task instance. So, by adjusting maxPoolSize, we can set
> the number of threads executing the task. Here are some code to show
> what I mean.
>
> public class ThreadPoolTask {
>
>         private ThreadPoolExecutor executor;
>
>         public ThreadPoolTask(Runnable task) {
>                 this.executor = new ThreadPoolExecutor(0, 1, 1, TimeUnit.SECONDS,
> new FullQueue(task));
>         }
>
>         public void setPoolSize(int size) {
>                 executor.setMaximumPoolSize(size);
>         }
>
>         public int getPoolSize() {
>                 return executor.getMaximumPoolSize();
>         }
>
>         private static class FullQueue implements BlockingQueue {
>                 // ...
>         }
> }
>
> What do you think?  Is there a more elegant way to implement my requirements?
>
> Thanks,
> Jean
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>


More information about the Concurrency-interest mailing list