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 Thread fork(Runnable runnable) {
    String name = "Forked-from-" + Thread.currentThread().getName();
    Thread thread = new Thread(runnable, name);
    thread.start();//from  w ww  .jav  a2  s.c  o m
    return thread;
}

From source file:Main.java

public static void gracefulShutdown(ExecutorService pool, int timeout, TimeUnit timeUnit) {
    pool.shutdown(); // Disable new tasks from being submitted
    try {//from   w w  w  .ja  v a2  s .  com
        // Wait a while for existing tasks to terminate
        if (!pool.awaitTermination(timeout, timeUnit)) {
            pool.shutdownNow(); // Cancel currently executing tasks
            // Wait a while for tasks to respond to being cancelled
            if (!pool.awaitTermination(timeout, 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:de.berlios.jhelpdesk.utils.StampUtils.java

public static String craeteStampFromObjects(Object first, Object... rest) {
    StringBuilder sb = new StringBuilder(Thread.currentThread().getName());
    sb.append(Thread.currentThread().getId());
    sb.append(first);//from  www.  ja  va2s.c  o m
    for (Object o : rest) {
        sb.append(o);
    }
    sb.append(System.nanoTime());
    return DigestUtils.shaHex(sb.toString());
}

From source file:Main.java

public static Thread getThreadByName(String name) {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();

    while (tg.getParent() != null) {

        tg = tg.getParent();// w  w  w  .j av  a2 s.  co  m
    }

    Thread[] threads = new Thread[tg.activeCount() + 1024];

    tg.enumerate(threads, true);

    boolean bad_found = false;

    for (int i = 0; i < threads.length; i++) {

        Thread t = threads[i];

        if (t != null && t.isAlive() && t != Thread.currentThread() && !t.isDaemon()
                && t.getName().equals(name)) {
            return t;
        }
    }

    return null;
}

From source file:Main.java

public static void gracefulShutdown(ExecutorService pool, int shutdownTimeout, int shutdownNowTimeout,
        TimeUnit timeUnit) {//from   w ww . j  a  v a  2s  . c o m
    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 terminated");
            }
        }
    } catch (InterruptedException ie) {
        // (Re-)Cancel if current thread also interrupted
        pool.shutdownNow();
        // Preserve interrupt status
        Thread.currentThread().interrupt();
    }
}

From source file:com.taobao.pushit.commons.ServerLoggerInit.java

public static void initLog() {

    if (initOK) {
        return;//from   w w  w  . j a v a 2s. c  o m
    }

    URL url = null;
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null || (url = cl.getResource(PUSHIT_SERVER_LOG4J_FILE)) == null) {
        cl = ServerLoggerInit.class.getClassLoader();
        if (cl == null || (url = cl.getResource(PUSHIT_SERVER_LOG4J_FILE)) == null) {
            fallback();
            return;
        }
    }

    final ClassLoader pre = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(PropertyConfigurator.class.getClassLoader());
        PropertyConfigurator.configure(url);
    } finally {
        Thread.currentThread().setContextClassLoader(pre);
    }
}

From source file:Main.java

public static void checkInterrupted(Throwable e) {
    if (e instanceof InterruptedException) {
        Thread.currentThread().interrupt();
    }/*  w  w  w .j  ava2s.  c  o  m*/
}

From source file:Main.java

public static void throwUncaughtException(Throwable th) {
    throwUncaughtException(Thread.currentThread(), th);
}

From source file:Main.java

public static void init(Context context) {
    mContext = context;
    mUiThread = Thread.currentThread();
    mExecutor = Executors.newFixedThreadPool(5);
}

From source file:Main.java

/**
 * Execute a callable with a clean thread context and security context as
 * to avoid any passing on of thread context and security context to another
 * thread that may be spawned from here and may end up holding copies in the end.
 * Any exception thrown will be forwarded via a generic runtime exception.
 * //from  w  ww  .  ja v a2 s.  co  m
 * @param contextLoader A class loader to set as context class loader 
 * @param callable A {@link Callable} to invoke
 * @return Return value from the {@link Callable} invocation
 */
public static <T> T cleanContextExecute(ClassLoader contextLoader, final Callable<T> callable) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(contextLoader);
    try {
        return AccessController.doPrivileged(new PrivilegedAction<T>() {
            public T run() {
                try {
                    return callable.call();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}