Example usage for java.lang Runnable getClass

List of usage examples for java.lang Runnable getClass

Introduction

In this page you can find the example usage for java.lang Runnable getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static Thread createThread(Runnable runnable) {
    return createThread(runnable, runnable.getClass().getSimpleName());
}

From source file:org.apache.hadoop.util.concurrent.ExecutorHelper.java

static void logThrowableFromAfterExecute(Runnable r, Throwable t) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("afterExecute in thread: " + Thread.currentThread().getName() + ", runnable type: "
                + r.getClass().getName());
    }/*  w w  w  .j a  va 2 s.co  m*/

    //For additional information, see: https://docs.oracle
    // .com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor
    // .html#afterExecute(java.lang.Runnable,%20java.lang.Throwable) .

    if (t == null && r instanceof Future<?>) {
        try {
            ((Future<?>) r).get();
        } catch (ExecutionException ee) {
            LOG.warn("Execution exception when running task in " + Thread.currentThread().getName());
            t = ee.getCause();
        } catch (InterruptedException ie) {
            LOG.warn("Thread (" + Thread.currentThread() + ") interrupted: ", ie);
            Thread.currentThread().interrupt();
        } catch (Throwable throwable) {
            t = throwable;
        }
    }

    if (t != null) {
        LOG.warn("Caught exception in thread " + Thread.currentThread().getName() + ": ", t);
    }
}

From source file:com.acmutv.ontoqa.tool.runtime.RuntimeManager.java

/**
 * Registers atexit runnables as JVM shutdown hooks.
 * @param hooks atexit runnables.//from www  . jav  a2s  .  com
 * @see Runtime
 */
public static void registerShutdownHooks(Runnable... hooks) {
    Runtime runtime = Runtime.getRuntime();
    for (Runnable hook : hooks) {
        runtime.addShutdownHook(new Thread(hook));
        LOGGER.trace("Registered shutdown hook {}", hook.getClass().getName());
    }
}

From source file:com.l2jfree.util.concurrent.ExecuteWrapper.java

public static void execute(Runnable runnable, long maximumRuntimeInMillisecWithoutWarning) {
    long begin = System.nanoTime();

    try {/*  www .  j av  a2  s .com*/
        runnable.run();
    } catch (RuntimeException e) {
        _log.warn("Exception in a Runnable execution:", e);
    } finally {
        long runtimeInNanosec = System.nanoTime() - begin;
        Class<? extends Runnable> clazz = runnable.getClass();

        RunnableStatsManager.handleStats(clazz, runtimeInNanosec);

        long runtimeInMillisec = TimeUnit.NANOSECONDS.toMillis(runtimeInNanosec);

        if (runtimeInMillisec > maximumRuntimeInMillisecWithoutWarning) {
            L2TextBuilder tb = L2TextBuilder.newInstance();

            tb.append(clazz);
            tb.append(" - execution time: ");
            tb.append(runtimeInMillisec);
            tb.append("msec");

            _log.warn(tb.moveToString());
        }
    }
}

From source file:uk.bl.wa.util.TimeLimiter.java

/**
 * Creates a Thread with runnable and starts it. Waits at most timeoutMS for it to finish before sending an
 * interrupt. If waitAfterInterrupt is true, it then waits at most timeoutMS for the thread to finish.
 *
 * Important: Due to the nature of Java Threads the runnable might still be executing in the background after
 * this method has returned. There is not hard shutdown of the runnable.
 * @param runnable the job to run in a limited amount of time.
 * @param timeoutMS the amount of time to wait for processing to finish.
 * @param waitAfterInterrupt if true, there will be a new timeout after interrupt has been called.
 * @return true if the runnable finished within the timeout, else false.
 */// w  w w . jav a2 s  . com
