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 BufferedImage loadImage(String fileName) throws IOException {
    URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
    if (url == null)
        throw new IOException("Could not find image: " + fileName);
    return ImageIO.read(url);
}

From source file:Main.java

public static void joinUninterruptibly(Thread t) {
    boolean interrupted = false;
    while (t.isAlive()) {
        try {/*from  www  .j  a  v a2s .c o  m*/
            t.join();
        } catch (InterruptedException e) {
            interrupted = true;
        }
    }
    if (interrupted) {
        Thread.currentThread().interrupt();
    }
}

From source file:Main.java

public static <T> Set<Class<T>> findClassesAssignableFrom(String packageName, Class<T> assignableFrom)
        throws IOException, ClassNotFoundException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Set<Class<T>> classes = new HashSet<Class<T>>();
    String path = packageName.replace('.', '/');
    URL resource = loader.getResource(path);
    if (resource != null) {
        String filePath = resource.getFile();
        if (filePath != null && new File(filePath).isDirectory()) {
            for (String file : new File(filePath).list()) {
                if (file.endsWith(".class")) {
                    String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
                    Class<T> clazz = (Class<T>) Class.forName(name);
                    if (assignableFrom.isAssignableFrom(clazz))
                        classes.add(clazz);
                }//from  w ww.  j a  va 2 s.  c o m
            }
        }
    }
    return classes;
}

From source file:Main.java

/** Returns a {@link String} containing an enumeration of all {@link Thread}s in all {@link ThreadGroup}s. */
public static String enumerateAllThreads() {
    ThreadGroup root = Thread.currentThread().getThreadGroup();

    // climb up the tree until we read the parent group
    while (root.getParent() != null) {
        root = root.getParent();/*from   ww  w. jav  a 2  s.  c  o m*/
    }

    final StringBuffer str = new StringBuffer();
    collectThreadGroupInfo(root, str, "");

    return str.toString();
}

From source file:Main.java

public static void handleInterruptedException() {
    Thread.currentThread().interrupt();
}

From source file:StaticSync.java

private static void print(String msg) {
    String threadName = Thread.currentThread().getName();
    System.out.println(threadName + ": " + msg);
}

From source file:Main.java

/**
 * Interrupt the specified thread. Returns true if it was able to
 * interrupt the specified thread or false if the specified thread
 * is the current thread and thus interrupt was not called.
 *//*  w  ww. j  a v  a  2  s . c om*/
public static boolean interrupt(Thread thread) {
    if (thread != Thread.currentThread()) {
        thread.interrupt();
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Print a string on console with date and invoking thread name and a newline.
 * @param string string to be printed/*w ww .ja  va 2s  .  com*/
 */
public static void println(String string) {
    System.out.println("[" + new Date(System.currentTimeMillis()) + "] " + Thread.currentThread().getName()
            + ": " + string);
}

From source file:Main.java

private static List<Class> getClasses(String packageName) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = null;
    try {//from  w w w .  j  a v a  2 s.  co  m
        resources = classLoader.getResources(path);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes;
}

From source file:Main.java

public static void checkInterruptedStatus() throws InterruptedException {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException();
    }/*from   w  w  w  .j av a 2 s  .  co  m*/
}