1 /* 2 * Copyright (C) 2017 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.systemui.shared.system; 18 19 import java.util.concurrent.Callable; 20 import java.util.concurrent.ExecutorService; 21 import java.util.concurrent.Executors; 22 import java.util.concurrent.Future; 23 24 /** 25 * Offloads work from other threads by running it in a background thread. 26 */ 27 public class BackgroundExecutor { 28 29 private static final BackgroundExecutor sInstance = new BackgroundExecutor(); 30 31 private final ExecutorService mExecutorService = Executors.newFixedThreadPool(2); 32 33 /** 34 * @return the static instance of the background executor. 35 */ get()36 public static BackgroundExecutor get() { 37 return sInstance; 38 } 39 40 /** 41 * Runs the given {@param callable} on one of the background executor threads. 42 */ submit(Callable<T> callable)43 public <T> Future<T> submit(Callable<T> callable) { 44 return mExecutorService.submit(callable); 45 } 46 47 /** 48 * Runs the given {@param runnable} on one of the background executor threads. 49 */ submit(Runnable runnable)50 public Future<?> submit(Runnable runnable) { 51 return mExecutorService.submit(runnable); 52 } 53 54 /** 55 * Runs the given {@param runnable} on one of the background executor threads. Return 56 * {@param result} when the future is resolved. 57 */ submit(Runnable runnable, T result)58 public <T> Future<T> submit(Runnable runnable, T result) { 59 return mExecutorService.submit(runnable, result); 60 } 61 } 62