Here you can find the source of invokeNow(Runnable runnable)
Runnable
on the AWT event dispatching thread now and wait until completion.public static void invokeNow(Runnable runnable)
//package com.java2s; /*//from w w w.j av a 2 s . co m * Copyright 2010-2013 Institut Pasteur. * * This file is part of Icy. * * Icy is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Icy is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Icy. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; import javax.swing.SwingUtilities; public class Main { /** * Invoke the specified <code>Runnable</code> on the AWT event dispatching thread now and wait * until completion.<br> * 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); } } } /** * Invoke the specified <code>Callable</code> on the AWT event dispatching thread now and return * the result.<br> * The returned result can be <code>null</code> when a {@link Throwable} exception happen.<br> * Use this method carefully as it may lead to dead lock. * * @throws InterruptedException * if the current thread was interrupted while waiting * @throws Exception * if the computation threw an exception */ public static <T> T invokeNow(Callable<T> callable) throws InterruptedException, Exception { if (SwingUtilities.isEventDispatchThread()) return callable.call(); final FutureTask<T> task = new FutureTask<T>(callable); try { EventQueue.invokeAndWait(task); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) throw (Exception) e.getCause(); // not an exception --> handle it //IcyExceptionHandler.showErrorMessage(e, true); return null; } try { return task.get(); } catch (ExecutionException e) { if (e.getCause() instanceof Exception) throw (Exception) e.getCause(); // not an exception --> handle it //IcyExceptionHandler.showErrorMessage(e, true); return null; } } /** * @return true if the current thread is an AWT event dispatching thread. */ public static boolean isEventDispatchThread() { return EventQueue.isDispatchThread(); } /** * @deprecated Use {@link #invokeNow(Runnable)} instead */ @Deprecated public static void invokeAndWait(Runnable runnable) { invokeNow(runnable); } }