Example usage for java.lang Runnable run

List of usage examples for java.lang Runnable run

Introduction

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

Prototype

public abstract void run();

Source Link

Document

When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

Usage

From source file:Main.java

/**
 * Invokes the given Runnable in the AWT event dispatching thread,
 * and waits for it to finish. This method may be called from any thread,
 * including the event dispatching thread itself.
 * @see SwingUtilities#invokeAndWait(Runnable)
 * @param runnable the Runnable to be executed.
 *//*from  ww w. j av  a2  s .co  m*/
public static void invokeAndWait(Runnable runnable) throws InterruptedException, InvocationTargetException {
    try {
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeAndWait(runnable);
        }
    } catch (Exception ex) {
        // Ignore any exceptions.
    }
}

From source file:Main.java

public static void run(final Runnable runnable) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override/*from  w ww .java2 s  .co  m*/
        public void run() {
            runnable.run();
        }
    });
}

From source file:Main.java

public static void runOnViewThread(View view, Runnable runnable) {
    if (view.getHandler() == null || view.getHandler().getLooper() == null
            || view.getHandler().getLooper().getThread() == Thread.currentThread()) {
        runnable.run();
    } else {//from   ww w.j a va 2 s  . com
        view.post(runnable);
    }
}

From source file:Main.java

/**
 * Will invoke the specified action in EDT in case it is called from non-EDT
 * thread./*from www .  ja v  a2s.c o  m*/
 *
 * @param runnable
 *            runnable
 * @throws InterruptedException
 * @throws InvocationTargetException
 */
public static void invokeAndWait(final Runnable runnable)
        throws InterruptedException, InvocationTargetException {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {
        SwingUtilities.invokeAndWait(runnable);
    }
}

From source file:edu.byu.nlp.util.asserts.MoreAsserts.java

public static void assertFails(Runnable runnable, Class<? extends Exception> expectedException) {
    try {// w  w  w.j  av  a2s. c om
        runnable.run();
    } catch (Exception e) {

        if (!expectedException.isInstance(e)) {
            Fail.fail("Threw the wrong kind of exception: " + e.getClass().getName()
                    + " instead of the expected " + expectedException.getName(), e);
        }

        return; // good failure
    }

    Fail.fail("should have thrown a " + expectedException.getName() + " exception but did not!");

}

From source file:Main.java

/**
 * Invoke the specified <code>Runnable</code> on the AWT event dispatching thread now and wait
 * until completion.<br>/*w w  w .ja  v  a 2 s .c o  m*/
 * Any exception is automatically caught by Icy exception handler, if you want to catch them use
 * {@link #invokeNow(Callable)} instead.<br>
 * Use this method carefully as it may lead to dead lock.
 */
public static void invokeNow(Runnable runnable) {
    if (isEventDispatchThread()) {
        try {
            runnable.run();
        } catch (Throwable t) {
            // the runnable thrown an exception
            //IcyExceptionHandler.handleException(t, true);
        }
    } else {
        try {
            EventQueue.invokeAndWait(runnable);
        } catch (InvocationTargetException e) {
            // the runnable thrown an exception
            //IcyExceptionHandler.handleException(e.getTargetException(), true);
        } catch (InterruptedException e) {
            // interrupt exception
            System.err.println("ThreadUtil.invokeNow(...) error :");
            //IcyExceptionHandler.showErrorMessage(e, true);
        }
    }
}

From source file:com.meltmedia.dropwizard.etcd.cluster.ClusterProcessor.java

protected static void safe(String key, Runnable runnable) {
    try {/* w w w  .  ja v  a 2  s  .c  o  m*/
        runnable.run();
    } catch (Throwable t) {
        logger.warn(String.format("message thrown while calling process %s", key), t);
    }
}

From source file:Main.java

/**
 * like Platform.runLater but waits until the thread has finished
 * based on: http://www.guigarage.com/2013/01/invokeandwait-for-javafx/
 * @param r the runnable to run in a JavaFX thread
 *///from   w w  w .j a  v a2 s .  co  m
public static void platformRunAndWait(final Runnable r) throws Throwable {
    if (Platform.isFxApplicationThread()) {
        try {
            r.run();
        } catch (Exception e) {
            throw new ExecutionException(e);
        }
    } else {
        final Lock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();
        final boolean[] done = { false };
        // to get around the requirement for final
        final Throwable[] ex = { null };
        lock.lock();
        try {

            Platform.runLater(() -> {
                lock.lock();
                try {
                    r.run();
                } catch (Throwable e) {
                    ex[0] = e;
                } finally {
                    try {
                        done[0] = true;
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            });

            while (!done[0])
                condition.await();

            if (ex[0] != null) {
                // re-throw exception from the runLater thread
                throw ex[0];
            }
        } finally {
            lock.unlock();
        }
    }
}

From source file:Main.java

/** 
 * Runs the supplied class after a certain period of time, the thread
 * will be executed in the EDT. //from   ww  w .  j  ava2 s.  c  o m
 *
 * @param execute The runnable object whose method will be called after the
 * specified delay
 * @param after The delay in ms before the event will be called
 */
public static Timer invokeAfter(final Runnable execute, int after) {
    Timer timer = new Timer(after, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            execute.run();
        }
    });
    timer.setRepeats(false);
    timer.start();
    return timer;
}

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

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

    try {/*from  w  w w. java 2s  .c o m*/
        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());
        }
    }
}