]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/Executors2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / Executors2.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.utils.threads;\r
12 \r
13 import java.util.concurrent.Executor;\r
14 \r
15 import org.eclipse.swt.widgets.Display;\r
16 \r
17 public class Executors2 {\r
18 \r
19         public static Executor createSWTExecutor(Display display, boolean async) {\r
20                 return async ? new SWTExecutorAsync(display) : new SWTExecutorSync(display);\r
21         }\r
22         \r
23 }\r
24 \r
25 class SWTExecutorAsync implements Executor {\r
26         \r
27         Display display;\r
28         public SWTExecutorAsync(Display display)\r
29         {\r
30                 this.display = display;\r
31         }\r
32         \r
33         @Override\r
34         public void execute(Runnable command) {\r
35                 // Don't accept work if the SWT thread is disposed.\r
36                 if (display.isDisposed()) \r
37                         throw new RuntimeException("The SWT thread has been disposed"); \r
38                 display.asyncExec(command);\r
39         }\r
40         \r
41 }\r
42 \r
43 class SWTExecutorSync implements Executor {\r
44         \r
45         Display display;\r
46         public SWTExecutorSync(Display display)\r
47         {\r
48                 this.display = display;\r
49         }\r
50         \r
51         @Override\r
52         public void execute(Runnable command) {\r
53                 // Don't accept work if the SWT thread is disposed.\r
54                 if (display.isDisposed()) \r
55                         throw new RuntimeException("The SWT thread has been disposed"); \r
56                 display.syncExec(command);\r
57         }\r
58         \r
59 }\r
60 \r