ThreadingHandler.java
01 /*
02  * Copyright 2008-2014 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.threading;
17 
18 import javax.annotation.Nonnull;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Future;
22 
23 /**
24  * Base contract for classes that can perform tasks in different threads following
25  * the conventions set by the application.
26  *
27  @author Andres Almiray
28  @since 2.0.0
29  */
30 public interface ThreadingHandler {
31     /**
32      * True if the current thread is the UI thread.
33      */
34     boolean isUIThread();
35 
36     /**
37      * Executes a code block asynchronously on the UI thread.
38      */
39     void runInsideUIAsync(@Nonnull Runnable runnable);
40 
41     /**
42      * Executes a code block synchronously on the UI thread.
43      */
44     void runInsideUISync(@Nonnull Runnable runnable);
45 
46     /**
47      * Executes a code block outside of the UI thread.
48      */
49     void runOutsideUI(@Nonnull Runnable runnable);
50 
51     /**
52      * Executes a code block as a Future on an ExecutorService.
53      */
54     @Nonnull
55     <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable);
56 
57     /**
58      * Executes a code block as a Future on a default ExecutorService.
59      */
60     @Nonnull
61     <R> Future<R> runFuture(@Nonnull Callable<R> callable);
62 }