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

public static void runInBackground(Runnable runnable, boolean forceNewThread) {
    if (forceNewThread || isMain()) {
        executor.execute(runnable);//  w w  w  .j a v a  2 s.  c o m
    } else {
        runnable.run();
    }

}

From source file:Main.java

/**
 * Make an asynchronous call in a separate thread, with a callback that's run on the current
 * Android UI thread./*w  ww.  j ava  2 s. c o  m*/
 * @param androidUIHandler  the Handler from the current Android context
 * @param call a {@link Runnable} to run in the thread.
 * @param callback a {@link Runnable} to run in the Android UI thread when the call above returns
 */
public static void runAsynchronously(final Handler androidUIHandler, final Runnable call,
        final Runnable callback) {
    Runnable runnable = new Runnable() {
        public void run() {
            call.run();
            if (callback != null) {
                androidUIHandler.post(new Runnable() {
                    public void run() {
                        callback.run();
                    }
                });
            }
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
}

From source file:com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope.java

public static void removeDecisionContext() {
    for (Runnable callback : destructionCallbacks.get()) {
        callback.run();
    }//from  w w w .  j  a  v  a2 s.  co m
    CurrentDecisionContext.unset();
}

From source file:Main.java

public static void addRunnable(final Runnable runnable) {
    initService();/*from w ww  . j a va2s . c o  m*/
    executorService.execute(new Runnable() {
        public void run() {
            try {
                runnable.run();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:Main.java

/**
 * In EDT just runs the runnable (so in that thread the pending AWT events
 * are _not_ dispatched before running the runnable).
 *//*from   w  w w .  j a va  2s .co  m*/
public static void invokeAndWaitFromAnyThread(Runnable r)
        throws InterruptedException, InvocationTargetException {
    if (SwingUtilities.isEventDispatchThread()) {
        try {
            r.run();
        } catch (RuntimeException e) {
            throw new InvocationTargetException(e);
        }
    } else {
        SwingUtilities.invokeAndWait(r);
    }
}

From source file:Main.java

/**
 * Runs the specified runnable in the EDT using
 * <code>SwingUtilities.invokeLater(...)</code>.
 *
 * @param runnable - the runnable to be executed
 *//*w w  w .  ja  va2 s . co  m*/
public static void invokeLater(Runnable runnable) {

    if (!SwingUtilities.isEventDispatchThread()) {

        SwingUtilities.invokeLater(runnable);

    } else {

        runnable.run();
    }

}

From source file:Main.java

public static void foreDelay(final Runnable r, final long millSeconds) {
    main.postDelayed(new Runnable() {

        @Override/*from www.  j  av  a  2  s.c  o  m*/
        public void run() {
            try {
                r.run();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }, millSeconds);
}

From source file:Main.java

/**
 * Run the supplied Runnable on the main thread. The method will block until the Runnable
 * completes./*from   ww w  . ja  v  a 2 s .co m*/
 *
 * @param r The Runnable to run.
 */
public static void runOnUiThreadBlocking(final Runnable r) {
    if (runningOnUiThread()) {
        r.run();
    } else {
        FutureTask<Void> task = new FutureTask<Void>(r, null);
        postOnUiThread(task);
        try {
            task.get();
        } catch (Exception e) {
            throw new RuntimeException("Exception occured while waiting for runnable", e);
        }
    }
}

From source file:Main.java

/**
 * Executes the network requests on a separate thread.
 *
 * @param runnable//from   www .j av a 2 s .  c om
 *            The runnable instance containing network mOperations to be
 *            executed.
 */
public static Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {

            }
        }
    };
    t.start();
    return t;
}

From source file:Main.java

/**
 * Similar as to {@link SwingUtilities#invokeLater(Runnable)}, but does a
 * check first if the current running thread is already the EDT. If so, it
 * will directly call the {@link Runnable#run()} method, otherwise leave it up
 * to {@link SwingUtilities#invokeLater(Runnable)} to invoke it on a later
 * moment.//from  w  ww  .  ja  v a  2s .  c  o m
 * 
 * @param aRunnable
 *          the runnable to call on the EDT, cannot be <code>null</code>.
 */
public static void invokeOnEDT(final Runnable aRunnable) {
    // System.out.println( "invokeOnEDT called with " + aRunnable );
    if (SwingUtilities.isEventDispatchThread()) {
        aRunnable.run();
    } else {
        SwingUtilities.invokeLater(aRunnable);
    }
}