List of usage examples for java.lang Thread stop
@Deprecated(since = "1.2") public final void stop()
From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java
/** * Partially inspired by org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads() */// w w w . j a v a 2s .co m @SuppressWarnings("deprecation") protected void stopThreads() { final Class<?> workerClass = findClass("java.util.concurrent.ThreadPoolExecutor$Worker"); final Field oracleTarget = findField(Thread.class, "target"); // Sun/Oracle JRE final Field ibmRunnable = findField(Thread.class, "runnable"); // IBM JRE for (Thread thread : getAllThreads()) { @SuppressWarnings("RedundantCast") final Runnable runnable = (oracleTarget != null) ? (Runnable) getFieldValue(oracleTarget, thread) : // Sun/Oracle JRE (Runnable) getFieldValue(ibmRunnable, thread); // IBM JRE if (thread != Thread.currentThread() && // Ignore current thread (isThreadInWebApplication(thread) || isLoadedInWebApplication(runnable))) { if (thread.getClass().getName().startsWith(JURT_ASYNCHRONOUS_FINALIZER)) { // Note, the thread group of this thread may be "system" if it is triggered by the Garbage Collector // however if triggered by us in forceStartOpenOfficeJurtCleanup() it may depend on the application server if (stopThreads) { info("Found JURT thread " + thread.getName() + "; starting " + JURTKiller.class.getSimpleName()); new JURTKiller(thread).start(); } else warn("JURT thread " + thread.getName() + " is still running in web app"); } else if (thread.getThreadGroup() != null && ("system".equals(thread.getThreadGroup().getName()) || // System thread "RMI Runtime".equals(thread.getThreadGroup().getName()))) { // RMI thread (honestly, just copied from Tomcat) if ("Keep-Alive-Timer".equals(thread.getName())) { thread.setContextClassLoader(getWebApplicationClassLoader().getParent()); debug("Changed contextClassLoader of HTTP keep alive thread"); } } else if (thread.isAlive()) { // Non-system, running in web app if ("java.util.TimerThread".equals(thread.getClass().getName())) { if (stopTimerThreads) { warn("Stopping Timer thread running in classloader."); stopTimerThread(thread); } else { info("Timer thread is running in classloader, but will not be stopped"); } } else { // If threads is running an java.util.concurrent.ThreadPoolExecutor.Worker try shutting down the executor if (workerClass != null && workerClass.isInstance(runnable)) { if (stopThreads) { warn("Shutting down " + ThreadPoolExecutor.class.getName() + " running within the classloader."); try { // java.util.concurrent.ThreadPoolExecutor, introduced in Java 1.5 final Field workerExecutor = findField(workerClass, "this$0"); final ThreadPoolExecutor executor = getFieldValue(workerExecutor, runnable); executor.shutdownNow(); } catch (Exception ex) { error(ex); } } else info(ThreadPoolExecutor.class.getName() + " running within the classloader will not be shut down."); } final String displayString = "'" + thread + "' of type " + thread.getClass().getName(); if (stopThreads) { final String waitString = (threadWaitMs > 0) ? "after " + threadWaitMs + " ms " : ""; warn("Stopping Thread " + displayString + " running in web app " + waitString); if (threadWaitMs > 0) { try { thread.join(threadWaitMs); // Wait for thread to run } catch (InterruptedException e) { // Do nothing } } // Normally threads should not be stopped (method is deprecated), since it may cause an inconsistent state. // In this case however, the alternative is a classloader leak, which may or may not be considered worse. if (thread.isAlive()) thread.stop(); } else { warn("Thread " + displayString + " is still running in web app"); } } } } } }
From source file:se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor.java
/** * Partially inspired by org.apache.catalina.loader.WebappClassLoader.clearReferencesThreads() *///from w w w . j av a 2 s . co m @SuppressWarnings("deprecation") protected void stopThreads() { final Class<?> workerClass = findClass("java.util.concurrent.ThreadPoolExecutor$Worker"); final Field oracleTarget = findField(Thread.class, "target"); // Sun/Oracle JRE final Field ibmRunnable = findField(Thread.class, "runnable"); // IBM JRE for (Thread thread : getAllThreads()) { final Runnable runnable = (oracleTarget != null) ? (Runnable) getFieldValue(oracleTarget, thread) : // Sun/Oracle JRE (Runnable) getFieldValue(ibmRunnable, thread); // IBM JRE if (thread != Thread.currentThread() && // Ignore current thread (isThreadInWebApplication(thread) || isLoadedInWebApplication(runnable))) { if (thread.getClass().getName().startsWith(JURT_ASYNCHRONOUS_FINALIZER)) { // Note, the thread group of this thread may be "system" if it is triggered by the Garbage Collector // however if triggered by us in forceStartOpenOfficeJurtCleanup() it may depend on the application server if (stopThreads) { info("Found JURT thread " + thread.getName() + "; starting " + JURTKiller.class.getSimpleName()); new JURTKiller(thread).start(); } else warn("JURT thread " + thread.getName() + " is still running in web app"); } else if (thread.getThreadGroup() != null && ("system".equals(thread.getThreadGroup().getName()) || // System thread "RMI Runtime".equals(thread.getThreadGroup().getName()))) { // RMI thread (honestly, just copied from Tomcat) if ("Keep-Alive-Timer".equals(thread.getName())) { thread.setContextClassLoader(getWebApplicationClassLoader().getParent()); debug("Changed contextClassLoader of HTTP keep alive thread"); } } else if (thread.isAlive()) { // Non-system, running in web app if ("java.util.TimerThread".equals(thread.getClass().getName())) { if (stopTimerThreads) { warn("Stopping Timer thread running in classloader."); stopTimerThread(thread); } else { info("Timer thread is running in classloader, but will not be stopped"); } } else { // If threads is running an java.util.concurrent.ThreadPoolExecutor.Worker try shutting down the executor if (workerClass != null && workerClass.isInstance(runnable)) { if (stopThreads) { warn("Shutting down " + ThreadPoolExecutor.class.getName() + " running within the classloader."); try { // java.util.concurrent.ThreadPoolExecutor, introduced in Java 1.5 final Field workerExecutor = findField(workerClass, "this$0"); final ThreadPoolExecutor executor = getFieldValue(workerExecutor, runnable); executor.shutdownNow(); } catch (Exception ex) { error(ex); } } else info(ThreadPoolExecutor.class.getName() + " running within the classloader will not be shut down."); } final String displayString = "'" + thread + "' of type " + thread.getClass().getName(); if (stopThreads) { final String waitString = (threadWaitMs > 0) ? "after " + threadWaitMs + " ms " : ""; warn("Stopping Thread " + displayString + " running in web app " + waitString); if (threadWaitMs > 0) { try { thread.join(threadWaitMs); // Wait for thread to run } catch (InterruptedException e) { // Do nothing } } // Normally threads should not be stopped (method is deprecated), since it may cause an inconsistent state. // In this case however, the alternative is a classloader leak, which may or may not be considered worse. if (thread.isAlive()) thread.stop(); } else { warn("Thread " + displayString + " is still running in web app"); } } } } } }