1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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 
17 package androidx.arch.core.executor;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.RestrictTo;
21 
22 /**
23  * A task executor that can divide tasks into logical groups.
24  * <p>
25  * It holds a collection a executors for each group of task.
26  * <p>
27  * TODO: Don't use this from outside, we don't know what the API will look like yet.
28  * @hide
29  */
30 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
31 public abstract class TaskExecutor {
32     /**
33      * Executes the given task in the disk IO thread pool.
34      *
35      * @param runnable The runnable to run in the disk IO thread pool.
36      */
executeOnDiskIO(@onNull Runnable runnable)37     public abstract void executeOnDiskIO(@NonNull Runnable runnable);
38 
39     /**
40      * Posts the given task to the main thread.
41      *
42      * @param runnable The runnable to run on the main thread.
43      */
postToMainThread(@onNull Runnable runnable)44     public abstract void postToMainThread(@NonNull Runnable runnable);
45 
46     /**
47      * Executes the given task on the main thread.
48      * <p>
49      * If the current thread is a main thread, immediately runs the given runnable.
50      *
51      * @param runnable The runnable to run on the main thread.
52      */
executeOnMainThread(@onNull Runnable runnable)53     public void executeOnMainThread(@NonNull Runnable runnable) {
54         if (isMainThread()) {
55             runnable.run();
56         } else {
57             postToMainThread(runnable);
58         }
59     }
60 
61     /**
62      * Returns true if the current thread is the main thread, false otherwise.
63      *
64      * @return true if we are on the main thread, false otherwise.
65      */
isMainThread()66     public abstract boolean isMainThread();
67 }
68