zoukankan      html  css  js  c++  java
  • 生成deamon线程的executor

    参考SwingWorker代码:

       /**
         * returns workersExecutorService.
         *
         * returns the service stored in the appContext or creates it if
         * necessary.
         *
         * @return ExecutorService for the {@code SwingWorkers}
         */
        private static synchronized ExecutorService getWorkersExecutorService() {
            final AppContext appContext = AppContext.getAppContext();
            ExecutorService executorService =
                (ExecutorService) appContext.get(SwingWorker.class);
            if (executorService == null) {
                //this creates daemon threads.
                ThreadFactory threadFactory =
                    new ThreadFactory() {
                        final ThreadFactory defaultFactory =
                            Executors.defaultThreadFactory();
                        public Thread newThread(final Runnable r) {
                            Thread thread =
                                defaultFactory.newThread(r);
                            thread.setName("SwingWorker-"
                                + thread.getName());
                            thread.setDaemon(true);
                            return thread;
                        }
                    };

                executorService =
                    new ThreadPoolExecutor(MAX_WORKER_THREADS, MAX_WORKER_THREADS,
                                           10L, TimeUnit.MINUTES,
                                           new LinkedBlockingQueue<Runnable>(),
                                           threadFactory);
                appContext.put(SwingWorker.class, executorService);

                // Don't use ShutdownHook here as it's not enough. We should track
                // AppContext disposal instead of JVM shutdown, see 6799345 for details
                final ExecutorService es = executorService;
                appContext.addPropertyChangeListener(AppContext.DISPOSED_PROPERTY_NAME,
                    new PropertyChangeListener() {
                        @Override
                        public void propertyChange(PropertyChangeEvent pce) {
                            boolean disposed = (Boolean)pce.getNewValue();
                            if (disposed) {
                                final WeakReference<ExecutorService> executorServiceRef =
                                    new WeakReference<ExecutorService>(es);
                                final ExecutorService executorService =
                                    executorServiceRef.get();
                                if (executorService != null) {
                                    AccessController.doPrivileged(
                                        new PrivilegedAction<Void>() {
                                            public Void run() {
                                                executorService.shutdown();
                                                return null;
                                            }
                                        }
                                    );
                                }
                            }
                        }
                    }
                );
            }
            return executorService;
        }
    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    (转)KMP算法实现。超级赞!见过的最容易理解的
    《越狱》观后感
    【Coursera】Security Introduction -Summary
    【Coursera】Security Introduction -Ninth Week(2)
    【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (2)其余内容
    【Coursera】Security Introduction -Eighth Week(2)
    【TCP/IP详解 卷一:协议】第十八章 TCP连接 的建立与终止 (1)三次握手,四次挥手
    【Coursera】Security Introduction -Eighth Week(1)
    【TCP/IP详解 卷一:协议】第十七章 TCP:传输控制协议
    【Coursera】Seventh Week
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2301143.html
Copyright © 2011-2022 走看看