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 {// ww w  .  ja v a 2  s .c o m
        for (int i = 1; i < 1000; i++) {
            System.out.print(".");
            Thread.sleep(250);
            synchronized (this) {
                if (stopped)
                    break;
            }
        }
    } catch (Exception exc) {
        System.out.println(Thread.currentThread().getName() + " interrupted.");
    }
    System.out.println(Thread.currentThread().getName() + " exiting.");
}

From source file:com.mgmtp.perfload.core.common.util.LtUtils.java

/**
 * Throws an {@link AbortionException} if the current thread has been interrupted.
 *///from   ww w. j  av  a2 s  . com
public static void checkInterrupt() {
    if (Thread.currentThread().isInterrupted()) {
        LOG.debug("Interrupt detected!");
        throw new AbortionException(LtStatus.INTERRUPTED, "Aborting test due to interrupt.");
    }
}

From source file:Main.java

/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already./*from   w ww. j  av a2 s . c o  m*/
 *
 * @param classLoaderToUse the actual ClassLoader to use for the thread context
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
public static ClassLoader overrideThreadContextClassLoader(ClassLoader classLoaderToUse) {
    Thread currentThread = Thread.currentThread();
    ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
    if (classLoaderToUse != null && !classLoaderToUse.equals(threadContextClassLoader)) {
        currentThread.setContextClassLoader(classLoaderToUse);
        return threadContextClassLoader;
    } else {
        return null;
    }
}

From source file:Main.java

public static ThreadGroup getRootThreadGroup() {
    if (rootThreadGroup != null) {
        return rootThreadGroup;
    }/*from  w ww.  jav  a2s  .com*/
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    ThreadGroup ptg;
    while ((ptg = tg.getParent()) != null) {
        tg = ptg;
    }
    return tg;
}

From source file:com.mayalogy.mayu.io.LocalDataManager.java

public static Object readFromJarAndDeserialize(String resourceName) throws ClassNotFoundException, IOException {
    InputStream is = Class.class.getResourceAsStream("/" + resourceName);
    if (is == null) { //Used for when jar is running in a servlet
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        is = classLoader.getResourceAsStream(resourceName);
    }/*from   w ww. j  a  va 2  s. c  o m*/
    ObjectInputStream ois = new ObjectInputStream(is);
    Object oRead = ois.readObject();
    ois.close();
    return oRead;
}

From source file:Main.java

/**
 * Causes the current thread to stop working for the given {@code time} in millis. Any {@link InterruptedException}
 * are suppressed for convenience but the thread interrupted state is preserved to interrupted.
 * //from  w  ww.  j a  v  a2 s . c om
 * @param time
 *          the sleep time in millis
 */
public static void silentSleep(final long time) {
    try {
        Thread.sleep(time);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:com.citrix.g2w.webdriver.TestLogger.java

/**
 * Method used to get unique string./*ww  w. j  av a2  s . co m*/
 * 
 * @return unique string
 */
public static String getUniqueString() {
    return new Date().getTime() + "-" + Thread.currentThread().getId();
}

From source file:Main.java

private static synchronized DocumentBuilder getDocumentBuilder() {
    DocumentBuilder domBuilder = (DocumentBuilder) domBuilders.get(Thread.currentThread());

    if (domBuilder != null) {
        return domBuilder;
    } else {/*  w  w  w  . j a  va 2 s. c o m*/
        try {
            domBuilder = domBuilderFactory.newDocumentBuilder();
            domBuilders.put(Thread.currentThread(), domBuilder);

            return domBuilder;
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("Cannot build parser: " + e.toString());
        }
    }
}

From source file:Main.java

/**
 * Graceful shutdown./*from   w  w w.  ja v a  2  s  . co  m*/
 *
 * @param pool the pool
 * @param shutdownTimeout the shutdown timeout
 * @param shutdownNowTimeout the shutdown now timeout
 * @param timeUnit the time unit
 */
public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(shutdownTimeout, timeUnit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(shutdownNowTimeout, timeUnit)) {
                System.err.println("Pool did not terminate");
            }
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static void sleep(long timeout, int nanos) {
    //the Thread.sleep method is not precise at all regarding nanos
    if (timeout > 0 || nanos > 900000) {
        try {//from   w  w  w .  j  a va 2  s . c  o  m
            Thread.sleep(timeout + (nanos / 1000000), (nanos % 1000000));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    } else {
        //this isn't a superb metronome either, but allows a granularity
        //with a reasonable precision in the order of 50ths of millisecond
        long initialTime = System.nanoTime() - 200;
        while (System.nanoTime() < initialTime + nanos)
            ;
    }
}