jdk/src/share/classes/com/sun/jmx/mbeanserver/PerThreadGroupPool.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     1 /*
       
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.mbeanserver;
       
    27 
       
    28 import java.lang.ref.WeakReference;
       
    29 import java.util.concurrent.ThreadPoolExecutor;
       
    30 
       
    31 /**
       
    32  * <p>A factory for ThreadPoolExecutor objects that allows the same object to
       
    33  * be shared by all users of the factory that are in the same ThreadGroup.</p>
       
    34  */
       
    35 // We return a ThreadPoolExecutor rather than the more general ExecutorService
       
    36 // because we need to be able to call allowCoreThreadTimeout so that threads in
       
    37 // the pool will eventually be destroyed when the pool is no longer in use.
       
    38 // Otherwise these threads would keep the ThreadGroup alive forever.
       
    39 public class PerThreadGroupPool<T extends ThreadPoolExecutor> {
       
    40     private final WeakIdentityHashMap<ThreadGroup, WeakReference<T>> map =
       
    41             WeakIdentityHashMap.make();
       
    42 
       
    43     public static interface Create<T extends ThreadPoolExecutor> {
       
    44         public T createThreadPool(ThreadGroup group);
       
    45     }
       
    46 
       
    47     private PerThreadGroupPool() {}
       
    48 
       
    49     public static <T extends ThreadPoolExecutor> PerThreadGroupPool<T> make() {
       
    50         return new PerThreadGroupPool<T>();
       
    51     }
       
    52 
       
    53     public synchronized T getThreadPoolExecutor(Create<T> create) {
       
    54         // Find out if there's already an existing executor for the calling
       
    55         // thread and reuse it. Otherwise, create a new one and store it in
       
    56         // the executors map. If there is a SecurityManager, the group of
       
    57         // System.getSecurityManager() is used, else the group of the calling
       
    58         // thread.
       
    59         SecurityManager s = System.getSecurityManager();
       
    60         ThreadGroup group = (s != null) ? s.getThreadGroup() :
       
    61             Thread.currentThread().getThreadGroup();
       
    62         WeakReference<T> wr = map.get(group);
       
    63         T executor = (wr == null) ? null : wr.get();
       
    64         if (executor == null) {
       
    65             executor = create.createThreadPool(group);
       
    66             executor.allowCoreThreadTimeOut(true);
       
    67             map.put(group, new WeakReference<T>(executor));
       
    68         }
       
    69         return executor;
       
    70     }
       
    71 }