101 |
|
} |
102 |
|
lock.lock(); |
103 |
|
try { |
104 |
< |
result = v; |
105 |
< |
exception = e; |
106 |
< |
done = true; |
107 |
< |
thread = null; |
108 |
< |
cond.signalAll(); |
104 |
> |
if (!done) { |
105 |
> |
result = v; |
106 |
> |
exception = e; |
107 |
> |
done = true; |
108 |
> |
thread = null; |
109 |
> |
cond.signalAll(); |
110 |
> |
} |
111 |
|
} |
112 |
|
finally { lock.unlock(); } |
113 |
|
} |
116 |
|
try { |
117 |
|
while (!done) |
118 |
|
cond.await(); |
119 |
+ |
if (cancelled) |
120 |
+ |
throw new CancellationException(); |
121 |
|
if (exception != null) |
122 |
|
throw new ExecutionException(exception); |
123 |
|
return result; |
129 |
|
long nanos = unit.toNanos(timeout); |
130 |
|
lock.lock(); |
131 |
|
try { |
132 |
< |
for (;;) { |
133 |
< |
if (done) break; |
130 |
< |
if (nanos < 0) |
132 |
> |
while (!done) { |
133 |
> |
if (nanos <= 0L) |
134 |
|
throw new TimeoutException(); |
135 |
|
nanos = cond.awaitNanos(nanos); |
136 |
|
} |
137 |
+ |
if (cancelled) |
138 |
+ |
throw new CancellationException(); |
139 |
|
if (exception != null) |
140 |
|
throw new ExecutionException(exception); |
141 |
|
return result; |
230 |
|
* execute successfully executes a runnable |
231 |
|
*/ |
232 |
|
public void testExecute() throws InterruptedException { |
233 |
< |
final ThreadPoolExecutor p = |
234 |
< |
new CustomTPE(1, 1, |
235 |
< |
LONG_DELAY_MS, MILLISECONDS, |
236 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
237 |
< |
final CountDownLatch done = new CountDownLatch(1); |
238 |
< |
final Runnable task = new CheckedRunnable() { |
239 |
< |
public void realRun() { |
240 |
< |
done.countDown(); |
241 |
< |
}}; |
237 |
< |
try { |
233 |
> |
try (PoolCleaner<CustomTPE> cleaner = |
234 |
> |
cleaner(new CustomTPE(1, 1, |
235 |
> |
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
> |
new ArrayBlockingQueue<Runnable>(10)))) { |
237 |
> |
final ThreadPoolExecutor p = cleaner.pool; |
238 |
> |
final CountDownLatch done = new CountDownLatch(1); |
239 |
> |
final Runnable task = new CheckedRunnable() { |
240 |
> |
public void realRun() { done.countDown(); |
241 |
> |
}}; |
242 |
|
p.execute(task); |
243 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
240 |
< |
} finally { |
241 |
< |
joinPool(p); |
243 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
244 |
|
} |
245 |
|
} |
246 |
|
|
713 |
|
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
714 |
|
new ArrayBlockingQueue<Runnable>(10)); |
715 |
|
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
716 |
< |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
716 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
717 |
|
threadsStarted.countDown(); |
718 |
|
try { |
719 |
|
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
723 |
|
for (int i = 0; i < count; i++) |
724 |
|
p.execute(waiter); |
725 |
|
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
726 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
727 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
728 |
|
final List<Runnable> queuedTasks; |
729 |
|
try { |
730 |
|
queuedTasks = p.shutdownNow(); |
737 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
738 |
|
assertTrue(p.isTerminated()); |
739 |
|
assertEquals(poolSize, ran.get()); |
740 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
741 |
|
} |
742 |
|
|
743 |
|
// Exception Tests |
1746 |
|
l.add(new StringTask()); |
1747 |
|
l.add(new StringTask()); |
1748 |
|
List<Future<String>> futures = |
1749 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1749 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1750 |
|
assertEquals(2, futures.size()); |
1751 |
|
for (Future<String> future : futures) |
1752 |
|
assertSame(TEST_STRING, future.get()); |
1876 |
|
} |
1877 |
|
} |
1878 |
|
|
1879 |
+ |
/** |
1880 |
+ |
* get(cancelled task) throws CancellationException |
1881 |
+ |
* (in part, a test of CustomTPE itself) |
1882 |
+ |
*/ |
1883 |
+ |
public void testGet_cancelled() throws Exception { |
1884 |
+ |
final ExecutorService e = |
1885 |
+ |
new CustomTPE(1, 1, |
1886 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1887 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1888 |
+ |
try { |
1889 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1890 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1891 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1892 |
+ |
for (int i = 0; i < 2; i++) { |
1893 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1894 |
+ |
throws Throwable { |
1895 |
+ |
blockerStarted.countDown(); |
1896 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1897 |
+ |
}}; |
1898 |
+ |
futures.add(e.submit(r)); |
1899 |
+ |
} |
1900 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1901 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1902 |
+ |
for (Future<?> future : futures) { |
1903 |
+ |
try { |
1904 |
+ |
future.get(); |
1905 |
+ |
shouldThrow(); |
1906 |
+ |
} catch (CancellationException success) {} |
1907 |
+ |
try { |
1908 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1909 |
+ |
shouldThrow(); |
1910 |
+ |
} catch (CancellationException success) {} |
1911 |
+ |
assertTrue(future.isCancelled()); |
1912 |
+ |
assertTrue(future.isDone()); |
1913 |
+ |
} |
1914 |
+ |
done.countDown(); |
1915 |
+ |
} finally { |
1916 |
+ |
joinPool(e); |
1917 |
+ |
} |
1918 |
+ |
} |
1919 |
+ |
|
1920 |
|
} |