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.car.telephony.common;
18 
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 
22 /**
23  * WorkerExecutor is a singleton tied to the application to provide {@link ExecutorService} for
24  * Dialer to run tasks in background.
25  */
26 public class WorkerExecutor {
27     private static WorkerExecutor sWorkerExecutor;
28 
29     private ExecutorService mSingleThreadExecutor;
30 
31     /** Returns the singleton WorkerExecutor for the application. */
getInstance()32     public static WorkerExecutor getInstance() {
33         if (sWorkerExecutor == null) {
34             sWorkerExecutor = new WorkerExecutor();
35         }
36         return sWorkerExecutor;
37     }
38 
WorkerExecutor()39     private WorkerExecutor() {
40         mSingleThreadExecutor = Executors.newSingleThreadExecutor();
41     }
42 
43     /** Returns the single thread executor. */
getSingleThreadExecutor()44     public ExecutorService getSingleThreadExecutor() {
45         return mSingleThreadExecutor;
46     }
47 
48     /** Tears down the singleton WorkerExecutor for the application */
tearDown()49     public void tearDown() {
50         mSingleThreadExecutor.shutdown();
51         sWorkerExecutor = null;
52     }
53 }
54