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:com.jayway.restassured.module.mockmvc.internal.SpringSecurityClassPathChecker.java

public static boolean isSpringSecurityInClasspath() {
    try {//w w  w.j  a  v a 2 s .c  o  m
        Class.forName(SECURITY_CONTEXT_HOLDER_CLASS_NAME, false,
                Thread.currentThread().getContextClassLoader());
        return true;
    } catch (Throwable e) {
        return false;
    }
}

From source file:ThreadDemo.java

public void run() {

    Thread t = Thread.currentThread();
    // tests if this thread is alive
    System.out.println("status = " + t.isAlive());
}

From source file:Main.java

/**
 * Execute the given {@link Runnable} on the ui thread.
 *
 * @param runnable The runnable to execute.
 *///w w  w .ja v a 2 s . co  m
public static void runOnUiThread(Runnable runnable) {
    Thread uiThread = Looper.getMainLooper().getThread();
    if (Thread.currentThread() != uiThread)
        new Handler(Looper.getMainLooper()).post(runnable);
    else
        runnable.run();
}

From source file:Main.java

public static Class<?> findClass(String name) {
    ClassLoader cl;/*  w  w w .  j  a  v a2s  . com*/
    try {
        cl = Thread.currentThread().getContextClassLoader();
    } catch (SecurityException e) {
        cl = null;
    }

    Map<String, Class<?>> map;
    synchronized (cache) {
        map = cache.get(cl);

        if (map == null) {
            map = new LinkedHashMap<String, Class<?>>(16, 0.75f, true) {
                private static final long serialVersionUID = 1L;

                protected boolean removeEldestEntry(Map.Entry<String, Class<?>> eldest) {
                    return size() > 1024;
                };
            };
            cache.put(cl, map);
        }
    }
    synchronized (map) {
        if (!map.containsKey(name)) {
            Class<?> target;
            try {
                if (cl != null) {
                    target = cl.loadClass(name);
                } else {
                    target = Class.forName(name);
                }
            } catch (ClassNotFoundException e) {
                target = null;
            }
            map.put(name, target);
        }
        return map.get(name);
    }
}

From source file:Main.java

/**
 * Same as {@link Thread#sleep(long)} except Exception is caught and ignored.
 *//*from  ww w  .j ava 2 s. c  o  m*/
public static void sleep(long milli) {
    try {
        Thread.sleep(milli);
    } catch (InterruptedException e) {
        // have to interrupt the thread
        Thread.currentThread().interrupt();
    }
}

From source file:TwoThreadSleep.java

public void run() {
    Thread t = Thread.currentThread();
    String name = t.getName();//from  w w w.  j  ava  2s .c  om

    System.out.println("entered loop() - " + name);

    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(200);
        } catch (InterruptedException x) {
        }

        System.out.println("name=" + name);
    }
    System.out.println("leave loop() - " + name);

}

From source file:MyThread.java

public void run() {
    System.out.println(Thread.currentThread().getName() + " starting.");
    try {/*w w w. jav  a2  s .c o  m*/
        Thread.sleep(30000);
        for (int i = 1; i < 10; i++) {
            if (Thread.interrupted()) {
                System.out.println("interrupted.");
                break;
            }
            System.out.print(".");
            for (long x = 0; x < 1000; x++)
                System.out.println("/");
        }
    } catch (Exception exc) {
        System.out.println(Thread.currentThread().getName() + " interrupted.");
    }
    System.out.println(Thread.currentThread().getName() + " exiting.");
}

From source file:A.java

synchronized void foo(B b) {
    String name = Thread.currentThread().getName();

    System.out.println(name + " entered A.foo");

    try {//w  w w . jav a 2  s . co  m
        Thread.sleep(1000);
    } catch (Exception e) {
        System.out.println("A Interrupted");
    }

    System.out.println(name + " trying to call B.last()");
    b.last();
}

From source file:Main.java

/**
 * @return true if the current thread is the UI thread, false otherwise.
 *//*from  www.j  a  v a  2 s .c  o  m*/
public static boolean currentThreadIsUiThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:TwoThread.java

public TwoThread() {
    creatorThread = Thread.currentThread();
}