1 /*
2  * Copyright (C) 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.textclassifier.common;
18 
19 import com.android.textclassifier.common.base.TcLog;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import com.google.common.util.concurrent.ThreadFactoryBuilder;
23 import java.util.concurrent.Executors;
24 
25 // TODO(licha): Revisit the configurations of thread pools
26 /**
27  * Holder of executor singletons.
28  *
29  * <p>Note because we have two processes, so we may keep two copis of executors in this class.
30  */
31 public final class TextClassifierServiceExecutors {
32   private static final String TAG = "TextClassifierServiceExecutors";
33 
34   /** Returns an executor with normal priority. Used for handling client requests. */
getNormhPriorityExecutor()35   public static ListeningExecutorService getNormhPriorityExecutor() {
36     return NormPriorityExecutorHolder.normPriorityExecutor;
37   }
38 
39   /** Returns a single-thread executor with low priority. Used for internal tasks like logging. */
getLowPriorityExecutor()40   public static ListeningExecutorService getLowPriorityExecutor() {
41     return LowPriorityExecutorHolder.lowPriorityExecutor;
42   }
43 
44   private static class NormPriorityExecutorHolder {
45     static final ListeningExecutorService normPriorityExecutor =
46         init("tcs-norm-prio-executor-%d", Thread.NORM_PRIORITY, /* corePoolSize= */ 2);
47   }
48 
49   private static class LowPriorityExecutorHolder {
50     static final ListeningExecutorService lowPriorityExecutor =
51         init("tcs-low-prio-executor-%d", Thread.NORM_PRIORITY - 1, /* corePoolSize= */ 1);
52   }
53 
init(String nameFormat, int priority, int corePoolSize)54   private static ListeningExecutorService init(String nameFormat, int priority, int corePoolSize) {
55     TcLog.v(TAG, "Creating executor: " + nameFormat);
56     return MoreExecutors.listeningDecorator(
57         Executors.newFixedThreadPool(
58             corePoolSize,
59             new ThreadFactoryBuilder()
60                 .setNameFormat(nameFormat)
61                 .setPriority(priority)
62                 // In Android, those uncaught exceptions will crash the whole process if not handled
63                 .setUncaughtExceptionHandler(
64                     (thread, throwable) ->
65                         TcLog.e(TAG, "Exception from executor: " + thread, throwable))
66                 .build()));
67   }
68 
TextClassifierServiceExecutors()69   private TextClassifierServiceExecutors() {}
70 }
71