Holding threads after timeout (ThreadPoolExecutor)

P

pksiazek

Hi.

I'm trying to make sure that code won't be running longer then given
time.
This code is implemented in class MyThread and executed by Executor.

When code is executing too long, TimeoutException is thrown (see
example).
I caught it and try to run method once again.
But then RejectedExecutionException is thrown (which means thread pool
is empty).
If I wait a while before running loop once again (Thread.sleep) thread
is back in pool
and code is working properly.

What I should do to make sure that thread is back in pool and I can
use it again?

Regards

import java.util.concurrent.*;

public class MyClass {
protected static ThreadPoolExecutor executor;

public void go() throws Exception {
executor = new ThreadPoolExecutor(1, 1, 10000,
TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new
ThreadPoolExecutor.AbortPolicy());

for(int i = 0; i < 10; i++) {
Callable<Object> callable = new MyThread();
Future<Object> future = executor.submit(callable);
try {
future.get(200, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
//Thread.sleep(1000);
e.printStackTrace();
}
}
}

public static void main(String[] args) throws Exception {
new MyClass().go();
}

class MyThread implements Callable<Object> {

public Object call() throws Exception {
Thread.sleep(1000);
return null;
}

}

}
 
Z

Zig

Hi.

I'm trying to make sure that code won't be running longer then given
time.
This code is implemented in class MyThread and executed by Executor.

When code is executing too long, TimeoutException is thrown (see
example).
I caught it and try to run method once again.
But then RejectedExecutionException is thrown (which means thread pool
is empty).
If I wait a while before running loop once again (Thread.sleep) thread
is back in pool
and code is working properly.

What I should do to make sure that thread is back in pool and I can
use it again?

A) Use

future.cancel(true);

in your catch clause. This should request that the task be cancelled,
though there is no guarantee that the background thread will resume
picking elements out of the queue immediately.

B) The ThreadPoolExecutor constructors are available for very special
purpose crafting. Most of the time, you would want to use the Executors
class to create the pool for you. eg:

final ExecutorService executor=Executers.newSingleThreadExecutor();
try {
(your code here)
} finally {
executor.shutDown();
}

That should setup the constructor such that when you submit a task, it
will be queued until the background task has either finished it's current
task, or becomes aware that the background task was cancelled and discards
it.

HTH,

-Zig
 
P

pksiazek

B) The ThreadPoolExecutor constructors are available for very special
purpose crafting. Most of the time, you would want to use the Executors
class to create the pool for you. eg:

final ExecutorService executor=Executers.newSingleThreadExecutor();
try {
(your code here)} finally {

executor.shutDown();

}
This is exectly what I was looking for.
Thanks a lot.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top