1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import java.util.concurrent.atomic.*; |
11 |
|
import junit.framework.*; |
12 |
< |
import java.util.List; |
12 |
> |
import java.util.*; |
13 |
|
|
14 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
37 |
|
} |
38 |
|
} |
39 |
|
|
40 |
+ |
static class FailingThreadFactory implements ThreadFactory{ |
41 |
+ |
int calls = 0; |
42 |
+ |
public Thread newThread(Runnable r){ |
43 |
+ |
if (++calls > 1) return null; |
44 |
+ |
return new Thread(r); |
45 |
+ |
} |
46 |
+ |
} |
47 |
+ |
|
48 |
+ |
|
49 |
|
/** |
50 |
|
* execute successfully executes a runnable |
51 |
|
*/ |
127 |
|
unexpectedException(); |
128 |
|
} |
129 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
130 |
< |
p2.shutdown(); |
130 |
> |
try { p2.shutdown(); } catch(SecurityException ok) { return; } |
131 |
|
joinPool(p2); |
132 |
|
} |
133 |
|
|
157 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
158 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
159 |
|
assertSame(tf, p.getThreadFactory()); |
149 |
– |
p.shutdown(); |
160 |
|
joinPool(p); |
161 |
|
} |
162 |
|
|
168 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
169 |
|
p.setThreadFactory(tf); |
170 |
|
assertSame(tf, p.getThreadFactory()); |
161 |
– |
p.shutdown(); |
171 |
|
joinPool(p); |
172 |
|
} |
173 |
|
|
193 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
194 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
195 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
187 |
– |
p.shutdown(); |
196 |
|
joinPool(p); |
197 |
|
} |
198 |
|
|
205 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
206 |
|
p.setRejectedExecutionHandler(h); |
207 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
200 |
– |
p.shutdown(); |
208 |
|
joinPool(p); |
209 |
|
} |
210 |
|
|
287 |
|
|
288 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
289 |
|
assertFalse(p1.isShutdown()); |
290 |
< |
p1.shutdown(); |
290 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
291 |
|
assertTrue(p1.isShutdown()); |
292 |
|
joinPool(p1); |
293 |
|
} |
302 |
|
try { |
303 |
|
p1.execute(new MediumRunnable()); |
304 |
|
} finally { |
305 |
< |
p1.shutdown(); |
305 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
306 |
|
} |
307 |
|
try { |
308 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
322 |
|
p1.execute(new SmallRunnable()); |
323 |
|
assertFalse(p1.isTerminating()); |
324 |
|
} finally { |
325 |
< |
p1.shutdown(); |
325 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
326 |
|
} |
327 |
|
try { |
328 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
350 |
|
assertSame(q, wq); |
351 |
|
assertFalse(wq.contains(tasks[0])); |
352 |
|
assertTrue(wq.contains(tasks[4])); |
353 |
+ |
for (int i = 1; i < 5; ++i) |
354 |
+ |
tasks[i].cancel(true); |
355 |
|
p1.shutdownNow(); |
356 |
|
} catch(Exception e) { |
357 |
|
unexpectedException(); |
382 |
|
assertTrue(q.contains(tasks[3])); |
383 |
|
assertTrue(p1.remove(tasks[3])); |
384 |
|
assertFalse(q.contains(tasks[3])); |
376 |
– |
p1.shutdownNow(); |
385 |
|
} catch(Exception e) { |
386 |
|
unexpectedException(); |
387 |
|
} finally { |
404 |
|
p1.purge(); |
405 |
|
long count = p1.getTaskCount(); |
406 |
|
assertTrue(count >= 2 && count < 5); |
399 |
– |
p1.shutdownNow(); |
407 |
|
joinPool(p1); |
408 |
|
} |
409 |
|
|
418 |
|
p1.execute(new MediumPossiblyInterruptedRunnable()); |
419 |
|
} |
420 |
|
finally { |
421 |
< |
l = p1.shutdownNow(); |
421 |
> |
try { |
422 |
> |
l = p1.shutdownNow(); |
423 |
> |
} catch (SecurityException ok) { return; } |
424 |
> |
|
425 |
|
} |
426 |
|
assertTrue(p1.isShutdown()); |
427 |
|
assertTrue(l.size() <= 4); |
782 |
|
for(int i = 1; i < 5; ++i) { |
783 |
|
assertTrue(tasks[i].done); |
784 |
|
} |
785 |
< |
p.shutdownNow(); |
785 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
786 |
|
} catch(RejectedExecutionException ex){ |
787 |
|
unexpectedException(); |
788 |
|
} finally { |
809 |
|
for(int i = 0; i < 5; ++i){ |
810 |
|
assertFalse(tasks[i].done); |
811 |
|
} |
812 |
< |
p.shutdownNow(); |
812 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
813 |
|
} catch(RejectedExecutionException ex){ |
814 |
|
unexpectedException(); |
815 |
|
} finally { |
832 |
|
p.execute(r3); |
833 |
|
assertFalse(p.getQueue().contains(r2)); |
834 |
|
assertTrue(p.getQueue().contains(r3)); |
835 |
< |
p.shutdownNow(); |
835 |
> |
try { p.shutdownNow(); } catch(SecurityException ok) { return; } |
836 |
|
} catch(RejectedExecutionException ex){ |
837 |
|
unexpectedException(); |
838 |
|
} finally { |
846 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
847 |
|
ThreadPoolExecutor tpe = |
848 |
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
849 |
< |
tpe.shutdown(); |
849 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
850 |
|
try { |
851 |
|
tpe.execute(new NoOpRunnable()); |
852 |
|
shouldThrow(); |
862 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
863 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
864 |
|
|
865 |
< |
p.shutdown(); |
865 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
866 |
|
try { |
867 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
868 |
|
p.execute(r); |
881 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
882 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
883 |
|
|
884 |
< |
p.shutdown(); |
884 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
885 |
|
try { |
886 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
887 |
|
p.execute(r); |
901 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
902 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
903 |
|
|
904 |
< |
p.shutdown(); |
904 |
> |
try { p.shutdown(); } catch(SecurityException ok) { return; } |
905 |
|
try { |
906 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
907 |
|
p.execute(r); |
941 |
|
shouldThrow(); |
942 |
|
} catch(IllegalArgumentException success){ |
943 |
|
} finally { |
944 |
< |
tpe.shutdown(); |
944 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
945 |
|
} |
946 |
|
joinPool(tpe); |
947 |
|
} |
960 |
|
shouldThrow(); |
961 |
|
} catch(IllegalArgumentException success){ |
962 |
|
} finally { |
963 |
< |
tpe.shutdown(); |
963 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
964 |
|
} |
965 |
|
joinPool(tpe); |
966 |
|
} |
979 |
|
shouldThrow(); |
980 |
|
} catch(IllegalArgumentException success){ |
981 |
|
} finally { |
982 |
< |
tpe.shutdown(); |
982 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
983 |
|
} |
984 |
|
joinPool(tpe); |
985 |
|
} |
1000 |
|
shouldThrow(); |
1001 |
|
} catch(IllegalArgumentException success){ |
1002 |
|
} finally { |
1003 |
< |
tpe.shutdown(); |
1003 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1004 |
|
} |
1005 |
|
joinPool(tpe); |
1006 |
|
} |
1010 |
|
*/ |
1011 |
|
public void testTerminated() { |
1012 |
|
ExtendedTPE tpe = new ExtendedTPE(); |
1013 |
< |
tpe.shutdown(); |
1013 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1014 |
|
assertTrue(tpe.terminatedCalled); |
1015 |
|
joinPool(tpe); |
1016 |
|
} |
1027 |
|
assertTrue(r.done); |
1028 |
|
assertTrue(tpe.beforeCalled); |
1029 |
|
assertTrue(tpe.afterCalled); |
1030 |
< |
tpe.shutdown(); |
1030 |
> |
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1031 |
|
} |
1032 |
|
catch(Exception ex) { |
1033 |
|
unexpectedException(); |
1035 |
|
joinPool(tpe); |
1036 |
|
} |
1037 |
|
} |
1038 |
+ |
|
1039 |
+ |
/** |
1040 |
+ |
* completed submit of callable returns result |
1041 |
+ |
*/ |
1042 |
+ |
public void testSubmitCallable() { |
1043 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1044 |
+ |
try { |
1045 |
+ |
Future<String> future = e.submit(new StringTask()); |
1046 |
+ |
String result = future.get(); |
1047 |
+ |
assertSame(TEST_STRING, result); |
1048 |
+ |
} |
1049 |
+ |
catch (ExecutionException ex) { |
1050 |
+ |
unexpectedException(); |
1051 |
+ |
} |
1052 |
+ |
catch (InterruptedException ex) { |
1053 |
+ |
unexpectedException(); |
1054 |
+ |
} finally { |
1055 |
+ |
joinPool(e); |
1056 |
+ |
} |
1057 |
+ |
} |
1058 |
+ |
|
1059 |
+ |
/** |
1060 |
+ |
* completed submit of runnable returns successfully |
1061 |
+ |
*/ |
1062 |
+ |
public void testSubmitRunnable() { |
1063 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1064 |
+ |
try { |
1065 |
+ |
Future<?> future = e.submit(new NoOpRunnable()); |
1066 |
+ |
future.get(); |
1067 |
+ |
assertTrue(future.isDone()); |
1068 |
+ |
} |
1069 |
+ |
catch (ExecutionException ex) { |
1070 |
+ |
unexpectedException(); |
1071 |
+ |
} |
1072 |
+ |
catch (InterruptedException ex) { |
1073 |
+ |
unexpectedException(); |
1074 |
+ |
} finally { |
1075 |
+ |
joinPool(e); |
1076 |
+ |
} |
1077 |
+ |
} |
1078 |
+ |
|
1079 |
+ |
/** |
1080 |
+ |
* completed submit of (runnable, result) returns result |
1081 |
+ |
*/ |
1082 |
+ |
public void testSubmitRunnable2() { |
1083 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1084 |
+ |
try { |
1085 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1086 |
+ |
String result = future.get(); |
1087 |
+ |
assertSame(TEST_STRING, result); |
1088 |
+ |
} |
1089 |
+ |
catch (ExecutionException ex) { |
1090 |
+ |
unexpectedException(); |
1091 |
+ |
} |
1092 |
+ |
catch (InterruptedException ex) { |
1093 |
+ |
unexpectedException(); |
1094 |
+ |
} finally { |
1095 |
+ |
joinPool(e); |
1096 |
+ |
} |
1097 |
+ |
} |
1098 |
+ |
|
1099 |
+ |
|
1100 |
+ |
|
1101 |
+ |
|
1102 |
+ |
|
1103 |
+ |
/** |
1104 |
+ |
* invokeAny(null) throws NPE |
1105 |
+ |
*/ |
1106 |
+ |
public void testInvokeAny1() { |
1107 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1108 |
+ |
try { |
1109 |
+ |
e.invokeAny(null); |
1110 |
+ |
} catch (NullPointerException success) { |
1111 |
+ |
} catch(Exception ex) { |
1112 |
+ |
unexpectedException(); |
1113 |
+ |
} finally { |
1114 |
+ |
joinPool(e); |
1115 |
+ |
} |
1116 |
+ |
} |
1117 |
+ |
|
1118 |
+ |
/** |
1119 |
+ |
* invokeAny(empty collection) throws IAE |
1120 |
+ |
*/ |
1121 |
+ |
public void testInvokeAny2() { |
1122 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1123 |
+ |
try { |
1124 |
+ |
e.invokeAny(new ArrayList<Callable<String>>()); |
1125 |
+ |
} catch (IllegalArgumentException success) { |
1126 |
+ |
} catch(Exception ex) { |
1127 |
+ |
unexpectedException(); |
1128 |
+ |
} finally { |
1129 |
+ |
joinPool(e); |
1130 |
+ |
} |
1131 |
+ |
} |
1132 |
+ |
|
1133 |
+ |
/** |
1134 |
+ |
* invokeAny(c) throws NPE if c has null elements |
1135 |
+ |
*/ |
1136 |
+ |
public void testInvokeAny3() { |
1137 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1138 |
+ |
try { |
1139 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1140 |
+ |
l.add(new StringTask()); |
1141 |
+ |
l.add(null); |
1142 |
+ |
e.invokeAny(l); |
1143 |
+ |
} catch (NullPointerException success) { |
1144 |
+ |
} catch(Exception ex) { |
1145 |
+ |
unexpectedException(); |
1146 |
+ |
} finally { |
1147 |
+ |
joinPool(e); |
1148 |
+ |
} |
1149 |
+ |
} |
1150 |
+ |
|
1151 |
+ |
/** |
1152 |
+ |
* invokeAny(c) throws ExecutionException if no task completes |
1153 |
+ |
*/ |
1154 |
+ |
public void testInvokeAny4() { |
1155 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1156 |
+ |
try { |
1157 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1158 |
+ |
l.add(new NPETask()); |
1159 |
+ |
e.invokeAny(l); |
1160 |
+ |
} catch (ExecutionException success) { |
1161 |
+ |
} catch(Exception ex) { |
1162 |
+ |
unexpectedException(); |
1163 |
+ |
} finally { |
1164 |
+ |
joinPool(e); |
1165 |
+ |
} |
1166 |
+ |
} |
1167 |
+ |
|
1168 |
+ |
/** |
1169 |
+ |
* invokeAny(c) returns result of some task |
1170 |
+ |
*/ |
1171 |
+ |
public void testInvokeAny5() { |
1172 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1173 |
+ |
try { |
1174 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1175 |
+ |
l.add(new StringTask()); |
1176 |
+ |
l.add(new StringTask()); |
1177 |
+ |
String result = e.invokeAny(l); |
1178 |
+ |
assertSame(TEST_STRING, result); |
1179 |
+ |
} catch (ExecutionException success) { |
1180 |
+ |
} catch(Exception ex) { |
1181 |
+ |
unexpectedException(); |
1182 |
+ |
} finally { |
1183 |
+ |
joinPool(e); |
1184 |
+ |
} |
1185 |
+ |
} |
1186 |
+ |
|
1187 |
+ |
/** |
1188 |
+ |
* invokeAll(null) throws NPE |
1189 |
+ |
*/ |
1190 |
+ |
public void testInvokeAll1() { |
1191 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1192 |
+ |
try { |
1193 |
+ |
e.invokeAll(null); |
1194 |
+ |
} catch (NullPointerException success) { |
1195 |
+ |
} catch(Exception ex) { |
1196 |
+ |
unexpectedException(); |
1197 |
+ |
} finally { |
1198 |
+ |
joinPool(e); |
1199 |
+ |
} |
1200 |
+ |
} |
1201 |
+ |
|
1202 |
+ |
/** |
1203 |
+ |
* invokeAll(empty collection) returns empty collection |
1204 |
+ |
*/ |
1205 |
+ |
public void testInvokeAll2() { |
1206 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1207 |
+ |
try { |
1208 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1209 |
+ |
assertTrue(r.isEmpty()); |
1210 |
+ |
} catch(Exception ex) { |
1211 |
+ |
unexpectedException(); |
1212 |
+ |
} finally { |
1213 |
+ |
joinPool(e); |
1214 |
+ |
} |
1215 |
+ |
} |
1216 |
+ |
|
1217 |
+ |
/** |
1218 |
+ |
* invokeAll(c) throws NPE if c has null elements |
1219 |
+ |
*/ |
1220 |
+ |
public void testInvokeAll3() { |
1221 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1222 |
+ |
try { |
1223 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1224 |
+ |
l.add(new StringTask()); |
1225 |
+ |
l.add(null); |
1226 |
+ |
e.invokeAll(l); |
1227 |
+ |
} catch (NullPointerException success) { |
1228 |
+ |
} catch(Exception ex) { |
1229 |
+ |
unexpectedException(); |
1230 |
+ |
} finally { |
1231 |
+ |
joinPool(e); |
1232 |
+ |
} |
1233 |
+ |
} |
1234 |
+ |
|
1235 |
+ |
/** |
1236 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1237 |
+ |
*/ |
1238 |
+ |
public void testInvokeAll4() { |
1239 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1240 |
+ |
try { |
1241 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1242 |
+ |
l.add(new NPETask()); |
1243 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1244 |
+ |
assertEquals(1, result.size()); |
1245 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1246 |
+ |
it.next().get(); |
1247 |
+ |
} catch(ExecutionException success) { |
1248 |
+ |
} catch(Exception ex) { |
1249 |
+ |
unexpectedException(); |
1250 |
+ |
} finally { |
1251 |
+ |
joinPool(e); |
1252 |
+ |
} |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
/** |
1256 |
+ |
* invokeAll(c) returns results of all completed tasks |
1257 |
+ |
*/ |
1258 |
+ |
public void testInvokeAll5() { |
1259 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1260 |
+ |
try { |
1261 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1262 |
+ |
l.add(new StringTask()); |
1263 |
+ |
l.add(new StringTask()); |
1264 |
+ |
List<Future<String>> result = e.invokeAll(l); |
1265 |
+ |
assertEquals(2, result.size()); |
1266 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1267 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1268 |
+ |
} catch (ExecutionException success) { |
1269 |
+ |
} catch(Exception ex) { |
1270 |
+ |
unexpectedException(); |
1271 |
+ |
} finally { |
1272 |
+ |
joinPool(e); |
1273 |
+ |
} |
1274 |
+ |
} |
1275 |
+ |
|
1276 |
+ |
|
1277 |
+ |
|
1278 |
+ |
/** |
1279 |
+ |
* timed invokeAny(null) throws NPE |
1280 |
+ |
*/ |
1281 |
+ |
public void testTimedInvokeAny1() { |
1282 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1283 |
+ |
try { |
1284 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1285 |
+ |
} catch (NullPointerException success) { |
1286 |
+ |
} catch(Exception ex) { |
1287 |
+ |
unexpectedException(); |
1288 |
+ |
} finally { |
1289 |
+ |
joinPool(e); |
1290 |
+ |
} |
1291 |
+ |
} |
1292 |
+ |
|
1293 |
+ |
/** |
1294 |
+ |
* timed invokeAny(,,null) throws NPE |
1295 |
+ |
*/ |
1296 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
1297 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1298 |
+ |
try { |
1299 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1300 |
+ |
l.add(new StringTask()); |
1301 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1302 |
+ |
} catch (NullPointerException success) { |
1303 |
+ |
} catch(Exception ex) { |
1304 |
+ |
unexpectedException(); |
1305 |
+ |
} finally { |
1306 |
+ |
joinPool(e); |
1307 |
+ |
} |
1308 |
+ |
} |
1309 |
+ |
|
1310 |
+ |
/** |
1311 |
+ |
* timed invokeAny(empty collection) throws IAE |
1312 |
+ |
*/ |
1313 |
+ |
public void testTimedInvokeAny2() { |
1314 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1315 |
+ |
try { |
1316 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1317 |
+ |
} catch (IllegalArgumentException success) { |
1318 |
+ |
} catch(Exception ex) { |
1319 |
+ |
unexpectedException(); |
1320 |
+ |
} finally { |
1321 |
+ |
joinPool(e); |
1322 |
+ |
} |
1323 |
+ |
} |
1324 |
+ |
|
1325 |
+ |
/** |
1326 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
1327 |
+ |
*/ |
1328 |
+ |
public void testTimedInvokeAny3() { |
1329 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1330 |
+ |
try { |
1331 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1332 |
+ |
l.add(new StringTask()); |
1333 |
+ |
l.add(null); |
1334 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1335 |
+ |
} catch (NullPointerException success) { |
1336 |
+ |
} catch(Exception ex) { |
1337 |
+ |
ex.printStackTrace(); |
1338 |
+ |
unexpectedException(); |
1339 |
+ |
} finally { |
1340 |
+ |
joinPool(e); |
1341 |
+ |
} |
1342 |
+ |
} |
1343 |
+ |
|
1344 |
+ |
/** |
1345 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
1346 |
+ |
*/ |
1347 |
+ |
public void testTimedInvokeAny4() { |
1348 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1349 |
+ |
try { |
1350 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1351 |
+ |
l.add(new NPETask()); |
1352 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1353 |
+ |
} catch(ExecutionException success) { |
1354 |
+ |
} catch(Exception ex) { |
1355 |
+ |
unexpectedException(); |
1356 |
+ |
} finally { |
1357 |
+ |
joinPool(e); |
1358 |
+ |
} |
1359 |
+ |
} |
1360 |
+ |
|
1361 |
+ |
/** |
1362 |
+ |
* timed invokeAny(c) returns result of some task |
1363 |
+ |
*/ |
1364 |
+ |
public void testTimedInvokeAny5() { |
1365 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1366 |
+ |
try { |
1367 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1368 |
+ |
l.add(new StringTask()); |
1369 |
+ |
l.add(new StringTask()); |
1370 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1371 |
+ |
assertSame(TEST_STRING, result); |
1372 |
+ |
} catch (ExecutionException success) { |
1373 |
+ |
} catch(Exception ex) { |
1374 |
+ |
unexpectedException(); |
1375 |
+ |
} finally { |
1376 |
+ |
joinPool(e); |
1377 |
+ |
} |
1378 |
+ |
} |
1379 |
+ |
|
1380 |
+ |
/** |
1381 |
+ |
* timed invokeAll(null) throws NPE |
1382 |
+ |
*/ |
1383 |
+ |
public void testTimedInvokeAll1() { |
1384 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1385 |
+ |
try { |
1386 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1387 |
+ |
} catch (NullPointerException success) { |
1388 |
+ |
} catch(Exception ex) { |
1389 |
+ |
unexpectedException(); |
1390 |
+ |
} finally { |
1391 |
+ |
joinPool(e); |
1392 |
+ |
} |
1393 |
+ |
} |
1394 |
+ |
|
1395 |
+ |
/** |
1396 |
+ |
* timed invokeAll(,,null) throws NPE |
1397 |
+ |
*/ |
1398 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
1399 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1400 |
+ |
try { |
1401 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1402 |
+ |
l.add(new StringTask()); |
1403 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1404 |
+ |
} catch (NullPointerException success) { |
1405 |
+ |
} catch(Exception ex) { |
1406 |
+ |
unexpectedException(); |
1407 |
+ |
} finally { |
1408 |
+ |
joinPool(e); |
1409 |
+ |
} |
1410 |
+ |
} |
1411 |
+ |
|
1412 |
+ |
/** |
1413 |
+ |
* timed invokeAll(empty collection) returns empty collection |
1414 |
+ |
*/ |
1415 |
+ |
public void testTimedInvokeAll2() { |
1416 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1417 |
+ |
try { |
1418 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1419 |
+ |
assertTrue(r.isEmpty()); |
1420 |
+ |
} catch(Exception ex) { |
1421 |
+ |
unexpectedException(); |
1422 |
+ |
} finally { |
1423 |
+ |
joinPool(e); |
1424 |
+ |
} |
1425 |
+ |
} |
1426 |
+ |
|
1427 |
+ |
/** |
1428 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1429 |
+ |
*/ |
1430 |
+ |
public void testTimedInvokeAll3() { |
1431 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1432 |
+ |
try { |
1433 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1434 |
+ |
l.add(new StringTask()); |
1435 |
+ |
l.add(null); |
1436 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1437 |
+ |
} catch (NullPointerException success) { |
1438 |
+ |
} catch(Exception ex) { |
1439 |
+ |
unexpectedException(); |
1440 |
+ |
} finally { |
1441 |
+ |
joinPool(e); |
1442 |
+ |
} |
1443 |
+ |
} |
1444 |
+ |
|
1445 |
+ |
/** |
1446 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1447 |
+ |
*/ |
1448 |
+ |
public void testTimedInvokeAll4() { |
1449 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1450 |
+ |
try { |
1451 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1452 |
+ |
l.add(new NPETask()); |
1453 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1454 |
+ |
assertEquals(1, result.size()); |
1455 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1456 |
+ |
it.next().get(); |
1457 |
+ |
} catch(ExecutionException success) { |
1458 |
+ |
} catch(Exception ex) { |
1459 |
+ |
unexpectedException(); |
1460 |
+ |
} finally { |
1461 |
+ |
joinPool(e); |
1462 |
+ |
} |
1463 |
+ |
} |
1464 |
+ |
|
1465 |
+ |
/** |
1466 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1467 |
+ |
*/ |
1468 |
+ |
public void testTimedInvokeAll5() { |
1469 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1470 |
+ |
try { |
1471 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1472 |
+ |
l.add(new StringTask()); |
1473 |
+ |
l.add(new StringTask()); |
1474 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1475 |
+ |
assertEquals(2, result.size()); |
1476 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1477 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1478 |
+ |
} catch (ExecutionException success) { |
1479 |
+ |
} catch(Exception ex) { |
1480 |
+ |
unexpectedException(); |
1481 |
+ |
} finally { |
1482 |
+ |
joinPool(e); |
1483 |
+ |
} |
1484 |
+ |
} |
1485 |
+ |
|
1486 |
+ |
/** |
1487 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1488 |
+ |
*/ |
1489 |
+ |
public void testTimedInvokeAll6() { |
1490 |
+ |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1491 |
+ |
try { |
1492 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1493 |
+ |
l.add(new StringTask()); |
1494 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1495 |
+ |
l.add(new StringTask()); |
1496 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1497 |
+ |
assertEquals(3, result.size()); |
1498 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1499 |
+ |
Future<String> f1 = it.next(); |
1500 |
+ |
Future<String> f2 = it.next(); |
1501 |
+ |
Future<String> f3 = it.next(); |
1502 |
+ |
assertTrue(f1.isDone()); |
1503 |
+ |
assertTrue(f2.isDone()); |
1504 |
+ |
assertTrue(f3.isDone()); |
1505 |
+ |
assertFalse(f1.isCancelled()); |
1506 |
+ |
assertTrue(f2.isCancelled()); |
1507 |
+ |
} catch(Exception ex) { |
1508 |
+ |
unexpectedException(); |
1509 |
+ |
} finally { |
1510 |
+ |
joinPool(e); |
1511 |
+ |
} |
1512 |
+ |
} |
1513 |
+ |
|
1514 |
+ |
/** |
1515 |
+ |
* Execution continues if there is at least one thread even if |
1516 |
+ |
* thread factory fails to create more |
1517 |
+ |
*/ |
1518 |
+ |
public void testFailingThreadFactory() { |
1519 |
+ |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1520 |
+ |
try { |
1521 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1522 |
+ |
for (int k = 0; k < 100; ++k) { |
1523 |
+ |
e.execute(new NoOpRunnable()); |
1524 |
+ |
} |
1525 |
+ |
Thread.sleep(LONG_DELAY_MS); |
1526 |
+ |
} catch(Exception ex) { |
1527 |
+ |
unexpectedException(); |
1528 |
+ |
} finally { |
1529 |
+ |
joinPool(e); |
1530 |
+ |
} |
1531 |
+ |
} |
1532 |
+ |
|
1533 |
+ |
/** |
1534 |
+ |
* allowsCoreThreadTimeOut is by default false. |
1535 |
+ |
*/ |
1536 |
+ |
public void testAllowsCoreThreadTimeOut() { |
1537 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1538 |
+ |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1539 |
+ |
joinPool(tpe); |
1540 |
+ |
} |
1541 |
+ |
|
1542 |
+ |
/** |
1543 |
+ |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1544 |
+ |
*/ |
1545 |
+ |
public void testAllowCoreThreadTimeOut_true() { |
1546 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1547 |
+ |
tpe.allowCoreThreadTimeOut(true); |
1548 |
+ |
tpe.execute(new NoOpRunnable()); |
1549 |
+ |
try { |
1550 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1551 |
+ |
assertEquals(0, tpe.getPoolSize()); |
1552 |
+ |
} catch(InterruptedException e){ |
1553 |
+ |
unexpectedException(); |
1554 |
+ |
} finally { |
1555 |
+ |
joinPool(tpe); |
1556 |
+ |
} |
1557 |
+ |
} |
1558 |
+ |
|
1559 |
+ |
/** |
1560 |
+ |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1561 |
+ |
*/ |
1562 |
+ |
public void testAllowCoreThreadTimeOut_false() { |
1563 |
+ |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1564 |
+ |
tpe.allowCoreThreadTimeOut(false); |
1565 |
+ |
tpe.execute(new NoOpRunnable()); |
1566 |
+ |
try { |
1567 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
1568 |
+ |
assertTrue(tpe.getPoolSize() >= 1); |
1569 |
+ |
} catch(InterruptedException e){ |
1570 |
+ |
unexpectedException(); |
1571 |
+ |
} finally { |
1572 |
+ |
joinPool(tpe); |
1573 |
+ |
} |
1574 |
+ |
} |
1575 |
+ |
|
1576 |
+ |
/** |
1577 |
+ |
* execute allows the same task to be submitted multiple times, even |
1578 |
+ |
* if rejected |
1579 |
+ |
*/ |
1580 |
+ |
public void testRejectedRecycledTask() { |
1581 |
+ |
final int nTasks = 1000; |
1582 |
+ |
final AtomicInteger nRun = new AtomicInteger(0); |
1583 |
+ |
final Runnable recycledTask = new Runnable() { |
1584 |
+ |
public void run() { |
1585 |
+ |
nRun.getAndIncrement(); |
1586 |
+ |
} }; |
1587 |
+ |
final ThreadPoolExecutor p = |
1588 |
+ |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1589 |
+ |
new ArrayBlockingQueue(30)); |
1590 |
+ |
try { |
1591 |
+ |
for (int i = 0; i < nTasks; ++i) { |
1592 |
+ |
for (;;) { |
1593 |
+ |
try { |
1594 |
+ |
p.execute(recycledTask); |
1595 |
+ |
break; |
1596 |
+ |
} |
1597 |
+ |
catch (RejectedExecutionException ignore) { |
1598 |
+ |
} |
1599 |
+ |
} |
1600 |
+ |
} |
1601 |
+ |
Thread.sleep(5000); // enough time to run all tasks |
1602 |
+ |
assertEquals(nRun.get(), nTasks); |
1603 |
+ |
} catch(Exception ex) { |
1604 |
+ |
ex.printStackTrace(); |
1605 |
+ |
unexpectedException(); |
1606 |
+ |
} finally { |
1607 |
+ |
p.shutdown(); |
1608 |
+ |
} |
1609 |
+ |
} |
1610 |
+ |
|
1611 |
|
} |