Example usage for java.lang Thread currentThread

List of usage examples for java.lang Thread currentThread

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static native Thread currentThread();

Source Link

Document

Returns a reference to the currently executing thread object.

Usage

From source file:Main.java

public static void v(String message) {
    if (LEVEL <= VERBOSE) {
        StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
        String tag = getDefaultTag(stackTraceElement);
        Log.v(tag, getLogInfo(stackTraceElement) + message);
    }//from www .  ja  v  a 2s . com
}

From source file:Main.java

public static int getLine(int N) {
    StackTraceElement ste = Thread.currentThread().getStackTrace()[N];
    return ste.getLineNumber();
}

From source file:Main.java

public static void normalShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit) {
    try {// w w  w .  j  av a  2  s  . c om
        pool.shutdownNow();
        if (!pool.awaitTermination(timeout, timeUnit)) {
            System.err.println("Pool did not terminated");
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Wait for all threads with the specified name to finish, i.e. to not appear in the
 * list of running threads any more./*  ww w  . ja  va 2  s.  c  o  m*/
 *
 * @param name
 *
 * @throws InterruptedException
 * @author dominik.stadler
 */
public static void waitForThreadToFinish(final String name) throws InterruptedException {
    int count = Thread.currentThread().getThreadGroup().activeCount();

    Thread[] threads = new Thread[count];
    Thread.currentThread().getThreadGroup().enumerate(threads);

    for (Thread t : threads) {
        if (t != null && name.equals(t.getName())) {
            t.join();
        }
    }
}

From source file:Main.java

public static void showThreadName(int num) {
    String groupName = Thread.currentThread().getThreadGroup().getName();
    String threadName = Thread.currentThread().getName();
    System.out.println(String.format("%d: %s-%s", num, groupName, threadName));
}

From source file:Main.java

/**
 * Wait for thread whose name contain the specified string to finish, i.e. to not appear in the
 * list of running threads any more./*  ww  w  .ja  va2  s.c  om*/
 *
 * @param name
 *
 * @throws InterruptedException
 * @author dominik.stadler
 */
public static void waitForThreadToFinishSubstring(final String name) throws InterruptedException {
    int count = Thread.currentThread().getThreadGroup().activeCount();

    Thread[] threads = new Thread[count];
    Thread.currentThread().getThreadGroup().enumerate(threads);

    for (Thread t : threads) {
        if (t != null && t.getName().contains(name)) {
            t.join();
        }
    }
}

From source file:TestUnsynchronized.java

public static void print(String msg) {
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName + ": " + msg);
}

From source file:Main.java

/**
 * Print a string on console with date and invoking thread name.
 * This method is not buffered, and will be flushed on encountering new-line.
 * @param string string to be printed/*from w w w  .  j  a va 2 s .  c  o  m*/
 */
public static void print(String string) {
    System.out.print("[" + new Date(System.currentTimeMillis()) + "] " + Thread.currentThread().getName() + ": "
            + string);
}

From source file:Main.java

public static void dumphreadLocals() {
    try {/*w ww. java2  s .  c  o  m*/
        // Get a reference to the thread locals table of the current thread
        Thread thread = Thread.currentThread();
        Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
        threadLocalsField.setAccessible(true);
        Object threadLocalTable = threadLocalsField.get(thread);

        // Get a reference to the array holding the thread local variables
        // inside the
        // ThreadLocalMap of the current thread
        @SuppressWarnings("rawtypes")
        Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
        Field tableField = threadLocalMapClass.getDeclaredField("table");
        tableField.setAccessible(true);
        Object[] table = (Object[]) tableField.get(threadLocalTable);

        @SuppressWarnings("rawtypes")
        Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry");
        Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value");
        entryValueField.setAccessible(true);
        // The key to the ThreadLocalMap is a WeakReference object. The
        // referent field of this object
        // is a reference to the actual ThreadLocal variable
        Field referentField = Reference.class.getDeclaredField("referent");
        referentField.setAccessible(true);

        for (Object entry : table) {
            // Each entry in the table array of ThreadLocalMap is an Entry
            // object
            // representing the thread local reference and its value
            if (entry != null) {

                Object tlcValue = entryValueField.get(entry);

                ThreadLocal threadLocal = (ThreadLocal) referentField.get(entry);

                // System.out.println("thread local value "+tlcValue);
                // if(threadLocal)
                printObject(threadLocal, tlcValue);
            }
        }
        System.out.println("__________________________");
    } catch (Exception e) {
        // We will tolerate an exception here and just log it
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static void showLog(String tag, String msg) {
    if (isDebug && !TextUtils.isEmpty(msg)) {
        if (TextUtils.isEmpty(tag))
            tag = mTag;//  ww  w.j av  a  2s .co  m
        StackTraceElement[] stackTraceElement = Thread.currentThread().getStackTrace();
        int currentIndex = -1;
        for (int i = 0; i < stackTraceElement.length; i++) {
            if (stackTraceElement[i].getMethodName().compareTo("showLog") == 0) {
                currentIndex = i + 1;
                break;
            }
        }
        if (currentIndex >= 0) {
            String fullClassName = stackTraceElement[currentIndex].getClassName();
            String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
            String methodName = stackTraceElement[currentIndex].getMethodName();
            String lineNumber = String.valueOf(stackTraceElement[currentIndex].getLineNumber());

            Log.i(tag, msg + "\n  ---->at " + className + "." + methodName + "(" + className + ".java:"
                    + lineNumber + ")");
        } else {
            Log.i(tag, msg);
        }
    }
}