1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.utils.threads;
13 import java.util.concurrent.Executor;
15 import org.eclipse.swt.widgets.Display;
17 public class Executors2 {
19 public static Executor createSWTExecutor(Display display, boolean async) {
20 return async ? new SWTExecutorAsync(display) : new SWTExecutorSync(display);
25 class SWTExecutorAsync implements Executor {
28 public SWTExecutorAsync(Display display)
30 this.display = display;
34 public void execute(Runnable command) {
35 // Don't accept work if the SWT thread is disposed.
36 if (display.isDisposed())
37 throw new RuntimeException("The SWT thread has been disposed");
38 display.asyncExec(command);
43 class SWTExecutorSync implements Executor {
46 public SWTExecutorSync(Display display)
48 this.display = display;
52 public void execute(Runnable command) {
53 // Don't accept work if the SWT thread is disposed.
54 if (display.isDisposed())
55 throw new RuntimeException("The SWT thread has been disposed");
56 display.syncExec(command);