Example usage for java.lang Thread getClass

List of usage examples for java.lang Thread getClass

Introduction

In this page you can find the example usage for java.lang Thread getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:dk.dma.ais.utils.aisbus.AisBusLauncher.java

public static void main(String[] args) throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override/*from w  w w  .  j a va2  s .c  o m*/
        public void uncaughtException(Thread t, Throwable e) {
            LOG.error("Uncaught exception in thread " + t.getClass().getCanonicalName() + ": " + e.getMessage(),
                    e);
            System.exit(-1);
        }
    });
    new AisBusLauncher().execute(args);
}

From source file:dk.dma.ais.utils.filter.AisFilter.java

public static void main(String[] args) throws Exception {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override/*from   w  w w .j ava 2  s . c o  m*/
        public void uncaughtException(Thread t, Throwable e) {
            System.err.println(
                    "Uncaught exception in thread " + t.getClass().getCanonicalName() + ": " + e.getMessage());
            System.exit(-1);
        }
    });
    final AisFilter aisFilter = new AisFilter();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            aisFilter.shutdown();
        }
    });
    aisFilter.execute(args);
}

From source file:Main.java

public static String getName(Thread t) {
    return getName(t.getClass());
}

From source file:Main.java

public static void dumphreadLocals()
        throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
    Thread thread = Thread.currentThread();
    Field threadLocalField = thread.getClass().getDeclaredField("threadLocals");
    threadLocalField.setAccessible(true);
    Object threadLocalTable = threadLocalField.get(thread);

    Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
    //Class threadLocalMapClass = Class.forName(threadLocalField.getType().getName());
    Field tableField = threadLocalMapClass.getDeclaredField("table");
    tableField.setAccessible(true);/*  ww  w  .j a va 2 s .co  m*/
    Object[] table = (Object[]) tableField.get(threadLocalTable);

    Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry");
    Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value");
    entryValueField.setAccessible(true);

    //Field referenceField = Reference.class.getDeclaredField("referent") ;
    //referenceField.setAccessible(true);

    for (Object entry : table) {
        if (entry != null) {
            Object threadLocalValue = entryValueField.get(entry);
            printObject(threadLocalValue);
            //ThreadLocal threadLocal = (ThreadLocal)referenceField.get(entry);
            //System.out.println("thread local  value "+threadLocal);
        }
    }
}

From source file:Main.java

private static int countThreads(String namePattern) {
    Thread[] list = new Thread[100];
    Thread.enumerate(list);/*www  .ja v a2s .  c o  m*/
    int matches = 0;
    for (Thread element : list) {
        if (element != null) {
            if (element.getName().matches(namePattern) || element.getClass().getName().matches(namePattern)) {
                matches++;
            }
        }
    }
    return matches;
}

From source file:Main.java

public static String getTreadStackTrace(Thread t) {
    if (java14) {
        return "";
    }//from   ww  w . j  a v a  2  s.  c o  m
    try {
        // Java 1.5 thread.getStackTrace();
        Method m = t.getClass().getMethod("getStackTrace", null);

        StackTraceElement[] trace = (StackTraceElement[]) m.invoke(t, null);
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < trace.length; i++) {
            b.append("\n\tat ").append(trace[i]);
        }
        return b.toString();
    } catch (Throwable e) {
        java14 = true;
        return "";
    }
}

From source file:com.nubits.nubot.utils.Utils.java

public static void logActiveThreads() {
    int active = Thread.activeCount();
    LOG.trace("currently active threads: " + active);
    Thread allThreads[] = new Thread[active];
    Thread.enumerate(allThreads);

    for (int i = 0; i < active; i++) {
        Thread t = allThreads[i];
        LOG.trace(i + ": " + t + " id: " + t.getId() + " name: " + t.getName() + " " + t.getContextClassLoader()
                + " group: " + t.getThreadGroup() + " alive" + t.isAlive());
        LOG.trace("super: " + t.getClass().getSuperclass());
    }/*from  www . j av  a 2  s  .co m*/

    if (active > maxThreadsError) {
        LOG.error("too many threads started");
    }
}

From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java

private static void clearReferencesStopTimerThread(Thread thread) {

    // Need to get references to:
    // - newTasksMayBeScheduled field
    // - queue field
    // - queue.clear()

    try {// w ww . j ava  2s . c  om
        Field newTasksMayBeScheduledField = thread.getClass().getDeclaredField("newTasksMayBeScheduled");
        newTasksMayBeScheduledField.setAccessible(true);
        Field queueField = thread.getClass().getDeclaredField("queue");
        queueField.setAccessible(true);

        Object queue = queueField.get(thread);

        Method clearMethod = queue.getClass().getDeclaredMethod("clear");
        clearMethod.setAccessible(true);

        synchronized (queue) {
            newTasksMayBeScheduledField.setBoolean(thread, false);
            clearMethod.invoke(queue);
            queue.notify(); // In case queue was already empty.
        }

        if (logger.isLoggable(Level.FINE))
            logger.fine("A web application appears to have started a TimerThread named [" + thread.getName()
                    + "] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.");

    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to terminate TimerThread named [" + thread.getName() + "]", e);
    }
}

From source file:org.openmrs.util.OpenmrsClassLoader.java

private static List<Thread> listThreads(ThreadGroup group, String indent) {
    List<Thread> threadToReturn = new ArrayList<Thread>();

    log.error(indent + "Group[" + group.getName() + ":" + group.getClass() + "]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i = 0; i < nt; i++) {
        Thread t = threads[i];
        log.error(indent + "  Thread[" + t.getName() + ":" + t.getClass() + ":"
                + (t.getContextClassLoader() == null ? "null cl"
                        : t.getContextClassLoader().getClass().getName() + " "
                                + t.getContextClassLoader().hashCode())
                + "]");
        threadToReturn.add(t);// ww  w . j av  a  2s.com
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
    ng = group.enumerate(groups, false);

    for (int i = 0; i < ng; i++) {
        threadToReturn.addAll(listThreads(groups[i], indent + "  "));
    }

    return threadToReturn;
}

From source file:com.streamsets.datacollector.main.TestLogConfigurator.java

@After
public void cleanUp() throws Exception {
    System.getProperties().remove("log4j.configuration");
    System.getProperties().remove("log4j.defaultInitOverride");
    for (Thread thread : Thread.getAllStackTraces().keySet()) {
        if (thread instanceof FileWatchdog) {
            Field interrupted = ((Class) thread.getClass().getGenericSuperclass())
                    .getDeclaredField("interrupted");
            interrupted.setAccessible(true);
            interrupted.set(thread, true);
            thread.interrupt();/*w  ww .  j  a  v a  2s.c  o m*/
        }
    }
}