• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.providers.media.util;
18 
19 import android.os.Handler;
20 import android.os.HandlerThread;
21 
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.TimeUnit;
25 
26 /**
27  * Thread for asynchronous event processing. This thread is configured as
28  * {@link android.os.Process#THREAD_PRIORITY_FOREGROUND}, which means more CPU
29  * resources will be dedicated to it, and it will be treated like "a user
30  * interface that the user is interacting with."
31  * <p>
32  * This thread is best suited for tasks that the user is actively waiting for,
33  * or for tasks that the user expects to be executed immediately.
34  *
35  * @see BackgroundThread
36  */
37 public final class ForegroundThread extends HandlerThread {
38     private static ForegroundThread sInstance;
39     private static Handler sHandler;
40     private static HandlerExecutor sHandlerExecutor;
41 
ForegroundThread()42     private ForegroundThread() {
43         super("fg", android.os.Process.THREAD_PRIORITY_FOREGROUND);
44     }
45 
ensureThreadLocked()46     private static void ensureThreadLocked() {
47         if (sInstance == null) {
48             sInstance = new ForegroundThread();
49             sInstance.start();
50             sHandler = new Handler(sInstance.getLooper());
51             sHandlerExecutor = new HandlerExecutor(sHandler);
52         }
53     }
54 
get()55     public static ForegroundThread get() {
56         synchronized (ForegroundThread.class) {
57             ensureThreadLocked();
58             return sInstance;
59         }
60     }
61 
getHandler()62     public static Handler getHandler() {
63         synchronized (ForegroundThread.class) {
64             ensureThreadLocked();
65             return sHandler;
66         }
67     }
68 
getExecutor()69     public static Executor getExecutor() {
70         synchronized (ForegroundThread.class) {
71             ensureThreadLocked();
72             return sHandlerExecutor;
73         }
74     }
75 
waitForIdle()76     public static void waitForIdle() {
77         final CountDownLatch latch = new CountDownLatch(1);
78         getExecutor().execute(() -> {
79             latch.countDown();
80         });
81         try {
82             latch.await(30, TimeUnit.SECONDS);
83         } catch (InterruptedException e) {
84             throw new IllegalStateException(e);
85         }
86     }
87 }
88