Java Swing UI Thread Event resultOfOnEventThread(final Callable task)

Here you can find the source of resultOfOnEventThread(final Callable task)

Description

Execute a task on the event dispatch thread and return the result.

License

Open Source License

Parameter

Parameter Description
task The task to execute.
T The type of the result to return.

Return

The result of the task.

Declaration

@SuppressWarnings("unchecked") 
public static <T> T resultOfOnEventThread(final Callable<T> task) 

Method Source Code


//package com.java2s;
/*//from   w  w  w  .j  a  v a2 s.com
 * WorldPainter, a graphical and interactive map generator for Minecraft.
 * Copyright ? 2011-2015  pepsoft.org, The Netherlands
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Callable;

public class Main {
    /**
     * Execute a task on the event dispatch thread and return the result. The
     * task <em>may</em> be executed on a different thread, so it must be
     * thread-safe.
     *
     * @param task The task to execute.
     * @param <T> The type of the result to return.
     * @return The result of the task.
     */
    @SuppressWarnings("unchecked") // Responsibility of caller
    public static <T> T resultOfOnEventThread(final Callable<T> task) {
        if (SwingUtilities.isEventDispatchThread()) {
            try {
                return task.call();
            } catch (Exception e) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new RuntimeException(e.getClass().getSimpleName() + " thrown by task", e);
                }
            }
        } else {
            final Object[] result = new Object[1];
            final Exception[] exception = new Exception[1];
            try {
                SwingUtilities.invokeAndWait(() -> {
                    try {
                        synchronized (result) {
                            result[0] = task.call();
                        }
                    } catch (Exception e) {
                        synchronized (exception) {
                            exception[0] = e;
                        }
                    }
                });
            } catch (InterruptedException e) {
                throw new RuntimeException(
                        "Thread interrupted while waiting for task to execute on event dispatch thread", e);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getTargetException();
                throw new RuntimeException(
                        cause.getClass().getSimpleName() + " thrown by task on event dispatch thread", cause);
            }
            synchronized (exception) {
                if (exception[0] != null) {
                    throw new RuntimeException(
                            exception[0].getClass().getSimpleName() + " thrown by task on event dispatch thread",
                            exception[0]);
                }
            }
            synchronized (result) {
                return (T) result[0];
            }
        }
    }
}

Related

  1. isEventDispatchThread()
  2. isEventDispatchThread()
  3. isLinefeedTag(HTML.Tag tag)
  4. onShowingChanged(final JComponent component, final Runnable callback)
  5. processOnSwingEventThread(Runnable todo, boolean wait)
  6. runAndWaitOnEDT(Runnable r)
  7. runAndWaitOnEDT(Runnable runnable)
  8. runInDispatchThread(Runnable r)
  9. runInEDT(final Runnable runnable)