• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /**
45    * Returns a single-thread executor with min priority. Used for downloader background processing.
46    */
getDownloaderExecutor()47   public static ListeningExecutorService getDownloaderExecutor() {
48     return DownloaderExecutorHolder.downloaderExecutor;
49   }
50 
51   /**
52    * Returns a single-thread executor with min priority for network IO ops. Currently only used by
53    * model downloader service.
54    */
getNetworkIOExecutor()55   public static ListeningExecutorService getNetworkIOExecutor() {
56     return NetworkIOExecutorHolder.networkIOExecutor;
57   }
58 
59   private static class NormPriorityExecutorHolder {
60     static final ListeningExecutorService normPriorityExecutor =
61         init("tcs-norm-prio-executor-%d", Thread.NORM_PRIORITY, /* corePoolSize= */ 2);
62   }
63 
64   private static class LowPriorityExecutorHolder {
65     static final ListeningExecutorService lowPriorityExecutor =
66         init("tcs-low-prio-executor-%d", Thread.NORM_PRIORITY - 1, /* corePoolSize= */ 1);
67   }
68 
69   private static class DownloaderExecutorHolder {
70     static final ListeningExecutorService downloaderExecutor =
71         init("tcs-download-executor-%d", Thread.MIN_PRIORITY, /* corePoolSize= */ 1);
72   }
73 
74   private static class NetworkIOExecutorHolder {
75     static final ListeningExecutorService networkIOExecutor =
76         init("tcs-network-io-executor-%d", Thread.MIN_PRIORITY, /* corePoolSize= */ 1);
77   }
78 
init(String nameFormat, int priority, int corePoolSize)79   private static ListeningExecutorService init(String nameFormat, int priority, int corePoolSize) {
80     TcLog.v(TAG, "Creating executor: " + nameFormat);
81     return MoreExecutors.listeningDecorator(
82         Executors.newFixedThreadPool(
83             corePoolSize,
84             new ThreadFactoryBuilder()
85                 .setNameFormat(nameFormat)
86                 .setPriority(priority)
87                 // In Android, those uncaught exceptions will crash the whole process if not handled
88                 .setUncaughtExceptionHandler(
89                     (thread, throwable) ->
90                         TcLog.e(TAG, "Exception from executor: " + thread, throwable))
91                 .build()));
92   }
93 
TextClassifierServiceExecutors()94   private TextClassifierServiceExecutors() {}
95 }
96