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 void sleep(long durationMillis) {
    try {/*w  ww. j  a va 2  s  . co  m*/
        Thread.sleep(durationMillis);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static final void shutdownAndAwaitTermination(ExecutorService executorService, long timeout,
        TimeUnit timeUnit) {//from   w  w  w  .java2  s. c  o  m
    if (isShutDown(executorService)) {
        return;
    }

    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(timeout, timeUnit)) {
            executorService.shutdownNow();
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
    executorService.shutdownNow();

}

From source file:Main.java

public static void assertSwingThread() {
    if (!SwingUtilities.isEventDispatchThread())
        throw new Error("Must be Swing event dispatching thread, but detected '"
                + Thread.currentThread().getName() + '\'');
}

From source file:Main.java

/**
 * sleep until timeout.//  ww  w . j  ava  2s . co  m
 * 
 * @param nanos
 */
public final static void deepSleep(long sleepFor, TimeUnit unit) {
    if (sleepFor < 0) {
        throw new IllegalArgumentException("sleepFor can't be minus.");
    }
    long startTimeInNanos = System.nanoTime();
    long leftNanos = unit.toNanos(sleepFor);
    boolean isInterrupted = false;
    while (leftNanos > 0) {
        try {
            TimeUnit.NANOSECONDS.sleep(leftNanos);
            leftNanos = 0;
        } catch (InterruptedException e) {
            isInterrupted = true;
            leftNanos -= (System.nanoTime() - startTimeInNanos);
        }
    }

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

From source file:Main.java

public static ThreadGroup createThreadGroup(String name) {
    SecurityManager s = System.getSecurityManager();
    ThreadGroup parent = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
    return new ThreadGroup(parent, name);
}

From source file:Main.java

public static long time(Executor executor, int concurrency, final Runnable action) throws InterruptedException {
    final CountDownLatch ready = new CountDownLatch(concurrency);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(concurrency);

    for (int i = 0; i < concurrency; i++) {
        executor.execute(new Runnable() {
            @Override/*from  w w w . j  av a  2 s  . c  o m*/
            public void run() {
                ready.countDown(); // Tell timer we're ready
                try {
                    start.await(); // Wait till peers are ready
                    action.run();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } finally {
                    done.countDown(); // Tell timer we're done
                }
            }
        });
    }

    ready.await(); // Wait for all workers to be ready
    long startNanos = System.nanoTime();
    start.countDown(); // And they're off!
    done.await(); // Wait for all workers to finish
    return System.nanoTime() - startNanos;
}

From source file:Main.java

/**
 * @param exec//from  w  w w .j  a  v a  2  s  .c om
 * @param checkInterval
 * @param shutdown
 */
public static void checkBlockPoolCompleted(ThreadPoolExecutor exec, int checkInterval, boolean shutdown) {
    while (true) {
        int activite = exec.getActiveCount();
        waitFor(checkInterval);
        if (activite == 0) {
            break;
        }
    }

    if (shutdown) {
        exec.shutdown();
        try {
            exec.awaitTermination(60, TimeUnit.SECONDS);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}

From source file:gov.nyc.doitt.gis.geoclient.util.ClassUtils.java

public static ClassLoader getDefaultClassLoader() {
    ClassLoader cl = null;/*  w  w  w.ja  va  2s .  com*/
    try {
        cl = Thread.currentThread().getContextClassLoader();
    } catch (Throwable ex) {
        // Cannot access thread context ClassLoader - falling back to system
        // class loader...
    }
    if (cl == null) {
        // No thread context class loader -> use class loader of this class.
        cl = ClassUtils.class.getClassLoader();
    }
    return cl;
}

From source file:Main.java

public static ThreadGroup getRootThreadGroup() {
    if (rootThreadGroup == null) {
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
        ThreadGroup parent = tg.getParent();
        while (parent != null) {
            tg = parent;//from   w ww. j  ava 2 s . c  o m
            parent = tg.getParent();
        }
        rootThreadGroup = tg;
    }
    return rootThreadGroup;
}

From source file:Main.java

/**
 * Load a given resource. <p/> This method will try to load the resource
 * using the following methods (in order):
 * <ul>//from   w  ww  .j  a  va2s.c  o m
 * <li>From Thread.currentThread().getContextClassLoader()
 * <li>From ClassLoaderUtil.class.getClassLoader()
 * <li>callingClass.getClassLoader()
 * </ul>
 * 
 * @param resourceName The name of the resource to load
 * @param callingClass The Class object of the calling object
 */
public static URL getResource(String resourceName, Class callingClass) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName.substring(1));
    }

    ClassLoader cluClassloader = Main.class.getClassLoader();
    if (cluClassloader == null) {
        cluClassloader = ClassLoader.getSystemClassLoader();
    }
    if (url == null) {
        url = cluClassloader.getResource(resourceName);
    }
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = cluClassloader.getResource(resourceName.substring(1));
    }

    if (url == null) {
        ClassLoader cl = callingClass.getClassLoader();

        if (cl != null) {
            url = cl.getResource(resourceName);
        }
    }

    if (url == null) {
        url = callingClass.getResource(resourceName);
    }

    if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
        return getResource('/' + resourceName, callingClass);
    }

    return url;
}