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

public void run() {
    System.out.println(Thread.currentThread().getName() + " starting.");
    try {/*from  www .jav a2 s  . c o m*/
        Thread.sleep(5000);
    } catch (Exception e) {
        System.out.print(Thread.currentThread().getName());
        System.out.println(" interrupted.");
    }
    System.out.println(Thread.currentThread().getName() + " exiting.");
}

From source file:Main.java

/**
 * Similar to {@link Class#forName(java.lang.String)}, but attempts to load
 * through the thread context class loader. Only if thread context class
 * loader is inaccessible, or it can't find the class will it attempt to
 * fall back to the class loader that loads the FreeMarker classes.
 *//*ww  w .  j av  a  2 s  . co m*/
public static Class forName(String className) throws ClassNotFoundException {
    try {
        ClassLoader ctcl = Thread.currentThread().getContextClassLoader();
        if (ctcl != null) { // not null: we don't want to fall back to the bootstrap class loader
            return Class.forName(className, true, ctcl);
        }
    } catch (ClassNotFoundException e) {
        ;// Intentionally ignored
    } catch (SecurityException e) {
        ;// Intentionally ignored
    }
    // Fall back to the defining class loader of the FreeMarker classes 
    return Class.forName(className);
}

From source file:Main.java

public static void logMessage(String message) {
    logMessage(Thread.currentThread().hashCode(), message);
}

From source file:TwoThreadSetName.java

public void printMsg() {
    Thread t = Thread.currentThread();
    String name = t.getName();
    System.out.println("name=" + name);
}

From source file:Main.java

public static Class<?> findClass(String className) throws ClassNotFoundException {
    // [JACKSON-597]: support primitive types (and void)
    if (className.indexOf('.') < 0) {
        if ("int".equals(className))
            return Integer.TYPE;
        if ("long".equals(className))
            return Long.TYPE;
        if ("float".equals(className))
            return Float.TYPE;
        if ("double".equals(className))
            return Double.TYPE;
        if ("boolean".equals(className))
            return Boolean.TYPE;
        if ("byte".equals(className))
            return Byte.TYPE;
        if ("char".equals(className))
            return Character.TYPE;
        if ("short".equals(className))
            return Short.TYPE;
        if ("void".equals(className))
            return Void.TYPE;
    }//  w w  w  .j ava 2s  .  c o  m
    // Two-phase lookup: first using context ClassLoader; then default
    Throwable prob = null;
    ClassLoader loader = Thread.currentThread().getContextClassLoader();

    if (loader != null) {
        try {
            return Class.forName(className, true, loader);
        } catch (Exception e) {
            prob = getRootCause(e);
        }
    }
    try {
        return Class.forName(className);
    } catch (Exception e) {
        if (prob == null) {
            prob = getRootCause(e);
        }
    }
    if (prob instanceof RuntimeException) {
        throw (RuntimeException) prob;
    }
    throw new ClassNotFoundException(prob.getMessage(), prob);
}

From source file:com.aestasit.markdown.BaseTest.java

protected static InputStream testData(String fileName) throws IOException {
    return Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
}

From source file:MyThread.java

public void run() {
    String thrdName = Thread.currentThread().getName();
    System.out.println(thrdName + " starting.");
    while (waiting)
        System.out.println("waiting:" + waiting);
    System.out.println("waiting...");
    startWait();/*from   w ww.  j a  v a 2 s .  c om*/
    try {
        Thread.sleep(1000);
    } catch (Exception exc) {
        System.out.println(thrdName + " interrupted.");
    }
    System.out.println(thrdName + " terminating.");
}

From source file:Main.java

private static String addThreadInfo(final String msg) {
    final String threadName = Thread.currentThread().getName();
    final String shortName = threadName.startsWith("OkHttp") ? "OkHttp" : threadName;
    return "[" + shortName + "] " + msg;
}

From source file:Main.java

public void doit() {
    System.out.println(Thread.currentThread().getStackTrace()[3].getMethodName());
}

From source file:Main.java

public static String getThreadId() {
    StringBuffer sb = new StringBuffer(30);
    sb.append("[thread Id: ").append(Thread.currentThread().getId()).append("] ");
    return sb.toString();
}