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 long getCurrentThreadId() {
    Thread thread = Thread.currentThread();
    long l = thread.getId();
    ;/*from w  w  w  . ja v  a 2  s .  c  o m*/
    return l;
}

From source file:Main.java

/**
 * Get all threads//w  w  w.j  a  va2  s . co m
 * 
 * @return
 */
public static String[] getThreadNames() {
    ThreadGroup group = Thread.currentThread().getThreadGroup();
    ThreadGroup parent = null;
    while ((parent = group.getParent()) != null) {
        group = parent;
    }
    Thread[] threads = new Thread[group.activeCount()];
    group.enumerate(threads);
    HashSet<String> set = new HashSet<String>();

    for (int i = 0; i < threads.length; ++i) {
        if (threads[i] != null && threads[i].isAlive()) {
            try {
                set.add(threads[i].getThreadGroup().getName() + ", " + threads[i].getName() + ", "
                        + threads[i].getPriority() + ", " + threads[i].getState());
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    String[] result = (String[]) set.toArray(new String[0]);
    Arrays.sort(result);

    for (int i = 0; i < result.length; i++) {
        //         logger.debug(result[i]);
    }

    return result;
}

From source file:Main.java

public static String getLogInfo(StackTraceElement stackTraceElement) {
    StringBuilder logInfoStringBuilder = new StringBuilder();
    String threadName = Thread.currentThread().getName();
    long threadID = Thread.currentThread().getId();
    String fileName = stackTraceElement.getFileName();
    String className = stackTraceElement.getClassName();
    String methodName = stackTraceElement.getMethodName();
    int lineNumber = stackTraceElement.getLineNumber();
    logInfoStringBuilder.append("[ ");
    logInfoStringBuilder.append("threadID=" + threadID).append(SEPARATOR);
    logInfoStringBuilder.append("threadName=" + threadName).append(SEPARATOR);
    logInfoStringBuilder.append("fileName=" + fileName).append(SEPARATOR);
    logInfoStringBuilder.append("className=" + className).append(SEPARATOR);
    logInfoStringBuilder.append("methodName=" + methodName).append(SEPARATOR);
    logInfoStringBuilder.append("lineNumber=" + lineNumber);
    logInfoStringBuilder.append(" ] ");
    return logInfoStringBuilder.toString();
}

From source file:Main.java

/**
 * Get the currentThread StackTrace/*from   w  w w .j  a v  a  2s .co m*/
 * <p>VMStack.getThreadStackTrace<p/>
 *
 * @return
 */
public static StackTraceElement getCallerStackTraceElement() {
    return Thread.currentThread().getStackTrace()[4];
}

From source file:Main.java

/**
 * Print a message to System.out //ww w.  j  a v  a 2s  . co m
 * but also identify which thread is saying the message
 * @param message - The message to post.
 */
private static void threadMessage(String message) {
    String threadName = Thread.currentThread().getName();
    System.out.format("%s: %s%n", threadName, message);
}

From source file:com.linkedin.helix.controller.HelixControllerMain.java

public static void main(String[] args) throws Exception {
    // read the config;
    // check if the this process is the master wait indefinitely
    // other approach is always process the events but when updating the zk
    // check if this is master.
    // This is difficult to get right
    // get the clusters to manage
    // for each cluster create a manager
    // add the respective listeners for each manager
    CommandLine cmd = processCommandLineArgs(args);
    String zkConnectString = cmd.getOptionValue(zkServerAddress);
    String clusterName = cmd.getOptionValue(cluster);
    String controllerMode = STANDALONE;
    String controllerName = null;
    int propertyTransServicePort = -1;

    if (cmd.hasOption(mode)) {
        controllerMode = cmd.getOptionValue(mode);
    }//w  ww  .j  a va 2s  .  c  o  m

    if (cmd.hasOption(propertyTransferServicePort)) {
        propertyTransServicePort = Integer.parseInt(cmd.getOptionValue(propertyTransferServicePort));
    }
    if (controllerMode.equalsIgnoreCase(DISTRIBUTED) && !cmd.hasOption(name)) {
        throw new IllegalArgumentException("A unique cluster controller name is required in DISTRIBUTED mode");
    }

    controllerName = cmd.getOptionValue(name);

    // Espresso_driver.py will consume this
    logger.info("Cluster manager started, zkServer: " + zkConnectString + ", clusterName:" + clusterName
            + ", controllerName:" + controllerName + ", mode:" + controllerMode);

    if (propertyTransServicePort > 0) {
        ZKPropertyTransferServer.getInstance().init(propertyTransServicePort, zkConnectString);
    }

    HelixManager manager = startHelixController(zkConnectString, clusterName, controllerName, controllerMode);
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        logger.info("controller:" + controllerName + ", " + Thread.currentThread().getName() + " interrupted");
    } finally {
        manager.disconnect();
        ZKPropertyTransferServer.getInstance().shutdown();
    }

}

From source file:Main.java

private static InputStream loadResourceInputStream(String file) throws FileNotFoundException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    InputStream inputStream = classLoader.getResourceAsStream(file);

    if (inputStream == null) {
        throw new FileNotFoundException("File: '" + file + "', not found.");
    }//w  ww  .j  av a  2  s .co  m

    return inputStream;
}

From source file:Main.java

public static void sleep(TimeUnit timeUnit, long duration) {
    try {/*from   w w  w. j  a  v  a 2 s.co  m*/
        timeUnit.sleep(duration);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static void requireLock(ReentrantLock lock) {
    if (!lock.isHeldByCurrentThread()) {
        throw new RuntimeException(
                "Lock is not held by current thread: thread-name = " + Thread.currentThread().getName());
    }/* w  w  w  .jav  a  2 s  .c om*/
}

From source file:Main.java

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