| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.os.Process; |
| 20 |
import android.util.Slog; |
| 21 |
|
| 22 |
import java.util.concurrent.CountDownLatch; |
| 23 |
import java.util.concurrent.ExecutionException; |
| 24 |
import java.util.concurrent.Executor; |
| 25 |
import java.util.concurrent.ExecutorService; |
| 26 |
import java.util.concurrent.Executors; |
| 27 |
import java.util.concurrent.Future; |
| 28 |
import java.util.concurrent.ThreadFactory; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
import java.util.concurrent.atomic.AtomicInteger; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ConcurrentUtils { |
| 38 |
|
| 39 |
private ConcurrentUtils() { |
| 40 |
} |
| 41 |
|
| 42 |
public static final Executor DIRECT_EXECUTOR = new DirectExecutor(); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static ExecutorService newFixedThreadPool(int nThreads, String poolName, |
| 54 |
int linuxThreadPriority) { |
| 55 |
return Executors.newFixedThreadPool(nThreads, |
| 56 |
new ThreadFactory() { |
| 57 |
private final AtomicInteger threadNum = new AtomicInteger(0); |
| 58 |
|
| 59 |
@Override |
| 60 |
public Thread newThread(final Runnable r) { |
| 61 |
return new Thread(poolName + threadNum.incrementAndGet()) { |
| 62 |
@Override |
| 63 |
public void run() { |
| 64 |
Process.setThreadPriority(linuxThreadPriority); |
| 65 |
r.run(); |
| 66 |
} |
| 67 |
}; |
| 68 |
} |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static <T> T waitForFutureNoInterrupt(Future<T> future, String description) { |
| 85 |
try { |
| 86 |
return future.get(); |
| 87 |
} catch (InterruptedException e) { |
| 88 |
Thread.currentThread().interrupt(); |
| 89 |
throw new IllegalStateException(description + " interrupted"); |
| 90 |
} catch (ExecutionException e) { |
| 91 |
throw new RuntimeException(description + " failed", e); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static void waitForCountDownNoInterrupt(CountDownLatch countDownLatch, long timeoutMs, |
| 108 |
String description) { |
| 109 |
try { |
| 110 |
if (!countDownLatch.await(timeoutMs, TimeUnit.MILLISECONDS)) { |
| 111 |
throw new IllegalStateException(description + " timed out."); |
| 112 |
} |
| 113 |
} catch (InterruptedException e) { |
| 114 |
Thread.currentThread().interrupt(); |
| 115 |
throw new IllegalStateException(description + " interrupted."); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static void wtfIfLockHeld(String tag, Object lock) { |
| 123 |
if (Thread.holdsLock(lock)) { |
| 124 |
Slog.wtf(tag, "Lock mustn't be held"); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static void wtfIfLockNotHeld(String tag, Object lock) { |
| 132 |
if (!Thread.holdsLock(lock)) { |
| 133 |
Slog.wtf(tag, "Lock must be held"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private static class DirectExecutor implements Executor { |
| 138 |
|
| 139 |
@Override |
| 140 |
public void execute(Runnable command) { |
| 141 |
command.run(); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public String toString() { |
| 146 |
return "DIRECT_EXECUTOR"; |
| 147 |
} |
| 148 |
} |
| 149 |
} |