Example usage for java.lang SecurityManager getThreadGroup

List of usage examples for java.lang SecurityManager getThreadGroup

Introduction

In this page you can find the example usage for java.lang SecurityManager getThreadGroup.

Prototype

public ThreadGroup getThreadGroup() 

Source Link

Document

Returns the thread group into which to instantiate any new thread being created at the time this is being called.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.setProperty("java.security.policy", "file:/C:/java.policy");

    SecurityManager sm = new SecurityManager();

    System.setSecurityManager(sm);

    ThreadGroup con = sm.getThreadGroup();

    System.out.println(con);/*  www.j a  v  a 2  s.  c o  m*/

}

From source file:Main.java

public static ThreadGroup createThreadGroup(String name) {
    SecurityManager s = System.getSecurityManager();
    ThreadGroup parent = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
    return new ThreadGroup(parent, name);
}

From source file:com.ery.estorm.util.Threads.java

/**
 * Returns a {@link java.util.concurrent.ThreadFactory} that names each created thread uniquely, with a common prefix.
 * //  www  .  java2 s  . co m
 * @param prefix
 *            The prefix of every created Thread's name
 * @return a {@link java.util.concurrent.ThreadFactory} that names threads
 */
public static ThreadFactory getNamedThreadFactory(final String prefix) {
    SecurityManager s = System.getSecurityManager();
    final ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();

    return new ThreadFactory() {
        final AtomicInteger threadNumber = new AtomicInteger(1);
        private final int poolNumber = Threads.poolNumber.getAndIncrement();
        final ThreadGroup group = threadGroup;

        @Override
        public Thread newThread(Runnable r) {
            final String name = prefix + "-pool" + poolNumber + "-t" + threadNumber.getAndIncrement();
            return new Thread(group, r, name);
        }
    };
}

From source file:com.newlandframework.rpc.parallel.NamedThreadFactory.java

public NamedThreadFactory(String prefix, boolean daemo) {
    this.prefix = StringUtils.isNotEmpty(prefix) ? prefix + "-thread-" : "";
    daemoThread = daemo;/*from w  w  w .  j a v  a2  s  .c  om*/
    SecurityManager s = System.getSecurityManager();
    threadGroup = (s == null) ? Thread.currentThread().getThreadGroup() : s.getThreadGroup();
}

From source file:net.pms.util.BasicThreadFactory.java

/**
 * Creates a new {@link BasicThreadFactory} using the given arguments.
 * <p>//from   w  ww . ja va  2  s.  c o  m
 * The {@link Thread} names generated by the new {@link BasicThreadFactory}
 * is created by calling {@link String#format} with {@code namePattern} as
 * the "format" and pool- and thread number as arguments. The formatting
 * rules are those of {@link java.util.Formatter}.
 * <p>
 * No more than two variables of type {@code %d} or {@code %s} is allowed in
 * {@code namePattern}, and they will be substituted as follows:
 * <ul>
 * <li>No variables: All {@link Thread} names generated by this
 * {@link ThreadFactory} will be equal to {@code namePattern}.</li>
 * <li>One variable: Only thread number will be used, pool number isn't
 * used.</li>
 * <li>Two variables: Pool number will be used for the first variable in
 * {@code namePattern}, thread number for the second.
 * </ul>
 *
 * @param namePattern The {@link java.util.Formatter} formatted
 *            {@link String} from which to generate {@link Thread} names.
 * @param threadPriority The {@link Thread} priority.
 * @param group The {@link ThreadGroup}.
 */
public BasicThreadFactory(String namePattern, int threadPriority, ThreadGroup group) {
    if (isBlank(namePattern)) {
        throw new IllegalArgumentException("namePattern cannot be blank");
    }
    if (group == null) {
        SecurityManager securityManager = System.getSecurityManager();
        group = (securityManager != null) ? securityManager.getThreadGroup()
                : Thread.currentThread().getThreadGroup();
    }
    this.group = group;
    this.threadPriority = Math.min(Thread.MAX_PRIORITY, Math.max(Thread.MIN_PRIORITY, threadPriority));
    int pctSes = 0;
    int pctDs = 0;
    int i = 0;
    while (true) {
        i = namePattern.indexOf("%s", i);
        if (i >= 0) {
            pctSes++;
            i++;
        } else {
            break;
        }
    }
    while (true) {
        i = namePattern.indexOf("%d", i);
        if (i >= 0) {
            pctDs++;
            i++;
        } else {
            break;
        }
    }
    if (pctSes + pctDs > 2) {
        throw new IllegalArgumentException("namePattern can't have more than 2 variables");
    }
    this.numVariables = pctSes + pctDs;
    this.namePattern = namePattern;
    if (numVariables == 2) {
        this.instancePoolNumber = POOL_NUMBER.getAndIncrement();
    } else {
        this.instancePoolNumber = 0;
    }
}