public static boolean run(Runnable runnable, long timeoutMS, boolean waitAfterInterrupt) {
    Thread parseThread = new Thread(runnable, "timelimiter_" + Long.toString(System.currentTimeMillis()));
    parseThread.setDaemon(true); // Ensure that the JVM will not hang on exit, waiting for Threads to finish
    final long startTime = System.currentTimeMillis();
    log.debug("Starting timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS + "ms");
    parseThread.start();
    try {
        parseThread.join(timeoutMS);
    } catch (InterruptedException e) {
        throw new RuntimeException("The Thread for the Runnable " + runnable.getClass()
                + " was interrupted while waiting for result", e);
    }

    long spendTime = System.currentTimeMillis() - startTime;
    if (spendTime <= timeoutMS) {
        // Finished within the timeout
        log.debug("Finished timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS
                + "ms successfully in " + spendTime + "ms");
        return true;
    }

    // Did not finish. Try interrupting
    parseThread.interrupt();
    if (waitAfterInterrupt) {
        try {
            parseThread.join(timeoutMS);
        } catch (InterruptedException e) {
            throw new RuntimeException(
                    "The Thread for the Runnable was interrupted while waiting for result after interrupting",
                    e);
        }
    }
    log.debug("Finished timelimited run of " + runnable.getClass() + " with timeout " + timeoutMS
            + "ms unsuccessfully in " + spendTime + "ms. The created Thread is still alive");
    return false;
}

From source file:voldemort.utils.RebalanceUtils.java

public static ExecutorService createExecutors(int numThreads) {

    return Executors.newFixedThreadPool(numThreads, new ThreadFactory() {

        @Override/*from  ww  w  . java 2 s  . c om*/
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName(r.getClass().getName());
            return thread;
        }
    });
}

From source file:org.jclouds.atmosonline.saas.AtmosStorageClientLiveTest.java

/**
 * Due to eventual consistency, container commands may not return correctly immediately. Hence,
 * we will try up to the inconsistency window to see if the assertion completes.
 */// w ww .j  a v  a 2 s .  c  om
protected static void assertEventually(Runnable assertion) throws InterruptedException {
    long start = System.currentTimeMillis();
    AssertionError error = null;
    for (int i = 0; i < 30; i++) {
        try {
            assertion.run();
            if (i > 0)
                System.err.printf("%d attempts and %dms asserting %s%n", i + 1,
                        System.currentTimeMillis() - start, assertion.getClass().getSimpleName());
            return;
        } catch (AssertionError e) {
            error = e;
        }
        Thread.sleep(INCONSISTENCY_WINDOW / 30);
    }
    if (error != null)
        throw error;

}

From source file:org.glowroot.agent.plugin.executor.ExecutorAspect.java

private static void onBeforeWithRunnableHolder(ThreadContext context,
        ParameterHolder<Runnable> runnableHolder) {
    Runnable runnable = runnableHolder.get();
    if (runnable instanceof SuppressedRunnableMixin) {
        return;/*from   w w  w . j a v  a2 s .c  om*/
    } else if (runnable instanceof RunnableEtcMixin) {
        onBeforeCommon(context, (RunnableEtcMixin) runnable);
    } else if (runnable != null && runnable.getClass().getName().contains("$$Lambda$")) {
        wrapRunnable(runnableHolder, context);
    }
}

From source file:org.glowroot.agent.plugin.executor.ExecutorAspect.java

private static boolean onThreadInitCommon(ThreadContext context, ParameterHolder<Runnable> runnableHolder) {
    Runnable runnable = runnableHolder.get();
    if (!(runnable instanceof SuppressedRunnableMixin)) {
        if (runnable instanceof RunnableEtcMixin) {
            onBeforeCommon(context, (RunnableEtcMixin) runnable);
            return true;
        } else if (runnable != null && runnable.getClass().getName().contains("$$Lambda$")) {
            wrapRunnable(runnableHolder, context);
            return true;
        }//from w ww  . ja  v  a  2  s. c om
    }
    return false;
}

From source file:com.clustercontrol.commons.util.MonitoredThreadPoolExecutor.java

@Override
protected void beforeExecute(Thread t, Runnable r) {
    try {//from w w  w  . ja  va  2  s.  c  o m
        beginTask(t, r.getClass().getName());
    } finally {
        super.beforeExecute(t, r);
    }
}