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 (;;) { |
129 |
< |
if (done) break; |
132 |
> |
while (!done) { |
133 |
|
if (nanos < 0) |
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; |
1879 |
|
} |
1880 |
|
} |
1881 |
|
|
1882 |
+ |
/** |
1883 |
+ |
* get(cancelled task) throws CancellationException |
1884 |
+ |
* (in part, a test of CustomTPE itself) |
1885 |
+ |
*/ |
1886 |
+ |
public void testGet_cancelled() throws Exception { |
1887 |
+ |
final ExecutorService e = |
1888 |
+ |
new CustomTPE(1, 1, |
1889 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1890 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1891 |
+ |
try { |
1892 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1893 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1894 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1895 |
+ |
for (int i = 0; i < 2; i++) { |
1896 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1897 |
+ |
throws Throwable { |
1898 |
+ |
blockerStarted.countDown(); |
1899 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1900 |
+ |
}}; |
1901 |
+ |
futures.add(e.submit(r)); |
1902 |
+ |
} |
1903 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1904 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1905 |
+ |
for (Future<?> future : futures) { |
1906 |
+ |
try { |
1907 |
+ |
future.get(); |
1908 |
+ |
shouldThrow(); |
1909 |
+ |
} catch (CancellationException success) {} |
1910 |
+ |
try { |
1911 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1912 |
+ |
shouldThrow(); |
1913 |
+ |
} catch (CancellationException success) {} |
1914 |
+ |
assertTrue(future.isCancelled()); |
1915 |
+ |
assertTrue(future.isDone()); |
1916 |
+ |
} |
1917 |
+ |
done.countDown(); |
1918 |
+ |
} finally { |
1919 |
+ |
joinPool(e); |
1920 |
+ |
} |
1921 |
+ |
} |
1922 |
+ |
|
1923 |
|
} |