From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java

/**
 * Returns a {@link ThreadFactory} that names each created thread uniquely,
 * with a common prefix./*from   w ww .jav  a 2 s .  c  o m*/
 * @param prefix The prefix of every created Thread's name
 * @return a {@link ThreadFactory} that names threads
 */
public static ThreadFactory getNamedThreadFactory(final String prefix) {
    SecurityManager s = System.getSecurityManager();
    final ThreadGroup threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();

    return new ThreadFactory() {
        final AtomicInteger threadNumber = new AtomicInteger(1);
        private final int poolNum = poolNumber.getAndIncrement();
        final ThreadGroup group = threadGroup;

        @Override
        public Thread newThread(Runnable r) {
            final String name = prefix + "-pool" + poolNum + "-t" + threadNumber.getAndIncrement();
            return new Thread(group, r, name);
        }
    };
}

From source file:org.hyperic.hq.measurement.agent.server.ScheduleThread.java

private ThreadFactory getFactory(final String plugin) {
    final SecurityManager s = System.getSecurityManager();
    final ThreadGroup group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
    return new ThreadFactory() {
        private final AtomicLong num = new AtomicLong();

        public Thread newThread(Runnable r) {
            final Thread rtn = new Thread(group, r);
            rtn.setDaemon(true);//from www . j  ava  2  s  . com
            rtn.setName(plugin + "-" + num.getAndIncrement());
            return rtn;
        }
    };
}

From source file:org.rhq.core.pc.util.LoggingThreadFactory.java

/**
 * Creates a factory that will produce either daemon or non-daemon threads. Be careful if you pass in <code>
 * false</code> for the <code>daemon</code> parameter; if you do, make sure your thread pool and all its threads
 * created by this factory are properly shutdown; otherwise you could prevent the VM from exiting properly.
 *
 * @param poolName the name of the thread pool that will be using this factory
 * @param daemon   if <code>true</code>, the factory will create daemon threads; <code>false</code> for non-daemon
 *                 threads//  w  ww  . ja  v a  2 s  .com
 */
public LoggingThreadFactory(String poolName, boolean daemon) {
    this.poolName = poolName;
    this.log = LogFactory.getLog("org.rhq.threadpools." + poolName);
    this.daemon = daemon;

    SecurityManager s = System.getSecurityManager();
    group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
}

From source file:org.rhq.enterprise.server.util.LoggingThreadFactory.java

/**
 * Creates a factory that will produce either daemon or non-daemon threads. Be careful if you pass in <code>
 * false</code> for the <code>daemon</code> parameter; if you do, make sure your thread pool and all its threads
 * created by this factory are properly shutdown; otherwise you could prevent the VM from exiting properly.
 *
 * @param poolName the name of the thread pool that will be using this factory
 * @param daemon   if <code>true</code>, the factory will create daemon threads; <code>false</code> for non-daemon
 *                 threads//from  w ww  .j  a v  a2s . c  o  m
 */
public LoggingThreadFactory(String poolName, boolean daemon) {
    this.poolName = poolName;
    this.log = LogFactory.getLog("org.rhq.server.threadpools." + poolName);
    this.daemon = daemon;

    SecurityManager s = System.getSecurityManager();
    group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
}

From source file:pt.lsts.neptus.util.logdownload.LogsDownloaderWorkerUtil.java

/**
 * Creates a {@link ScheduledThreadPoolExecutor} for use on {@link LogsDownloaderWorker}.
 * //from   w  ww  .j a va 2 s  .c  om
 * @param caller
 * @return
 */
static ScheduledThreadPoolExecutor createThreadPool(LogsDownloaderWorker caller) {
    ScheduledThreadPoolExecutor ret = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(4,
            new ThreadFactory() {
                private ThreadGroup group;
                private long count = 0;
                {
                    SecurityManager s = System.getSecurityManager();
                    group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
                }

                @Override
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(group, r);
                    t.setName(caller.getClass().getSimpleName() + "::" + Integer.toHexString(caller.hashCode())
                            + "::" + count++);
                    t.setDaemon(true);
                    return t;
                }
            });

    return ret;
}