ThreadingHandler.java
01 /*
02  * Copyright 2010-2013 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.core;
17 
18 import groovy.lang.Closure;
19 
20 import java.util.concurrent.Callable;
21 import java.util.concurrent.ExecutorService;
22 import java.util.concurrent.Future;
23 
24 /**
25  * Base contract for classes that can perform tasks in different threads following
26  * the conventions set by the application.
27  *
28  @author Andres Almiray
29  @since 0.9.3
30  */
31 public interface ThreadingHandler {
32     /**
33      * True if the current thread is the UI thread.
34      */
35     boolean isUIThread();
36 
37     /**
38      * Executes a code block asynchronously on the UI thread.
39      */
40     void execInsideUIAsync(Runnable runnable);
41 
42     /**
43      * Executes a code block synchronously on the UI thread.
44      */
45     void execInsideUISync(Runnable runnable);
46 
47     /**
48      * Executes a code block outside of the UI thread.
49      */
50     void execOutsideUI(Runnable runnable);
51 
52     /**
53      * Executes a code block as a Future on an ExecutorService.
54      */
55     <R> Future<R> execFuture(ExecutorService executorService, Closure<R> closure);
56 
57     /**
58      * Executes a code block as a Future on a default ExecutorService.
59      */
60     <R> Future<R> execFuture(Closure<R> closure);
61 
62     /**
63      * Executes a code block as a Future on an ExecutorService.
64      */
65     <R> Future<R> execFuture(ExecutorService executorService, Callable<R> callable);
66 
67     /**
68      * Executes a code block as a Future on a default ExecutorService.
69      */
70     <R> Future<R> execFuture(Callable<R> callable);
71 }