Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28088

thread pool with CachedThreadPool

From Philipp Kraus <philipp.kraus@flashpixx.de>
Newsgroups comp.lang.java.programmer
Subject thread pool with CachedThreadPool
Date 2014-04-15 10:47 +0200
Organization Clausthal University of Technology
Message-ID <liirm6$1vl6$1@ariadne.rz.tu-clausthal.de> (permalink)

Show all headers | View raw


Hello,

I try to use the Executors.newCachedThreadPool to create a pool with runnables:

class myPool
{

private boolean running = false;
ExecutorService pool = Executors.newCachedThreadPool();

public void start()
{
    running = true;
    for(int i=0; i < poolsize; i++)
	pool.submit( new myWorker(this) );
}

public void stop()
{
        running = false;
        pool.shutdown();
        try {
            m_pool.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException ex) {}
}

public boolean isRunning()
{
    return running;
}

}

class myWortker implements Runnable
{
     private myPool master = null;

     public myWorker( myPool pool ) { master = pool; }

     public void run()
     {
            while(master.isRunning)
            {
                  do a lot of work
            }
}

I have removed any synchronized calls to show the basic structure only. 
If I run
myPool pool = new myPool();
pool.start()
pool.stop();

everything works fine, all workers are started and all workers shut 
down. If I call after the stop() the start()
method again I get an exception 
"java.util.concurrent.RejectedExecutionException".

IMHO start() should create all workers and stop() should shutdown the 
pool and a new start call should create the pool with workers again. 
Did I missing anything? I think I don't understand the pool logic in a 
correct manner. Can anybody explain me my mistake?

Thanks a lot

Phil

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

thread pool with CachedThreadPool Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-04-15 10:47 +0200
  Re: thread pool with CachedThreadPool Joerg Meier <joergmmeier@arcor.de> - 2014-04-15 12:02 +0200
    Re: thread pool with CachedThreadPool Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-04-15 12:40 +0200
      Re: thread pool with CachedThreadPool Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-04-15 15:05 -0700
        Re: thread pool with CachedThreadPool Robert Klemme <shortcutter@googlemail.com> - 2014-04-16 08:43 +0200
          Re: thread pool with CachedThreadPool Josip Almasi <joe@vrspace.org> - 2014-04-16 12:42 +0200
            Re: thread pool with CachedThreadPool Robert Klemme <shortcutter@googlemail.com> - 2014-04-16 17:55 +0200
              Re: thread pool with CachedThreadPool Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2014-04-16 22:11 -0700
                Re: thread pool with CachedThreadPool Josip Almasi <joe@vrspace.org> - 2014-04-17 12:12 +0200

csiph-web