7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import java.util.concurrent.atomic.*; |
11 |
|
import junit.framework.*; |
12 |
|
import java.util.*; |
13 |
|
|
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 |
|
} |