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

/**
 * Sleep for specified duration in milliseconds.
 * @param millis duration to sleep in milliseconds
 *///from w w w  . j av a  2 s.  c  o m
public static void sleep(final long millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static boolean isUIThread() {
    return Thread.currentThread().equals(Looper.getMainLooper().getThread());
}

From source file:Main.java

public static void printStr(String s) {
    System.out/* ww w .j a  va  2s  .  c o m*/
            .println("[" + getDateFormat(new Date()) + "] - {" + Thread.currentThread().getName() + "} - " + s);
}

From source file:Main.java

public static void runOnViewThread(View view, Runnable runnable) {
    if (view.getHandler() == null || view.getHandler().getLooper() == null
            || view.getHandler().getLooper().getThread() == Thread.currentThread()) {
        runnable.run();/* www  .ja  v a 2 s.  co  m*/
    } else {
        view.post(runnable);
    }
}

From source file:Main.java

public static boolean isMain() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

public static List<Thread> getFullThreadList(boolean includeDaemons) {

    List<Thread> list = new ArrayList<Thread>();

    // Find the root thread group
    ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
    while (root.getParent() != null) {
        root = root.getParent();//from   ww w.  j a  v a 2  s .  c  o  m
    }

    // Visit each thread group
    visit(list, root, 0, includeDaemons);

    // returns the full list
    return list;
}

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.  ja v  a  2 s.  c  o  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:TestSynchronized.java

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

From source file:Main.java

/**
 * Throws an {@link IllegalStateException} if the current thread is not an initialization thread.
 *///  w ww  .  j  a v a 2s .com
public static void ensureInitThread() {
    Thread currentThread = Thread.currentThread();
    boolean isInitThread = currentThread.getName().equals("LunaInitializationThread");
    checkState(isInitThread, String.format("thread[%s] not an initialization thread", currentThread));
}

From source file:Main.java

public static void printCurrentTimeAndThreadAndMessage(String message) {
    System.out.println("[" + getDateFormat(new Date()) + "] - {" + Thread.currentThread().getName() + "} - { "
            + message + " }");
}