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:ThreadIDMain.java

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

From source file:Main.java

public static String getMethodName() {

    return Thread.currentThread().getStackTrace()[4].getMethodName();
}

From source file:Main.java

public static void sleep(int milliseconds, int nanoseconds) {
    try {/*from ww  w .  ja v  a2s.  c  o m*/
        Thread.sleep(milliseconds, nanoseconds);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static void logCallStack(PrintStream out) {
    printStackTrace(Thread.currentThread().getStackTrace(), out);
}

From source file:Main.java

public static void sleep(long duration, TimeUnit unit) {
    try {//from  w ww. j  ava  2s . co  m
        Thread.sleep(unit.toMillis(duration));
    } catch (InterruptedException var4) {
        Thread.currentThread().interrupt();
    }

}

From source file:Main.java

public static void await(CountDownLatch barrier, long timeoutMillis) {
    try {/*  w ww .  ja v  a  2s . c o m*/
        barrier.await(timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static long getPid() {
    RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
    String[] mxNameTable = mx.getName().split("@"); //$NON-NLS-1$
    if (mxNameTable.length == 2) {
        return Long.parseLong(mxNameTable[0]);
    } else {// www .  ja v  a 2 s .  c  om
        return Thread.currentThread().getId();
    }
}

From source file:Main.java

public static String readInputStream(InputStreamReader in) {
    StringBuilder sb = new StringBuilder();
    try {//from   www. j  a v  a2  s  .com
        BufferedReader reader = new BufferedReader(in);
        char[] temp = new char[1024];
        int length;
        while ((length = reader.read(temp)) != -1) {
            if (Thread.currentThread().isInterrupted())
                break;
            sb.append(temp, 0, length);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        }
    }
    return (sb.toString());
}

From source file:Main.java

/**
 * Gets an {@code Icon} given the url {@code resource}.
 * //ww w  .ja v a2  s .com
 * @param resource
 *            the url path to a valid icon
 * @return An {@code Icon}
 * @throws RuntimeException if the {@link ClassLoader} could not locate the resource.
 */
@Deprecated
public static Icon getIcon(String resource) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL url = cl.getResource(resource);
    if (null == url) {
        throw new RuntimeException("ClassLoader could not locate " + resource);
    }
    return new ImageIcon(url);
}

From source file:Main.java

public static void trace(Object object) {
    if (logTrace) {
        StackTraceElement[] traces = Thread.currentThread().getStackTrace();
        StackTraceElement trace = traces[3];
        android.util.Log.d(TAG_TRACE,// www. ja v  a 2 s .  c  o  m
                addThreadInfo(object.getClass().getSimpleName() + " : " + trace.getMethodName()));
    }
}