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 runInSwingThread(final Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {/*from ww w .ja  va  2s  .c  o m*/
        try {
            SwingUtilities.invokeAndWait(runnable);
        } catch (final InvocationTargetException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

public static void safeSwing(@Nonnull final Runnable runnable) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {//www  .j a v  a 2 s  . c  o m
        SwingUtilities.invokeLater(runnable);
    }
}

From source file:Main.java

public static void invokeLaterOnEDT(Runnable runnable) {
    try {//  w w w  . j  av  a  2 s.c o  m
        if (SwingUtilities.isEventDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * A wrapper around SwingUtilities.invokeAndWait() that makes sure that 
 * SwingUtilities.invokeAndWait() is only called when the current thread is 
 * not the AWT event dispatching thread, as required by the documentation
 * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by 
 * SwingUtilities.invokeAndWait().//from   w ww. j  a va2  s.  c  o  m
 * @param runnable The Runnable to call in the event dispatch thread.
 */
public static void invokeAndWait(Runnable runnable) {
    try {
        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else
            SwingUtilities.invokeAndWait(runnable);
    } catch (InvocationTargetException | InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void runThreadSafeSynchronized(final Object synchronizationTarget,
        final boolean singleThreadedOrThreadSafe, final Runnable codeToRun) {
    if (singleThreadedOrThreadSafe) {
        codeToRun.run();
    } else {/* www. ja v a2 s. c  o m*/
        runThreadSafeSynchronized(synchronizationTarget, codeToRun);
    }
}

From source file:Main.java

public static void runThreadSafeSynchronized(final Object synchronizationTarget, final Runnable codeToRun) {
    synchronized (synchronizationTarget) {
        codeToRun.run();
    }/*from w ww  .  j a v  a  2  s .co  m*/
}

From source file:Main.java

public static void executeRunnable(Runnable runnable, boolean waits) {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {//from  www.  jav  a  2 s. c o  m
        try {
            if (waits) {
                SwingUtilities.invokeAndWait(runnable);
            } else {
                SwingUtilities.invokeLater(runnable);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

/**
 * A wrapper around SwingUtilities.invokeAndWait() that makes sure that 
 * SwingUtilities.invokeAndWait() is only called when the current thread is 
 * not the AWT event dispatching thread, as required by the documentation
 * of SwingUtilities.invokeAndWait(); plus catches exceptions thrown by 
 * SwingUtilities.invokeAndWait().//from   www.  ja  v a 2s. c o m
 * @param runnable The Runnable to call in the event dispatch thread.
 */
public static void invokeAndWait(Runnable runnable) {
    try {
        if (SwingUtilities.isEventDispatchThread())
            runnable.run();
        else
            SwingUtilities.invokeAndWait(runnable);
    } catch (InvocationTargetException ex) {
        ex.printStackTrace();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Safe handling for the {@link SwingUtilities#invokeAndWait(Runnable)}
 * method. It is unsafe to call invokeAndWait on the dispatch thread due to
 * deadlocks. This method simply runs the given {@link Runnable} if this is
 * called in the dispatch thread./*w  ww  . jav a  2  s  .  com*/
 * 
 * @param r
 *            - the runnable to run
 * @throws Exception
 *             - any exceptions propagate, the possible
 *             {@link InvocationTargetException} is unwrapped.
 */
public static void runOnDispatch(Runnable r) throws Exception {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {
        try {
            SwingUtilities.invokeAndWait(r);
        } catch (InvocationTargetException iie) {
            if (iie.getCause() instanceof Exception) {
                throw (Exception) iie.getCause();
            } else {
                throw iie;
            }
        }
    }
}

From source file:Main.java

public static void invokeInEDTAndWait(Runnable runnable) throws Exception {
    if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
    } else {//from  w  ww  .  jav  a 2 s  .  co m
        try {
            SwingUtilities.invokeAndWait(runnable);

        } catch (InvocationTargetException e) {
            Throwable targetException = e.getTargetException();
            if (targetException instanceof Exception)
                throw (Exception) targetException;
            if (targetException instanceof Error)
                throw (Error) targetException;
            throw new IllegalStateException(targetException);

        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
}