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