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

/**
 * Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT)
 * And waits for completion//from  w  ww .  j  a va2 s  . co m
 * @param runnable runnable code dedicated to Swing
 * @throws IllegalStateException if any exception occurs while the given runnable code executes using EDT
 */
public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException {
    if (isEDT()) {
        // current Thread is EDT, simply execute runnable:
        runnable.run();
    } else {
        // If the current thread is interrupted, then use invoke later EDT (i.e. do not wait):
        if (Thread.currentThread().isInterrupted()) {
            invokeLaterEDT(runnable);
        } else {
            try {
                // Using invokeAndWait to be in sync with the calling thread:
                SwingUtilities.invokeAndWait(runnable);

            } catch (InterruptedException ie) {
                // propagate the exception because it should never happen:
                throw new IllegalStateException(
                        "SwingUtils.invokeAndWaitEDT : interrupted while running " + runnable, ie);
            } catch (InvocationTargetException ite) {
                // propagate the internal exception :
                throw new IllegalStateException(
                        "SwingUtils.invokeAndWaitEDT : an exception occured while running " + runnable,
                        ite.getCause());
            }
        }
    }
}

From source file:Main.java

public static void invokeAndWait(final Runnable doRun) {
    if (doRun == null) {
        throw new IllegalArgumentException("The runnable cannot be null");
    }//from   www.ja v a 2s .  c  o m
    if (EventQueue.isDispatchThread()) {
        doRun.run();
    } else {
        try {
            EventQueue.invokeAndWait(doRun);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

From source file:de.codesourcery.eve.skills.util.Misc.java

public static void runOnEventThreadLater(Runnable r) {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {/*from w  ww. j av a2s .  c  o  m*/
        SwingUtilities.invokeLater(r);
    }
}

From source file:Main.java

public static void onTextFieldChange(JTextField field, final Runnable task) {
    field.getDocument().addDocumentListener(new DocumentListener() {

        @Override/*from w  ww  . ja va2s. c  om*/
        public void insertUpdate(DocumentEvent e) {
            task.run();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            task.run();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            task.run();
        }
    });
}

From source file:de.codesourcery.eve.skills.util.Misc.java

public static void runOnEventThread(Runnable r) {
    if (SwingUtilities.isEventDispatchThread()) {
        r.run();
    } else {//from w ww.  j  a  v  a  2  s  . co  m
        try {
            SwingUtilities.invokeAndWait(r);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:grails.validation.DeferredBindingActions.java

public static void runActions() {
    List<Runnable> runnables = deferredBindingActions.get();
    if (runnables != null) {
        try {/*  w  ww .j a  va 2s  .  c  om*/
            for (Runnable runnable : getDeferredBindingActions()) {
                if (runnable != null) {
                    try {
                        runnable.run();
                    } catch (Exception e) {
                        LOG.error("Error running deferred data binding: " + e.getMessage(), e);
                    }
                }
            }
        } finally {
            clear();
        }
    }
}

From source file:Main.java

public static void doInBackground(final Runnable task) {
    new Thread(new Runnable() {
        @Override/*from  w  w w .j av  a  2 s. c  o  m*/
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
            task.run();
        }
    }).start();
}

From source file:Main.java

public static Thread startLoopThread(final Runnable runnable, final long repeatInterval) {
    Thread thread = new Thread(new Runnable() {
        @Override//  w w w.ja  v a 2 s.  c  o m
        public void run() {
            while (true) {
                runnable.run();
                if (repeatInterval > 0) {
                    try {
                        Thread.sleep(repeatInterval);
                    } catch (InterruptedException e) {
                        // NOP
                    }
                }
            }
        }
    });
    thread.start();
    return thread;
}

From source file:com.atlassian.jira.rest.client.TestUtil.java

public static void assertErrorCodeWithRegexp(int errorCode, String regExp, Runnable runnable) {
    try {// w w  w .ja v  a 2 s .  co m
        runnable.run();
        Assert.fail(RestClientException.class + " exception expected");
    } catch (com.atlassian.jira.rest.client.api.RestClientException ex) {
        final ErrorCollection errorElement = getOnlyElement(ex.getErrorCollections().iterator());
        final String errorMessage = getOnlyElement(errorElement.getErrorMessages().iterator());
        Assert.assertTrue("'" + ex.getMessage() + "' does not match regexp '" + regExp + "'",
                errorMessage.matches(regExp));
        Assert.assertTrue(ex.getStatusCode().isPresent());
        Assert.assertEquals(errorCode, ex.getStatusCode().get().intValue());
    }
}

From source file:com.atlassian.jira.rest.client.TestUtil.java

private static void assertExpectedErrors(final Collection<ErrorCollection> expectedErrors,
        final Runnable runnable) {
    try {// w ww  . j  av  a  2  s. co m
        runnable.run();
        Assert.fail(RestClientException.class + " exception expected");
    } catch (com.atlassian.jira.rest.client.api.RestClientException e) {
        Assert.assertEquals(e.getErrorCollections(), expectedErrors);
    }
}