首页

关于alibaba的dubbo源码包中ExecutorUtil线程执行工具类对线程池的处理源码示例

标签:alibaba,dubbo,ExecutorUtil,线程执行工具类,线程池     发布时间:2018-06-21   

一、前言

关于alibaba的dubbo源码包com.alibaba.dubbo.common.utils.ExecutorUtil执行器工具类,对线程池java.util.concurrent.ThreadPoolExecutor的执行器服务优化控制,详情参见源码说明。

二、源码说明

package com.alibaba.dubbo.common.utils;@b@@b@import com.alibaba.dubbo.common.Constants;@b@import com.alibaba.dubbo.common.URL;@b@  @b@import java.util.concurrent.Executor;@b@import java.util.concurrent.ExecutorService;@b@import java.util.concurrent.LinkedBlockingQueue;@b@import java.util.concurrent.ThreadPoolExecutor;@b@import java.util.concurrent.TimeUnit;@b@@b@public class ExecutorUtil { @b@@b@    private static final ThreadPoolExecutor shutdownExecutor = new ThreadPoolExecutor(0, 1,@b@            0L, TimeUnit.MILLISECONDS,@b@            new LinkedBlockingQueue<Runnable>(100),@b@            new NamedThreadFactory("Close-ExecutorService-Timer", true));@b@@b@    public static boolean isShutdown(Executor executor) {@b@        if (executor instanceof ExecutorService) {@b@            if (((ExecutorService) executor).isShutdown()) {@b@                return true;@b@            }@b@        }@b@        return false;@b@    }@b@@b@    public static void gracefulShutdown(Executor executor, int timeout) {@b@        if (!(executor instanceof ExecutorService) || isShutdown(executor)) {@b@            return;@b@        }@b@        final ExecutorService es = (ExecutorService) executor;@b@        try {@b@            es.shutdown(); // Disable new tasks from being submitted@b@        } catch (SecurityException ex2) {@b@            return;@b@        } catch (NullPointerException ex2) {@b@            return;@b@        }@b@        try {@b@            if (!es.awaitTermination(timeout, TimeUnit.MILLISECONDS)) {@b@                es.shutdownNow();@b@            }@b@        } catch (InterruptedException ex) {@b@            es.shutdownNow();@b@            Thread.currentThread().interrupt();@b@        }@b@        if (!isShutdown(es)) {@b@            newThreadToCloseExecutor(es);@b@        }@b@    }@b@@b@    public static void shutdownNow(Executor executor, final int timeout) {@b@        if (!(executor instanceof ExecutorService) || isShutdown(executor)) {@b@            return;@b@        }@b@        final ExecutorService es = (ExecutorService) executor;@b@        try {@b@            es.shutdownNow();@b@        } catch (SecurityException ex2) {@b@            return;@b@        } catch (NullPointerException ex2) {@b@            return;@b@        }@b@        try {@b@            es.awaitTermination(timeout, TimeUnit.MILLISECONDS);@b@        } catch (InterruptedException ex) {@b@            Thread.currentThread().interrupt();@b@        }@b@        if (!isShutdown(es)) {@b@            newThreadToCloseExecutor(es);@b@        }@b@    }@b@@b@    private static void newThreadToCloseExecutor(final ExecutorService es) {@b@        if (!isShutdown(es)) {@b@            shutdownExecutor.execute(new Runnable() {@b@                public void run() {@b@                    try {@b@                        for (int i = 0; i < 1000; i++) {@b@                            es.shutdownNow();@b@                            if (es.awaitTermination(10, TimeUnit.MILLISECONDS)) {@b@                                break;@b@                            }@b@                        }@b@                    } catch (InterruptedException ex) {@b@                        Thread.currentThread().interrupt();@b@                    } catch (Throwable e) { @b@                    }@b@                }@b@            });@b@        }@b@    }@b@@b@    /**@b@     * append thread name with url address@b@     *@b@     * @return new url with updated thread name@b@     */@b@    public static URL setThreadName(URL url, String defaultName) {@b@        String name = url.getParameter(Constants.THREAD_NAME_KEY, defaultName);@b@        name = new StringBuilder(32).append(name).append("-").append(url.getAddress()).toString();@b@        url = url.addParameter(Constants.THREAD_NAME_KEY, name);@b@        return url;@b@    }@b@}