| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collection; |
| 21 |
import java.util.List; |
| 22 |
import java.util.concurrent.*; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ConcurrencyUtil { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public static <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, ExecutorService executorService) throws Throwable { |
| 33 |
if (executorService == null) { |
| 34 |
for (Callable<T> task : tasks) { |
| 35 |
task.call(); |
| 36 |
} |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
| 41 |
boolean done = false; |
| 42 |
try { |
| 43 |
for (Callable<T> t : tasks) { |
| 44 |
Future<T> future = executorService.submit(t); |
| 45 |
futures.add(future); |
| 46 |
} |
| 47 |
|
| 48 |
for (Future f : futures) { |
| 49 |
((Runnable) f).run(); |
| 50 |
} |
| 51 |
for (Future f : futures) { |
| 52 |
try { |
| 53 |
f.get(); |
| 54 |
} catch (CancellationException ignore) { |
| 55 |
} catch (ExecutionException e) { |
| 56 |
Throwable cause = e.getCause(); |
| 57 |
if (cause != null) { |
| 58 |
throw cause; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
done = true; |
| 63 |
} finally { |
| 64 |
if (!done) { |
| 65 |
for (Future f : futures) { |
| 66 |
f.cancel(false); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
return futures; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static <K, V> V cacheOrGet(ConcurrentMap<K, V> map, final K key, final V defaultValue) { |
| 79 |
V v = map.get(key); |
| 80 |
if (v != null) return v; |
| 81 |
V prev = map.putIfAbsent(key, defaultValue); |
| 82 |
return prev == null ? defaultValue : prev; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
public static ThreadPoolExecutor newSingleThreadExecutor(final String threadFactoryName) { |
| 87 |
return newSingleThreadExecutor(threadFactoryName, Thread.NORM_PRIORITY); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public static ThreadPoolExecutor newSingleThreadExecutor(final String threadFactoryName, final int threadPriority) { |
| 92 |
return new ThreadPoolExecutor(1, 1, |
| 93 |
0L, TimeUnit.MILLISECONDS, |
| 94 |
new LinkedBlockingQueue<Runnable>(), newNamedThreadFactory(threadFactoryName, true, threadPriority)); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public static ScheduledThreadPoolExecutor newSingleScheduledThreadExecutor(final String threadFactoryName) { |
| 99 |
return newSingleScheduledThreadExecutor(threadFactoryName, Thread.NORM_PRIORITY); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
public static ScheduledThreadPoolExecutor newSingleScheduledThreadExecutor(final String threadFactoryName, final int threadPriority) { |
| 104 |
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, newNamedThreadFactory(threadFactoryName, true, threadPriority)); |
| 105 |
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
| 106 |
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
| 107 |
return executor; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
public static ThreadFactory newNamedThreadFactory(final String threadName, final boolean isDaemon, final int threadPriority) { |
| 112 |
return new ThreadFactory() { |
| 113 |
|
| 114 |
@Override |
| 115 |
public Thread newThread(final Runnable r) { |
| 116 |
final Thread thread = new Thread(r, threadName); |
| 117 |
thread.setDaemon(isDaemon); |
| 118 |
thread.setPriority(threadPriority); |
| 119 |
return thread; |
| 120 |
} |
| 121 |
}; |
| 122 |
} |
| 123 |
|
| 124 |
public static ThreadFactory newNamedThreadFactory(final String threadName) { |
| 125 |
return new ThreadFactory() { |
| 126 |
|
| 127 |
@Override |
| 128 |
public Thread newThread(final Runnable r) { |
| 129 |
return new Thread(r, threadName); |
| 130 |
} |
| 131 |
}; |
| 132 |
} |
| 133 |
} |