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:at.ac.univie.isc.asio.spring.ContextPropagator.java

/**
 * Create a propagator, which stores the context of the current thread, as retrieved from
 * {@link RequestContextHolder#currentRequestAttributes()}.
 *///  w ww .j av  a2 s  .c  om
public static ContextPropagator capture() {
    final Thread thread = Thread.currentThread();
    log.debug(Scope.SYSTEM.marker(), "capturing context of {}", thread);
    return new ContextPropagator(RequestContextHolder.currentRequestAttributes(), thread);
}

From source file:Main.java

private static String clipPeerIdFromCurrentThreadName() {
    String currName = Thread.currentThread().getName();
    int ix = currName.lastIndexOf(ID_SEPARATOR_IN_THREADNAME);
    if (ix != -1) {
        return currName.substring(ix + 1, currName.length());
    }/*from  w ww  .  j  av a  2  s  . c o m*/
    return "";
}

From source file:org.apache.activemq.apollo.dto.JsonCodec.java

static public <T> T decode(Buffer buffer, Class<T> type) throws IOException {
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(JsonCodec.class.getClassLoader());
    try {//w  w w  . jav a 2s  .c  om
        return mapper.readValue(buffer.in(), type);
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}

From source file:Main.java

@Override
public void start(Stage stage) {
    String name = Thread.currentThread().getName();
    System.out.println("start() method: " + name);

    Scene scene = new Scene(new Group(), 200, 200);
    stage.setScene(scene);//ww w  . j  a  v a2s  .c  o m
    stage.setTitle("JavaFX Application Life Cycle");
    stage.show();
}

From source file:Main.java

/**
 * Helper method to determine caller outer class.
 * <p/>//from  www. j a  va 2s  . co m
 * Takes caller class and finds its top enclosing class (which is supposed to be test class).
 * 
 * @param level
 *            on which level this call is being made. 0 - call is made immediately in the method of HotSwapTool.
 * @return outer class reference
 */
private static Class<?> determineOuter(int level) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    // one for Thread#getStackTrace
    // one for #determineOuter
    // one for the caller
    String callerName = stack[level + 3].getClassName();
    try {
        Class<?> clazz = cl.loadClass(callerName);
        while (clazz.getEnclosingClass() != null) {
            clazz = clazz.getEnclosingClass();
        }
        return clazz;
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Cannot find caller class: " + callerName, e);
    }
}

From source file:Main.java

/**
 * Sleeps for the given amount of time even if interrupted. Preserves
 * the interrupt status.// w w w . j av a  2s  . co  m
 * @param msToWait the amount of time to sleep in milliseconds
 */
public static void sleepWithoutInterrupt(final long msToWait) {
    long timeMillis = System.currentTimeMillis();
    long endTime = timeMillis + msToWait;
    boolean interrupted = false;
    while (timeMillis < endTime) {
        try {
            Thread.sleep(endTime - timeMillis);
        } catch (InterruptedException ex) {
            interrupted = true;
        }
        timeMillis = System.currentTimeMillis();
    }

    if (interrupted) {
        Thread.currentThread().interrupt();
    }
}

From source file:com.networknt.light.server.handler.loader.Loader.java

public static File getFileFromResourceFolder(String folder) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(folder);
    File file = null;/*from w w  w  . j  av  a 2s .c  o  m*/
    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        file = new File(url.getPath());
    } finally {
        return file;
    }
}

From source file:com.qagen.osfe.common.utils.Log.java

/**
 * Forces Log to cleanup any cached resources. This is called by the StripesFilter when
 * it is destroyed, but can be called from user code as well if necessary.
 *//*  w w w.ja  v a  2s. c o  m*/
public static void cleanup() {
    LogFactory.release(Thread.currentThread().getContextClassLoader());
}

From source file:Main.java

/**
 * Loads the class with the specified name. This method first attempts  to load the
 * class with the current context classloader and only if the search failed, it
 * tries to load the class with the given class loader.
 *
 * @param name the name of the class.//w ww  .  j  a v  a 2 s  .com
 * @param loader the classloader to load the class.
 *
 * @return the result {@link java.lang.Class} object.
 *
 * @throws ClassNotFoundException if the class was not found.
 */
private static Class loadClass(final String name, final ClassLoader loader) throws ClassNotFoundException {
    ClassLoader l = Thread.currentThread().getContextClassLoader();

    if (l != null) {
        try {
            return l.loadClass(name);
        } catch (ClassNotFoundException ignored) {
            // fall back to the given classloader
        }
    }

    return loader.loadClass(name);
}

From source file:GeneralInterrupt.java

public void work2() throws InterruptedException {
    while (true) {
        if (Thread.currentThread().isInterrupted()) {
            System.out.println("C isInterrupted()=" + Thread.currentThread().isInterrupted());
            Thread.sleep(2000);/*from w  w w.j av  a  2s  . c  om*/
            System.out.println("D isInterrupted()=" + Thread.currentThread().isInterrupted());
        }
    }
}