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 showToast(final Activity context, final String msg) {
    if ("main".equals(Thread.currentThread().getName())) {
        Toast.makeText(context, msg, 1).show();
    } else {//from   ww  w  .j  av  a2 s .  c o  m
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg, 1).show();
            }
        });
    }
}

From source file:Main.java

public static void showToast(final Activity context, final String msg) {
    if ("main".equals(Thread.currentThread().getName())) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    } else {//from   w  w  w . ja v  a 2  s.  c o m
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:GetPriority.java

private static Runnable makeRunnable() {
    Runnable r = new Runnable() {
        public void run() {
            for (int i = 0; i < 5; i++) {
                Thread t = Thread.currentThread();
                System.out.println("in run() - priority=" + t.getPriority() + ", name=" + t.getName());

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException x) {
                    // ignore
                }//from  www  . j  av a 2s  .  c o m
            }
        }
    };

    return r;
}

From source file:Main.java

public static void show(final Activity activity, final String text) {
    if ("main".equalsIgnoreCase(Thread.currentThread().getName())) {
        Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
    } else {/*from  w  ww  .j a  v a 2 s . com*/
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 */// www .ja  v  a 2  s  .  co m
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}

From source file:Main.java

public static void interruptCurrentThreadWithLog(Logger logger) {
    logger.info("Thread {} was interrupted", Thread.currentThread());
    Thread.currentThread().interrupt();
}

From source file:Main.java

public static void sleep(long time) {
    if (time <= 0)
        return;// ww  w  .  ja v  a2s  . c  o  m
    try {
        Thread.sleep(time);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Returns current method name/*from ww  w.ja  v  a 2  s  .co  m*/
 * @return method name
 */
public static String getCurrentMethod() {
    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
    return methodName;
}

From source file:Wait.java

public static void manySec(long s) {
    try {/*from   w  w w .j  a  v a2  s  .c  o m*/
        Thread.currentThread().sleep(s * 1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Print diagnostic info about the current thread.
 *//* w  ww.  j av  a  2  s  .c o m*/
public static void printThreadInfo() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}