Debian Codegraph @

src/com/bulenkov/iconloader/util/ConcurrencyUtil.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
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.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 * @author Konstantin Bulenkov
26 */
27 public class ConcurrencyUtil {
28 /**
29 * Invokes and waits all tasks using threadPool, avoiding thread starvation on the way
30 * (see <a href="http://gafter.blogspot.com/2006/11/thread-pool-puzzler.html">"A Thread Pool Puzzler"</a>).
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 // force not started futures to execute using the current thread
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 * @return defaultValue if there is no entry in the map (in that case defaultValue is placed into the map),
75 * or corresponding value if entry already exists.
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 }