Example usage for java.lang ThreadGroup isDestroyed

List of usage examples for java.lang ThreadGroup isDestroyed

Introduction

In this page you can find the example usage for java.lang ThreadGroup isDestroyed.

Prototype

public synchronized boolean isDestroyed() 

Source Link

Document

Tests if this thread group has been destroyed.

Usage

From source file:Main.java

public ThreadGroupDemo() {
    try {//from  w w w  .  j  ava  2s  .  co  m
        ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

        ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

        Thread t1 = new Thread(pGroup, this);
        System.out.println("Starting " + t1.getName());
        t1.start();

        Thread t2 = new Thread(cGroup, this);
        System.out.println("Starting " + t2.getName());
        t2.start();

        t1.join();
        t2.join();

        if (!cGroup.isDestroyed()) {
            cGroup.destroy();
        } else {
            System.out.println(cGroup.getName() + " destroyed");
        }

        // parent group destroyed
        if (!pGroup.isDestroyed()) {
            pGroup.destroy();
        } else {
            System.out.println(pGroup.getName() + " destroyed");
        }

    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Destroy any ThreadGroups that are loaded by the application classloader
 *//*w ww  .ja  v  a 2 s  .  c o m*/
public void destroyThreadGroups() {
    try {
        ThreadGroup systemThreadGroup = Thread.currentThread().getThreadGroup();
        while (systemThreadGroup.getParent() != null) {
            systemThreadGroup = systemThreadGroup.getParent();
        }
        // systemThreadGroup should now be the topmost ThreadGroup, "system"

        int enumeratedGroups;
        ThreadGroup[] allThreadGroups;
        int noOfGroups = systemThreadGroup.activeGroupCount(); // Estimate no of groups
        do {
            noOfGroups += 10; // Make room for 10 extra
            allThreadGroups = new ThreadGroup[noOfGroups];
            enumeratedGroups = systemThreadGroup.enumerate(allThreadGroups);
        } while (enumeratedGroups >= noOfGroups); // If there was not room for all groups, try again

        for (ThreadGroup threadGroup : allThreadGroups) {
            if (isLoadedInWebApplication(threadGroup) && !threadGroup.isDestroyed()) {
                warn("ThreadGroup '" + threadGroup + "' was loaded inside application, needs to be destroyed");

                int noOfThreads = threadGroup.activeCount();
                if (noOfThreads > 0) {
                    warn("There seems to be " + noOfThreads + " running in ThreadGroup '" + threadGroup
                            + "'; interrupting");
                    try {
                        threadGroup.interrupt();
                    } catch (Exception e) {
                        error(e);
                    }
                }

                try {
                    threadGroup.destroy();
                    info("ThreadGroup '" + threadGroup + "' successfully destroyed");
                } catch (Exception e) {
                    error(e);
                }
            }
        }
    } catch (Exception ex) {
        error(ex);
    }
}

From source file:org.eclipse.gemini.blueprint.extender.internal.support.ExtenderConfiguration.java

/**
 * {@inheritDoc}/*from   w ww  .j ava2s . com*/
 * 
 * Cleanup the configuration items.
 */
public void stop(BundleContext extenderBundleContext) {

    synchronized (lock) {
        if (isMulticasterManagedInternally) {
            eventMulticaster.removeAllListeners();
            eventMulticaster = null;
        }

        if (extenderConfiguration != null) {
            extenderConfiguration.close();
            extenderConfiguration = null;
        }

        // postpone the task executor shutdown
        if (forceThreadShutdown) {

            if (isTaskExecutorManagedInternally) {
                log.warn("Forcing the (internally created) taskExecutor to stop...");
                ThreadGroup th = ((SimpleAsyncTaskExecutor) taskExecutor).getThreadGroup();
                if (!th.isDestroyed()) {
                    // ask the threads nicely to stop
                    th.interrupt();
                }
            }
            taskExecutor = null;
        }

        if (isShutdownTaskExecutorManagedInternally) {
            try {
                ((DisposableBean) shutdownTaskExecutor).destroy();
            } catch (Exception ex) {
                log.debug("Received exception while shutting down shutdown task executor", ex);
            }
            shutdownTaskExecutor = null;
        }
    }
}

From source file:org.springframework.osgi.extender.internal.support.ExtenderConfiguration.java

/**
 * {@inheritDoc}/*from  w  w  w  .  ja  v  a  2  s  .  c  o  m*/
 * 
 * Cleanup the configuration items.
 */
public void destroy() {

    synchronized (lock) {
        if (isMulticasterManagedInternally) {
            eventMulticaster.removeAllListeners();
            eventMulticaster = null;
        }

        if (extenderConfiguration != null) {
            extenderConfiguration.close();
            extenderConfiguration = null;
        }

        // postpone the task executor shutdown
        if (forceThreadShutdown) {

            if (isTaskExecutorManagedInternally) {
                log.warn("Forcing the (internally created) taskExecutor to stop...");
                ThreadGroup th = ((SimpleAsyncTaskExecutor) taskExecutor).getThreadGroup();
                if (!th.isDestroyed()) {
                    // ask the threads nicely to stop
                    th.interrupt();
                }
            }
            taskExecutor = null;
        }

        if (isShutdownTaskExecutorManagedInternally) {
            try {
                ((DisposableBean) shutdownTaskExecutor).destroy();
            } catch (Exception ex) {
                log.debug("Received exception while shutting down shutdown task executor", ex);
            }
            shutdownTaskExecutor = null;
        }
    }
}