aspose file tools*
The moose likes Threads and Synchronization and the fly likes ThreadPoolExecutor restart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadPoolExecutor restart" Watch "ThreadPoolExecutor restart" New topic
Author

ThreadPoolExecutor restart

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Suppose I added (submitted) some tasks (threads) into the ThreadPoolExecutor to be running. At some stage, I must ensure that the main thread to be blocked and all the threads in the ThreadPoolExecutor to be completed before releasing the lock on the main thread.

I spotted that I can use the combination of the following two methods on ThreadPoolExecutor:

shutdown();
awaitTermination();

That's great. Since after these two methods, the main thread can ensure all the threads in the pool have completed.

However, after the shutdown(), there is no way that you can add another task into the pool. (The pool is considered to be closed and shutdown). Where is the restart() method on the pool, I wonder.

The only workaround I found is to create a new instance of the ThreadPoolExecutor like the code below.

if (exec.isShutdown())
{
exec = (ThreadPoolExecutor)Executors.newCachedThreadPool();
}

Can anybody advise if this is a good practice to use ThreadPoolExecutor? It seems that everytime I perform a shutdown and awaitTermination, I need to create a new instance of ThreadPoolExecutor. I smells bad code.

Thanks


SCJP, SCJD, SCWCD, SCBCD, SCEA
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Jiafan,

maybe the check



is a better choice.

Cheers,
Ramon


Blackbelt on BlackBeltFactory.com.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
For your use case, I think it would be better to use invokeAll(), which will block until all tasks in the Collection you pass in have been completed or cancelled. That way you don't have to keep creating a whole new ThreadPoolExecutor for each set of tasks you need to wait for.
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Mike Simmons wrote:For your use case, I think it would be better to use invokeAll(), which will block until all tasks in the Collection you pass in have been completed or cancelled. That way you don't have to keep creating a whole new ThreadPoolExecutor for each set of tasks you need to wait for.


Thanks for your reply. However, invokeAll() does not suit my case. As soon as a task is added (submitted) into the TPE, I hope to start it immediately. I do not wait for another tasks to be added before starting these threads.

Now I think I have another solution to wait for all the running threads in the TPE, *without* shutting down the TPE and re-instantiate another instance of TPE.

Every time I add a new task into the TPE, I maintain a hashmap like below:


And if I want to wait for all the running threads to complete, I simply need to invoke the get() method on all the Future object like below:


The nature of the get() method waits for the thread to complete and get the result if necessary. (In my case, I don't have any return values). This definitely look like a better solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadPoolExecutor restart