P
Philipp Kraus
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
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