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

public void run() {
    // returns the identifier of this Thread.
    System.out.println("Name = " + Thread.currentThread().getName());
    System.out.print("Id = " + Thread.currentThread().getId());
}

From source file:net.erdfelt.android.sdkfido.logging.Logging.java

public static void config() {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL url = cl.getResource("logging.properties");
    if (url != null) {
        InputStream in = null;/*ww w  .jav  a  2 s. c  om*/
        try {
            in = url.openStream();
            LogManager.getLogManager().readConfiguration(in);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
}

From source file:Main.java

/**
 * Prints a formatted message to the command prompt
 * /*from ww  w . j  a  v a 2 s .c om*/
 * @param format
 *          the message (format)
 * @param params
 *          the message parameters (optional)
 */
public static void log(final String format, final Object... params) {
    System.out.printf("%tT [%s] %s%n", System.currentTimeMillis(), Thread.currentThread().getName(),
            String.format(format, params));
}

From source file:Main.java

public static void sleep(long duration, TimeUnit unit) {
    try {/*from w w w  . j a v  a 2 s . c o  m*/
        Thread.sleep(unit.toMillis(duration));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

/**
 * Prints the current thread's details to the console.
 *//*from www.  j  ava  2s.  c o m*/
public static void printCurrentThreadDetails() {

    Thread currentThread = Thread.currentThread();

    StackTraceElement[] stackTrace = currentThread.getStackTrace();

    for (StackTraceElement element : stackTrace) {

        System.out.println("\t" + element);
    }
}

From source file:com.refreshsf.contrib.client.tests.ResourceUtil.java

public static URL getResource(String fileName) {
    return Thread.currentThread().getContextClassLoader().getResource(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  w  w . jav  a 2 s .  c  o m*/
    try {
        Thread.sleep(10000);
    } catch (Exception exc) {
        System.out.println(thrdName + " interrupted.");
    }
    System.out.println(thrdName + " terminating.");
}

From source file:gov.nih.nci.ncicb.cadsr.common.util.ObjectFactory.java

/**
 * Load a class for the specified name and scope.
 * In the cases when the active ClassLoader is null, we use
 *  Class.forName(), otherwise ClassLoader.loadClass() is used
 *//*from   w  ww .  jav a 2 s  .com*/
public static Class forName(Class scope, String className) throws ClassNotFoundException {
    ClassLoader loader = null;

    try {
        loader = Thread.currentThread().getContextClassLoader();
    } // end try
    catch (Exception e) {
        loader = scope.getClassLoader();
    } // end catch

    return (loader != null) ? loader.loadClass(className) : Class.forName(className);
}

From source file:Main.java

public static int getThreadNumber(int maxThreads) {
    if (maxThreads == 1) {
        return 1;
    } else {//ww w .  j  a  v a2 s.c  om
        int threadNumber = 1;
        String name = Thread.currentThread().getName();
        int lastIndex = name.lastIndexOf("-");
        if (lastIndex >= 0) {
            try {
                threadNumber = Integer.parseInt(name.substring(lastIndex + 1));
            } catch (NumberFormatException e) {
            }
        }
        return threadNumber;
    }
}

From source file:com.tek271.reverseProxy.utils.FileTools.java

public static InputStream readFromContextToInputStream(String fileName) {
    InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    if (inputStream == null) {
        throw new IllegalArgumentException("Cannot find " + fileName);
    }/*  w ww .j  a  va  2 s .  c  o  m*/
    return inputStream;
}