]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/Executors2.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / Executors2.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.utils.threads;
12
13 import java.util.concurrent.Executor;
14
15 import org.eclipse.swt.widgets.Display;
16
17 public class Executors2 {
18
19         public static Executor createSWTExecutor(Display display, boolean async) {
20                 return async ? new SWTExecutorAsync(display) : new SWTExecutorSync(display);
21         }
22         
23 }
24
25 class SWTExecutorAsync implements Executor {
26         
27         Display display;
28         public SWTExecutorAsync(Display display)
29         {
30                 this.display = display;
31         }
32         
33         @Override
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);
39         }
40         
41 }
42
43 class SWTExecutorSync implements Executor {
44         
45         Display display;
46         public SWTExecutorSync(Display display)
47         {
48                 this.display = display;
49         }
50         
51         @Override
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);
57         }
58         
59 }
60