ConcurrentUtils.java revision e29a5a11529dc7df82911b48b9f95461383cbcc2
1/*
2 * Copyright (C) 2016 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
17package com.android.internal.util;
18
19import android.os.Process;
20
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.ExecutorService;
23import java.util.concurrent.Executors;
24import java.util.concurrent.Future;
25import java.util.concurrent.ThreadFactory;
26import java.util.concurrent.atomic.AtomicInteger;
27
28/**
29 * Utility methods for common functionality using java.util.concurrent package
30 *
31 * @hide
32 */
33public class ConcurrentUtils {
34
35    private ConcurrentUtils() {
36    }
37
38    /**
39     * Creates a thread pool using
40     * {@link java.util.concurrent.Executors#newFixedThreadPool(int, ThreadFactory)}
41     *
42     * @param nThreads the number of threads in the pool
43     * @param poolName base name of the threads in the pool
44     * @param linuxThreadPriority a Linux priority level. see {@link Process#setThreadPriority(int)}
45     * @return the newly created thread pool
46     */
47    public static ExecutorService newFixedThreadPool(int nThreads, String poolName,
48            int linuxThreadPriority) {
49        return Executors.newFixedThreadPool(nThreads,
50                new ThreadFactory() {
51                    private final AtomicInteger threadNum = new AtomicInteger(0);
52
53                    @Override
54                    public Thread newThread(final Runnable r) {
55                        return new Thread(poolName + threadNum.incrementAndGet()) {
56                            @Override
57                            public void run() {
58                                Process.setThreadPriority(linuxThreadPriority);
59                                r.run();
60                            }
61                        };
62                    }
63                });
64    }
65
66    /**
67     * Waits if necessary for the computation to complete, and then retrieves its result.
68     * <p>If {@code InterruptedException} occurs, this method will interrupt the current thread
69     * and throw {@code IllegalStateException}</p>
70     *
71     * @param future future to wait for result
72     * @param description short description of the operation
73     * @return the computed result
74     * @throws IllegalStateException if interrupted during wait
75     * @throws RuntimeException if an error occurs while waiting for {@link Future#get()}
76     * @see Future#get()
77     */
78    public static <T> T waitForFutureNoInterrupt(Future<T> future, String description) {
79        try {
80            return future.get();
81        } catch (InterruptedException e) {
82            Thread.currentThread().interrupt();
83            throw new IllegalStateException(description + " interrupted");
84        } catch (ExecutionException e) {
85            throw new RuntimeException(description + " failed", e);
86        }
87    }
88
89}
90