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