14 |
|
import java.util.concurrent.ArrayBlockingQueue; |
15 |
|
import java.util.concurrent.BlockingQueue; |
16 |
|
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
|
import java.util.concurrent.Executors; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
|
import java.util.concurrent.TimeUnit; |
33 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
|
import java.util.concurrent.locks.Condition; |
35 |
|
import java.util.concurrent.locks.ReentrantLock; |
36 |
|
|
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; |
128 |
< |
if (nanos < 0) |
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; |
352 |
|
*/ |
353 |
|
public void testGetKeepAliveTime() { |
354 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
355 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
355 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
356 |
|
joinPool(p); |
357 |
|
} |
358 |
|
|
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* shutdownNow returns a list containing tasks that were not run |
708 |
> |
* shutdownNow returns a list containing tasks that were not run, |
709 |
> |
* and those tasks are drained from the queue |
710 |
|
*/ |
711 |
< |
public void testShutdownNow() { |
712 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
713 |
< |
List l; |
714 |
< |
try { |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
717 |
< |
} |
718 |
< |
finally { |
711 |
> |
public void testShutdownNow() throws InterruptedException { |
712 |
> |
final int poolSize = 2; |
713 |
> |
final int count = 5; |
714 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
715 |
> |
ThreadPoolExecutor p = |
716 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
717 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
718 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
719 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
720 |
> |
threadsStarted.countDown(); |
721 |
|
try { |
722 |
< |
l = p.shutdownNow(); |
723 |
< |
} catch (SecurityException ok) { return; } |
722 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
723 |
> |
} catch (InterruptedException success) {} |
724 |
> |
ran.getAndIncrement(); |
725 |
> |
}}; |
726 |
> |
for (int i = 0; i < count; i++) |
727 |
> |
p.execute(waiter); |
728 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
729 |
> |
assertEquals(poolSize, p.getActiveCount()); |
730 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
731 |
> |
final List<Runnable> queuedTasks; |
732 |
> |
try { |
733 |
> |
queuedTasks = p.shutdownNow(); |
734 |
> |
} catch (SecurityException ok) { |
735 |
> |
return; // Allowed in case test doesn't have privs |
736 |
|
} |
737 |
|
assertTrue(p.isShutdown()); |
738 |
< |
assertTrue(l.size() <= 4); |
738 |
> |
assertTrue(p.getQueue().isEmpty()); |
739 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
740 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
741 |
> |
assertTrue(p.isTerminated()); |
742 |
> |
assertEquals(poolSize, ran.get()); |
743 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
744 |
|
} |
745 |
|
|
746 |
|
// Exception Tests |
1250 |
|
* execute(null) throws NPE |
1251 |
|
*/ |
1252 |
|
public void testExecuteNull() { |
1253 |
< |
ThreadPoolExecutor p = null; |
1253 |
> |
ThreadPoolExecutor p = |
1254 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1255 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1256 |
|
try { |
1228 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1257 |
|
p.execute(null); |
1258 |
|
shouldThrow(); |
1259 |
|
} catch (NullPointerException success) {} |
1749 |
|
l.add(new StringTask()); |
1750 |
|
l.add(new StringTask()); |
1751 |
|
List<Future<String>> futures = |
1752 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1752 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1753 |
|
assertEquals(2, futures.size()); |
1754 |
|
for (Future<String> future : futures) |
1755 |
|
assertSame(TEST_STRING, future.get()); |
1764 |
|
public void testTimedInvokeAll6() throws Exception { |
1765 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1766 |
|
try { |
1767 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1768 |
< |
l.add(new StringTask()); |
1769 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1770 |
< |
l.add(new StringTask()); |
1771 |
< |
List<Future<String>> futures = |
1772 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1773 |
< |
assertEquals(l.size(), futures.size()); |
1774 |
< |
for (Future future : futures) |
1775 |
< |
assertTrue(future.isDone()); |
1776 |
< |
assertFalse(futures.get(0).isCancelled()); |
1777 |
< |
assertTrue(futures.get(1).isCancelled()); |
1767 |
> |
for (long timeout = timeoutMillis();;) { |
1768 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1769 |
> |
tasks.add(new StringTask("0")); |
1770 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1771 |
> |
tasks.add(new StringTask("2")); |
1772 |
> |
long startTime = System.nanoTime(); |
1773 |
> |
List<Future<String>> futures = |
1774 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1775 |
> |
assertEquals(tasks.size(), futures.size()); |
1776 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1777 |
> |
for (Future future : futures) |
1778 |
> |
assertTrue(future.isDone()); |
1779 |
> |
assertTrue(futures.get(1).isCancelled()); |
1780 |
> |
try { |
1781 |
> |
assertEquals("0", futures.get(0).get()); |
1782 |
> |
assertEquals("2", futures.get(2).get()); |
1783 |
> |
break; |
1784 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1785 |
> |
timeout *= 2; |
1786 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1787 |
> |
fail("expected exactly one task to be cancelled"); |
1788 |
> |
} |
1789 |
> |
} |
1790 |
|
} finally { |
1791 |
|
joinPool(e); |
1792 |
|
} |
1829 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1830 |
|
*/ |
1831 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1832 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1832 |
> |
long keepAliveTime = timeoutMillis(); |
1833 |
|
final ThreadPoolExecutor p = |
1834 |
|
new CustomTPE(2, 10, |
1835 |
< |
coreThreadTimeOut, MILLISECONDS, |
1835 |
> |
keepAliveTime, MILLISECONDS, |
1836 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1837 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1838 |
|
try { |
1839 |
|
p.allowCoreThreadTimeOut(true); |
1840 |
|
p.execute(new CheckedRunnable() { |
1841 |
< |
public void realRun() throws InterruptedException { |
1841 |
> |
public void realRun() { |
1842 |
|
threadStarted.countDown(); |
1843 |
|
assertEquals(1, p.getPoolSize()); |
1844 |
|
}}); |
1845 |
|
await(threadStarted); |
1846 |
< |
delay(coreThreadTimeOut); |
1846 |
> |
delay(keepAliveTime); |
1847 |
|
long startTime = System.nanoTime(); |
1848 |
|
while (p.getPoolSize() > 0 |
1849 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1859 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1860 |
|
*/ |
1861 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1862 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1862 |
> |
long keepAliveTime = timeoutMillis(); |
1863 |
|
final ThreadPoolExecutor p = |
1864 |
|
new CustomTPE(2, 10, |
1865 |
< |
coreThreadTimeOut, MILLISECONDS, |
1865 |
> |
keepAliveTime, MILLISECONDS, |
1866 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1867 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1868 |
|
try { |
1872 |
|
threadStarted.countDown(); |
1873 |
|
assertTrue(p.getPoolSize() >= 1); |
1874 |
|
}}); |
1875 |
< |
delay(2 * coreThreadTimeOut); |
1875 |
> |
delay(2 * keepAliveTime); |
1876 |
|
assertTrue(p.getPoolSize() >= 1); |
1877 |
|
} finally { |
1878 |
|
joinPool(p); |
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 |
|
} |