Note that there are some explanatory texts on larger screens.

plurals
  1. POThreadPoolExecutor Exception Notification
    primarykey
    data
    text
    <p>Can you please help me validating this piece of code. I am trying to perform some file download task simultaneously. And later if all the download are successfully only then i am suppose to post-process them. If any of the task failed i should be throwing the exception to the caller, as i don't want to post process semi successful results.</p> <p>Here is my custom ThreadPoolExecutor where i try to check for any exceptions from any thread in the pool</p> <pre><code>public class CustomExecutorePool extends ThreadPoolExecutor { private AtomicBoolean errorFlag = new AtomicBoolean(false); public CustomExecutorePool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue&lt;Runnable&gt; workQueue,ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); } @Override protected void afterExecute(Runnable r, Throwable childThreadException){ if(null != childThreadException){ logger.error("Exception occurred in one of the child threads: &gt;&gt;&gt; " + childThreadException.getMessage() ); errorFlag.set(true); } } public AtomicBoolean isErrorPresent(){ return errorFlag; } </code></pre> <p>As you can see i try to use an AtmociBoolean flag and set it to true if any of the child thread has an exception.</p> <p>And here is the snippet from my main class where i try to use this information :</p> <pre><code>ThreadPoolExecutor docFetchingThreadPool = new CustomExecutorePool(...) try { // populate the thread pool queue for all the runnables to fetch documents for(Document myDoc : list){ docFetchingThreadPool.execute(myDoc); } logger.info("All docs added to the work queue"); docFetchingThreadPool.shutdown(); logger.info("Waiting to finish work queue task for 3 mins"); docFetchingThreadPool.awaitTermination(3, RETREIVAL_WAIT_UNIT); if(docFetchingThreadPool.isTerminated() &amp;&amp; !(docFetchingThreadPool.isErrorPresent().get()){ //sucessfull post-process all docs now //...... }else { throw new MyException("Exception occured in fetching"); } } catch (InterruptedException e) { logger.error("Interrupt exception received ", e); Thread.currentThread().interrupt(); throw new MyException("Interrupt exception"); } finally { if(null != docFetchingThreadPool){ logger.info("Force shoutdown for residual threads if any..."); docFetchingThreadPool.shutdownNow(); } } </code></pre> <p>Can you please let me know if this looks okay, or will the error boolean flag will have any synronization issues or is this even the correct way to go about. I am aware of using the invokeall function on the executor service, but still debating which one will be a cleaner approach for handling any exception scenarios.</p> <p>Thanks for your time,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload