< prev index next >

src/java.management/share/classes/javax/management/monitor/Monitor.java

Print this page

1485      */
1486     private class MonitorTask implements Runnable {
1487 
1488         private ThreadPoolExecutor executor;
1489 
1490         /*
1491          * ------------------------------------------
1492          *  CONSTRUCTORS
1493          * ------------------------------------------
1494          */
1495 
1496         public MonitorTask() {
1497             // Find out if there's already an existing executor for the calling
1498             // thread and reuse it. Otherwise, create a new one and store it in
1499             // the executors map. If there is a SecurityManager, the group of
1500             // System.getSecurityManager() is used, else the group of the thread
1501             // instantiating this MonitorTask, i.e. the group of the thread that
1502             // calls "Monitor.start()".
1503             @SuppressWarnings("removal")
1504             SecurityManager s = System.getSecurityManager();

1505             ThreadGroup group = (s != null) ? s.getThreadGroup() :
1506                 Thread.currentThread().getThreadGroup();
1507             synchronized (executorsLock) {
1508                 for (ThreadPoolExecutor e : executors.keySet()) {
1509                     DaemonThreadFactory tf =
1510                             (DaemonThreadFactory) e.getThreadFactory();

1511                     ThreadGroup tg = tf.getThreadGroup();
1512                     if (tg == group) {
1513                         executor = e;
1514                         break;
1515                     }
1516                 }
1517                 if (executor == null) {
1518                     executor = new ThreadPoolExecutor(
1519                             maximumPoolSize,
1520                             maximumPoolSize,
1521                             60L,
1522                             TimeUnit.SECONDS,
1523                             new LinkedBlockingQueue<Runnable>(),
1524                             new DaemonThreadFactory("ThreadGroup<" +
1525                             group.getName() + "> Executor", group));
1526                     executor.allowCoreThreadTimeOut(true);
1527                     executors.put(executor, null);
1528                 }
1529             }
1530         }

1580 
1581     /**
1582      * Daemon thread factory used by the monitor executors.
1583      * <P>
1584      * This factory creates all new threads used by an Executor in
1585      * the same ThreadGroup. If there is a SecurityManager, it uses
1586      * the group of System.getSecurityManager(), else the group of
1587      * the thread instantiating this DaemonThreadFactory. Each new
1588      * thread is created as a daemon thread with priority
1589      * Thread.NORM_PRIORITY. New threads have names accessible via
1590      * Thread.getName() of "{@literal JMX Monitor <pool-name> Pool [Thread-M]}",
1591      * where M is the sequence number of the thread created by this
1592      * factory.
1593      */
1594     private static class DaemonThreadFactory implements ThreadFactory {
1595         final ThreadGroup group;
1596         final AtomicInteger threadNumber = new AtomicInteger(1);
1597         final String namePrefix;
1598         static final String nameSuffix = "]";
1599 

1600         public DaemonThreadFactory(String poolName) {
1601             @SuppressWarnings("removal")
1602             SecurityManager s = System.getSecurityManager();
1603             group = (s != null) ? s.getThreadGroup() :
1604                                   Thread.currentThread().getThreadGroup();
1605             namePrefix = "JMX Monitor " + poolName + " Pool [Thread-";
1606         }
1607 
1608         public DaemonThreadFactory(String poolName, ThreadGroup threadGroup) {
1609             group = threadGroup;
1610             namePrefix = "JMX Monitor " + poolName + " Pool [Thread-";
1611         }
1612 
1613         public ThreadGroup getThreadGroup() {
1614             return group;
1615         }
1616 
1617         public Thread newThread(Runnable r) {
1618             Thread t = new Thread(
1619                 group,
1620                 r,
1621                 namePrefix + threadNumber.getAndIncrement() + nameSuffix,

1485      */
1486     private class MonitorTask implements Runnable {
1487 
1488         private ThreadPoolExecutor executor;
1489 
1490         /*
1491          * ------------------------------------------
1492          *  CONSTRUCTORS
1493          * ------------------------------------------
1494          */
1495 
1496         public MonitorTask() {
1497             // Find out if there's already an existing executor for the calling
1498             // thread and reuse it. Otherwise, create a new one and store it in
1499             // the executors map. If there is a SecurityManager, the group of
1500             // System.getSecurityManager() is used, else the group of the thread
1501             // instantiating this MonitorTask, i.e. the group of the thread that
1502             // calls "Monitor.start()".
1503             @SuppressWarnings("removal")
1504             SecurityManager s = System.getSecurityManager();
1505             @SuppressWarnings("deprecation")
1506             ThreadGroup group = (s != null) ? s.getThreadGroup() :
1507                 Thread.currentThread().getThreadGroup();
1508             synchronized (executorsLock) {
1509                 for (ThreadPoolExecutor e : executors.keySet()) {
1510                     DaemonThreadFactory tf =
1511                             (DaemonThreadFactory) e.getThreadFactory();
1512                     @SuppressWarnings("deprecation")
1513                     ThreadGroup tg = tf.getThreadGroup();
1514                     if (tg == group) {
1515                         executor = e;
1516                         break;
1517                     }
1518                 }
1519                 if (executor == null) {
1520                     executor = new ThreadPoolExecutor(
1521                             maximumPoolSize,
1522                             maximumPoolSize,
1523                             60L,
1524                             TimeUnit.SECONDS,
1525                             new LinkedBlockingQueue<Runnable>(),
1526                             new DaemonThreadFactory("ThreadGroup<" +
1527                             group.getName() + "> Executor", group));
1528                     executor.allowCoreThreadTimeOut(true);
1529                     executors.put(executor, null);
1530                 }
1531             }
1532         }

1582 
1583     /**
1584      * Daemon thread factory used by the monitor executors.
1585      * <P>
1586      * This factory creates all new threads used by an Executor in
1587      * the same ThreadGroup. If there is a SecurityManager, it uses
1588      * the group of System.getSecurityManager(), else the group of
1589      * the thread instantiating this DaemonThreadFactory. Each new
1590      * thread is created as a daemon thread with priority
1591      * Thread.NORM_PRIORITY. New threads have names accessible via
1592      * Thread.getName() of "{@literal JMX Monitor <pool-name> Pool [Thread-M]}",
1593      * where M is the sequence number of the thread created by this
1594      * factory.
1595      */
1596     private static class DaemonThreadFactory implements ThreadFactory {
1597         final ThreadGroup group;
1598         final AtomicInteger threadNumber = new AtomicInteger(1);
1599         final String namePrefix;
1600         static final String nameSuffix = "]";
1601 
1602         @SuppressWarnings({"deprecation", "removal"})
1603         public DaemonThreadFactory(String poolName) {

1604             SecurityManager s = System.getSecurityManager();
1605             group = (s != null) ? s.getThreadGroup() :
1606                                   Thread.currentThread().getThreadGroup();
1607             namePrefix = "JMX Monitor " + poolName + " Pool [Thread-";
1608         }
1609 
1610         public DaemonThreadFactory(String poolName, ThreadGroup threadGroup) {
1611             group = threadGroup;
1612             namePrefix = "JMX Monitor " + poolName + " Pool [Thread-";
1613         }
1614 
1615         public ThreadGroup getThreadGroup() {
1616             return group;
1617         }
1618 
1619         public Thread newThread(Runnable r) {
1620             Thread t = new Thread(
1621                 group,
1622                 r,
1623                 namePrefix + threadNumber.getAndIncrement() + nameSuffix,
< prev index next >