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

/**
 * Join the current thread with the specified thread, waiting at most
 * the given number of milliseconds. A time of zero means wait forever.
 * A return value of false indicates that some error occured while
 * waiting or that we were unable to wait because the specified thread
 * was the current thread.//from   w ww. j av  a2  s .  co  m
 */
public static boolean join(Thread thread, long time) {
    try {
        if (thread == Thread.currentThread())
            return false;
        thread.join(time);
        return true;
    } catch (Exception ex) {
    }
    return false;
}

From source file:Main.java

public static Document file2Document(String filename)
        throws ParserConfigurationException, SAXException, IOException {
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    if (is == null) {
        is = ClassLoader.getSystemResourceAsStream(filename);
    }//  w w w .  j  a va 2 s . c o  m

    if (is == null) {
        return null;
    }
    return inputStream2Document(is);
}

From source file:Main.java

/**
 * Loads a class from the classloader; /*from  ww  w  .  j a v  a 2 s  .  co m*/
 * If not found, the classloader of the {@code context} class specified will be used.
 * If the flag {@code checkParent} is true, the classloader's parent is included in 
 * the lookup.
 */
static Class<?> loadClass(String className, Class<?> context, boolean checkParent) {
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
        if (context != null) {
            ClassLoader loader = context.getClassLoader();
            while (loader != null) {
                try {
                    clazz = loader.loadClass(className);
                    return clazz;
                } catch (ClassNotFoundException e1) {
                    loader = checkParent ? loader.getParent() : null;
                }
            }
        }
    }
    return clazz;
}

From source file:Main.java

/**
 * @return {@code true} if the current thread is the UI thread.
 *//*from w ww. j a v a  2 s.c  om*/
public static boolean isOnUiThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:Main.java

/**
 * Forces the current thread to yield until the given image has completely
 * loaded. This is useful if you need to guarantee that an image has fully
 * loaded.//from  w ww.ja v a  2s.  co m
 * 
 * @param in
 *            the image being loaded
 * @deprecated Use a java.awt.MediaTracker to watch your image loading
 */
@Deprecated
public static void blockUntilImagePrepared(Image in) {
    while (!Toolkit.getDefaultToolkit().prepareImage(in, -1, -1, null)) {
        Thread.currentThread();
        Thread.yield();
    }
}

From source file:Main.java

static final public void resetThreadLocal(Object lock) {
    synchronized (lock) {
        // Get a reference to the thread locals table of the current thread
        try {/*from   www.j a v  a  2s . c  o  m*/
            Thread thread = Thread.currentThread();

            threadLocalsField.setAccessible(true);
            Object threadLocalTable = threadLocalsField.get(thread);

            // Get a reference to the array holding the thread local variables inside the
            // ThreadLocalMap of the current thread

            tableField.setAccessible(true);
            Object table = tableField.get(threadLocalTable);

            // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object
            // is a reference to the actual ThreadLocal variable
            referentField.setAccessible(true);

            for (int i = 0; i < Array.getLength(table); i++) {
                // Each entry in the table array of ThreadLocalMap is an Entry object
                // representing the thread local reference and its value
                Object entry = Array.get(table, i);
                if (entry != null) {
                    // Get a reference to the thread local object and remove it from the table
                    ThreadLocal<?> threadLocal = (ThreadLocal<?>) referentField.get(entry);
                    threadLocal.remove();
                }
            }
        } catch (Exception e) {
        } //noop
        finally {
            try {
                if (referentField != null)
                    referentField.setAccessible(false);
                if (tableField != null)
                    tableField.setAccessible(false);
                if (threadLocalsField != null)
                    threadLocalsField.setAccessible(false);
            } catch (Throwable e) {
                // TODO: log error
            }
        }
    }
}

From source file:Main.java

public static String getMethod(int N) {
    StackTraceElement ste = Thread.currentThread().getStackTrace()[N];
    return ste.getMethodName();
}

From source file:Main.java

/** Helper method for building a string of thread information.*/
public static String getThreadInfo() {
    return "@[name=" + Thread.currentThread().getName() + ", id=" + Thread.currentThread().getId() + "]";
}

From source file:Main.java

public static String TAG() {
    StackTraceElement stacktrace = Thread.currentThread().getStackTrace()[3];
    return "NITRO:" + stacktrace.getClassName().substring("com.qweex.callisto".length() + 1) + ":"
            + stacktrace.getMethodName() + "::" + stacktrace.getLineNumber();
}

From source file:Main.java

public static String convertXMLFileToString(String fileName) {
    try {//from   ww w .  j  av  a  2 s . co m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(new DOMSource(doc), new StreamResult(stw));
        return stw.toString();
    } catch (Exception e) {
        System.out.println("Unable to load xml file: " + e.toString());
    }
    return null;
}