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:eu.planets_project.pp.plato.util.FileUtils.java

public static File getResourceFile(String name) throws URISyntaxException {
    URI uri = Thread.currentThread().getContextClassLoader().getResource(name).toURI();
    return new File(uri);
}

From source file:com.khs.sherpa.util.Util.java

public static URL getResource(String resource) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    return cl.getResource(resource);
}

From source file:Main.java

/**
 * Stupid... but there is places all over that hold on to
 * AccessControlContexts and Context Class Loaders. This easily consitutes a
 * leak. So, for stuff that doesn't need anything from the context class
 * loader this is a way of keeping context to a minimum;
 * //  ww w.jav  a 2  s . co  m
 * 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 propagated.
 * 
 * @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
 * @throws Exception
 */
public static <T> T cleanContextExceptionExecute(ClassLoader contextLoader, final Callable<T> callable)
        throws Exception {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(contextLoader);
    try {
        return AccessController.doPrivileged(new PrivilegedExceptionAction<T>() {
            public T run() throws Exception {
                return callable.call();
            }
        });
    } finally {
        Thread.currentThread().setContextClassLoader(cl);
    }
}

From source file:Main.java

@Override
public void init() {
    System.out.println("init(): " + Thread.currentThread().getName());

    Runnable task = () -> System.out.println("Running the task on the " + Thread.currentThread().getName());

    // Submit the task to be run on the JavaFX Aplication Thread
    Platform.runLater(task);/*from w w w.  jav  a2  s .co m*/
}

From source file:Main.java

/**
 * Tries to load the class from the preferred loader.  If not successful, tries to 
 * load the class from the current thread's context class loader or system class loader.
 * @param classname Desired class name.//from w  w  w.java 2s.  c  om
 * @param preferredLoader The preferred class loader
 * @return the loaded class.
 * @throws ClassNotFoundException if the class could not be loaded by any loader
 */
public static Class<?> loadClass(String classname, ClassLoader preferredLoader) throws ClassNotFoundException {
    ClassNotFoundException exception = null;
    for (ClassLoader loader : Arrays.asList(preferredLoader, Thread.currentThread().getContextClassLoader(),
            ClassLoader.getSystemClassLoader())) {
        try {
            return loader.loadClass(classname);
        } catch (ClassNotFoundException e) {
            if (exception == null) {
                exception = e;
            }
        }
    }
    throw exception;
}

From source file:ThreadDemo.java

public void run() {
    try {/*from  ww  w  .j  a va  2s .com*/
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + "yielding control...");
    Thread.yield();

    System.out.println(Thread.currentThread().getName() + " has finished executing.");
}

From source file:MyThread.java

public void run() {
    try {/*  w  w w. j a v  a  2 s . c  om*/
        Thread.sleep(30000);
        for (int i = 1; i < 10; i++) {
            if (Thread.interrupted()) {
                System.out.println("interrupted.");
                break;
            }
        }
    } catch (Exception exc) {
        System.out.println(Thread.currentThread().getName() + " interrupted.");
    }
    System.out.println(Thread.currentThread().getName() + " exiting.");
}

From source file:architecture.common.util.ClassUtils.java

public static InputStream loadResource(String name) {
    InputStream in = ClassUtils.class.getResourceAsStream(name);
    if (in == null) {
        in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        if (in == null)
            in = ClassUtils.class.getClassLoader().getResourceAsStream(name);
    }/* w  w  w .  jav a2s  . c o m*/
    if (in == null)
        try {

        } catch (Throwable e) {
            log.warn(L10NUtils.format("002202", name), e);
        }
    return in;
}

From source file:SecurityActions.java

static ClassLoader getTCL() {
    if (System.getSecurityManager() == null) {
        return Thread.currentThread().getContextClassLoader();
    } else {//from   ww w .  ja v a  2s .  com
        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                return Thread.currentThread().getContextClassLoader();
            }
        });
    }
}

From source file:com.juhuasuan.osprey.LoggerInit.java

static private void init() {
    ClassLoader oldTCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(LoggerInit.class.getClassLoader());

    try {// w  w w  .j a  va2 s .c  o  m
        DOMConfigurator.configure(LoggerInit.class.getClassLoader().getResource("osprey-log4j.xml"));
        Logger log4jLogger = Logger.getLogger(LOGGER_NAME);

        FileAppender bizFileAppender = getFileAppender(Logger.getRootLogger());
        if (null == bizFileAppender) {
            LOGGER.warn("ROOT LOGGER dosn't contain any FileAppender!!!");
            return;
        }
        FileAppender fileAppender = getFileAppender(log4jLogger);

        String bizLogDir = new File(bizFileAppender.getFile()).getParent();
        String logFile = new File(bizLogDir, "osprey.log").getAbsolutePath();
        fileAppender.setFile(logFile);
        fileAppender.activateOptions();
        AsyncAppender asynAppender = new AsyncAppender();
        asynAppender.addAppender(fileAppender);
        log4jLogger.addAppender(asynAppender);
        log4jLogger.removeAppender(fileAppender);
        LOGGER.warn("OSPREY LOGGER added Appender. Append file:" + logFile);
    } finally {
        Thread.currentThread().setContextClassLoader(oldTCL);
    }
}