Example usage for java.lang ThreadGroup interrupt

List of usage examples for java.lang ThreadGroup interrupt

Introduction

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

Prototype

public final void interrupt() 

Source Link

Document

Interrupts all threads in this thread group.

Usage

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("new Group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();/*w w  w  .j  a v  a  2 s.  c  o m*/
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("Group");

    ThreadGroup newGroup = new ThreadGroup(group, "new group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();//from   w ww. j a v a  2  s . co m
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    ThreadGroup tg = new ThreadGroup("My Group");

    MyThread thrd = new MyThread(tg, "MyThread #1");
    MyThread thrd2 = new MyThread(tg, "MyThread #2");
    MyThread thrd3 = new MyThread(tg, "MyThread #3");

    thrd.start();//from w w w .  ja va  2s  .  c  om
    thrd2.start();
    thrd3.start();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in thread group.");

    Thread thrds[] = new Thread[tg.activeCount()];
    tg.enumerate(thrds);
    for (Thread t : thrds)
        System.out.println(t.getName());

    thrd.myStop();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in tg.");
    tg.interrupt();
}

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

/**
 * Destroy any ThreadGroups that are loaded by the application classloader
 *//*from  ww w . ja  v a 2s  .co 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.apache.xmlrpc.WebServer.java

/**
 * Listens for client requests until stopped.  Call {@link
 * #start()} to invoke this method, and {@link #shutdown()} to
 * break out of it./*from  w w w  .  j  a v a  2  s  .co  m*/
 *
 * @throws RuntimeException Generally caused by either an
 * <code>UnknownHostException</code> or <code>BindException</code>
 * with the vanilla web server.
 *
 * @see #start()
 * @see #shutdown()
 */
public void run() {
    try {
        while (listener != null) {
            try {
                Socket socket = serverSocket.accept();
                try {
                    socket.setTcpNoDelay(true);
                } catch (SocketException socketOptEx) {
                    System.err.println(socketOptEx);
                }

                if (allowConnection(socket)) {
                    Runner runner = getRunner();
                    runner.handle(socket);
                } else {
                    socket.close();
                }
            } catch (InterruptedIOException checkState) {
                // Timeout while waiting for a client (from
                // SO_TIMEOUT)...try again if still listening.
            } catch (Exception ex) {
                System.err.println("Exception in XML-RPC listener loop (" + ex + ").");
                if (XmlRpc.debug) {
                    ex.printStackTrace();
                }
            } catch (Error err) {
                System.err.println("Error in XML-RPC listener loop (" + err + ").");
                err.printStackTrace();
            }
        }
    } catch (Exception exception) {
        System.err.println("Error accepting XML-RPC connections (" + exception + ").");
        if (XmlRpc.debug) {
            exception.printStackTrace();
        }
    } finally {
        if (serverSocket != null) {
            try {
                serverSocket.close();
                if (XmlRpc.debug) {
                    System.out.print("Closed XML-RPC server socket");
                }
                serverSocket = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Shutdown our Runner-based threads
        if (runners != null) {
            ThreadGroup g = runners;
            runners = null;
            try {
                g.interrupt();
            } catch (Exception e) {
                System.err.println(e);
                e.printStackTrace();
            }
        }
    }
}

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

/**
 * {@inheritDoc}//from   w  w  w  . j  a  v  a  2 s  .  c om
 * 
 * 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}/*  w ww .j  a va2 s.co 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;
        }
    }
}