• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.tv.settings.library.util;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.Future;
26 
27 public class ThreadUtils {
28 
29     private static volatile Thread sMainThread;
30     private static volatile Handler sMainThreadHandler;
31     private static volatile ExecutorService sThreadExecutor;
32 
33     /**
34      * Returns true if the current thread is the UI thread.
35      */
isMainThread()36     public static boolean isMainThread() {
37         if (sMainThread == null) {
38             sMainThread = Looper.getMainLooper().getThread();
39         }
40         return Thread.currentThread() == sMainThread;
41     }
42 
43     /**
44      * Returns a shared UI thread handler.
45      */
getUiThreadHandler()46     public static Handler getUiThreadHandler() {
47         if (sMainThreadHandler == null) {
48             sMainThreadHandler = new Handler(Looper.getMainLooper());
49         }
50 
51         return sMainThreadHandler;
52     }
53 
54     /**
55      * Checks that the current thread is the UI thread. Otherwise throws an exception.
56      */
ensureMainThread()57     public static void ensureMainThread() {
58         if (!isMainThread()) {
59             throw new RuntimeException("Must be called on the UI thread");
60         }
61     }
62 
63     /**
64      * Posts runnable in background using shared background thread pool.
65      *
66      * @Return A future of the task that can be monitored for updates or cancelled.
67      */
postOnBackgroundThread(Runnable runnable)68     public static Future postOnBackgroundThread(Runnable runnable) {
69         return getThreadExecutor().submit(runnable);
70     }
71 
72     /**
73      * Posts callable in background using shared background thread pool.
74      *
75      * @Return A future of the task that can be monitored for updates or cancelled.
76      */
postOnBackgroundThread(Callable callable)77     public static Future postOnBackgroundThread(Callable callable) {
78         return getThreadExecutor().submit(callable);
79     }
80 
81     /**
82      * Posts the runnable on the main thread.
83      */
postOnMainThread(Runnable runnable)84     public static void postOnMainThread(Runnable runnable) {
85         getUiThreadHandler().post(runnable);
86     }
87 
getThreadExecutor()88     private static synchronized ExecutorService getThreadExecutor() {
89         if (sThreadExecutor == null) {
90             sThreadExecutor = Executors.newFixedThreadPool(
91                     Runtime.getRuntime().availableProcessors());
92         }
93         return sThreadExecutor;
94     }
95 }
96