ThreadingHandler.java
01 /*
02  * Copyright 2008-2016 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 javax.annotation.Nullable;
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 2.0.0
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 runInsideUIAsync(@Nonnull Runnable runnable);
41 
42     /**
43      * Executes a code block synchronously on the UI thread.
44      */
45     void runInsideUISync(@Nonnull Runnable runnable);
46 
47     /**
48      * Executes a code block outside of the UI thread.
49      */
50     void runOutsideUI(@Nonnull Runnable runnable);
51 
52     /**
53      * Executes a code block as a Future on an ExecutorService.
54      */
55     @Nonnull
56     <R> Future<R> runFuture(@Nonnull ExecutorService executorService, @Nonnull Callable<R> callable);
57 
58     /**
59      * Executes a code block as a Future on a default ExecutorService.
60      */
61     @Nonnull
62     <R> Future<R> runFuture(@Nonnull Callable<R> callable);
63 
64     /**
65      * Executes a code block synchronously on the UI thread.
66      @since 2.2.0
67      */
68     @Nullable
69     <R> R runInsideUISync(@Nonnull Callable<R> callable);
70 }