• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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.pump.concurrent;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 
22 import androidx.annotation.AnyThread;
23 import androidx.annotation.NonNull;
24 
25 import java.util.concurrent.Executor;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.ThreadFactory;
29 import java.util.concurrent.TimeUnit;
30 
31 @AnyThread
32 public final class Executors {
Executors()33     private Executors() { }
34 
35     private static final Executor DIRECT_EXECUTOR = Runnable::run;
36     private static final Executor MAIN_THREAD_EXECUTOR = new Executor() {
37         private final Handler mHandler = new Handler(Looper.getMainLooper());
38 
39         @Override
40         public void execute(@NonNull Runnable command) {
41             if (mHandler.getLooper() != Looper.myLooper()) {
42                 mHandler.post(command);
43             } else {
44                 command.run();
45             }
46         }
47     };
48     private static final Executor UI_THREAD_EXECUTOR = MAIN_THREAD_EXECUTOR;
49 
directExecutor()50     public static @NonNull Executor directExecutor() {
51         return DIRECT_EXECUTOR;
52     }
53 
mainThreadExecutor()54     public static @NonNull Executor mainThreadExecutor() {
55         return MAIN_THREAD_EXECUTOR;
56     }
57 
uiThreadExecutor()58     public static @NonNull Executor uiThreadExecutor() {
59         return UI_THREAD_EXECUTOR;
60     }
61 
newFixedUniqueThreadPool(int nThreads)62     public static @NonNull ExecutorService newFixedUniqueThreadPool(int nThreads) {
63         return new UniqueExecutor(nThreads, nThreads, 0, TimeUnit.MILLISECONDS,
64                 new LinkedBlockingQueue<>());
65     }
66 
newFixedUniqueThreadPool(int nThreads, @NonNull ThreadFactory threadFactory)67     public static @NonNull ExecutorService newFixedUniqueThreadPool(int nThreads,
68             @NonNull ThreadFactory threadFactory) {
69         return new UniqueExecutor(nThreads, nThreads, 0, TimeUnit.MILLISECONDS,
70                 new LinkedBlockingQueue<>(), threadFactory);
71     }
72 
newCachedUniqueThreadPool()73     public static @NonNull ExecutorService newCachedUniqueThreadPool() {
74         return new UniqueExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
75                 new LinkedBlockingQueue<>());
76     }
77 
newCachedUniqueThreadPool( @onNull ThreadFactory threadFactory)78     public static @NonNull ExecutorService newCachedUniqueThreadPool(
79             @NonNull ThreadFactory threadFactory) {
80         return new UniqueExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
81                 new LinkedBlockingQueue<>(), threadFactory);
82     }
83 }
84