java ThreadPoolExecutor exception caught

First, create a thread pool

among them:

public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)

Specific logic saturation policy enforcement.

protected void afterExecute(Runnable r, Throwable t)

After the specified logical exception.

package com.kintech.scanAF.common;
 
import com.kintech.common.utils.log.LogerHelper;
import java.util.concurrent.*;
 
/**
 * @author Tyler
 * @date 2019/9/12
 */
public class ThreadHelper {
    //初始化线程池
    private static final ExecutorService pool = new ThreadPoolExecutor(
            2, 
            5, 
            60,
            TimeUnit.SECONDS, 
            new ArrayBlockingQueue<Runnable>(10),
            Executors.defaultThreadFactory(), 
            new ThreadPoolExecutor.DiscardPolicy(){
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            LogerHelper.Write("--- "+this.getClass().getName()+"\r\n--- 队列已满,请稍后再来");
        }
    })
    {
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            super.afterExecute(r, t);
            LogerHelper.Write(t.getMessage());
            System.out.println(t.getMessage());
        }
    };
 
    /**
     * The thread pool method (method RunnableFunc folder) 
     * @param RUN
      * / 
    public  static  void Execute (the Runnable RUN) 
    { 
        pool.execute (RUN); 
    } 
    / ** 
     * execute thread pool method (Method folder RunnableFunc in) 
     * @param RUN
      * / 
    public  static Future <?> Submit (the Runnable RUN) 
    { 
        Future Future = <?> pool.submit (RUN);
         return Future; 
    } 
 
}

Thread pool parameters introduced:

public ThreadPoolExecutor (
   int corePoolSize, // number of threads 
  int maximumPoolSize, // maximum number of threads 
  Long keepAliveTime, // thread survival time 
  TimeUnit Unit, // time unit 
  BlockingQueue <Runnable> workQueue, // task queue 
  ThreadFactory threadFactory, // thread creation factory, you can thread a name 
  RejectedExecutionHandler Handler) // saturation strategy

Second, create a task

package com.kintech.scanAF.common.RunnableFunc;
 
 
/**
 * @author Tyler
 * @date 2019/9/12
 */
public class Test implements Runnable {
    private String a;
    public Test(String a)
    {
        this.a=a;
    }
 
    @Override
    public void run() {
        try
        {
            throw new RuntimeException( "Service has error !");
        }
        catch (Exception e) {
            throw e;
        }
        finally
        {
            a=null;
        }
 
    }
}

Third, call and get an exception

public void MainTest(String a) throws IOException {
        Future<?> future = ThreadHelper.submit(new Test(a));
        try {
            future.get();
        }
        catch (Exception ex)
        {
//记录日志
            LogerHelper.Write(ex.getMessage());
//swing弹窗
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Message", JOptionPane.ERROR_MESSAGE);
        }
 
    }

 

Guess you like

Origin www.cnblogs.com/hanjun0612/p/11542046.html