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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
14 |
|
|
15 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
31 |
|
Exception exception; |
32 |
|
CustomTask(Callable<V> c) { callable = c; } |
33 |
|
CustomTask(final Runnable r, final V res) { callable = new Callable<V>() { |
34 |
< |
public V call() throws Exception { r.run(); return res; }}; |
34 |
> |
public V call() throws Exception { r.run(); return res; }}; |
35 |
|
} |
36 |
|
public boolean isDone() { |
37 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
45 |
|
if (!done) { |
46 |
|
cancelled = true; |
47 |
|
done = true; |
48 |
< |
if (mayInterrupt && thread != null) |
48 |
> |
if (mayInterrupt && thread != null) |
49 |
|
thread.interrupt(); |
50 |
|
return true; |
51 |
|
} |
84 |
|
public V get() throws InterruptedException, ExecutionException { |
85 |
|
lock.lock(); |
86 |
|
try { |
87 |
< |
while (!done) |
87 |
> |
while (!done) |
88 |
|
cond.await(); |
89 |
|
if (exception != null) |
90 |
|
throw new ExecutionException(exception); |
109 |
|
} |
110 |
|
finally { lock.unlock(); } |
111 |
|
} |
112 |
< |
} |
112 |
> |
} |
113 |
> |
|
114 |
|
|
114 |
– |
|
115 |
|
static class CustomTPE extends ThreadPoolExecutor { |
116 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
117 |
|
return new CustomTask<V>(c); |
118 |
|
} |
119 |
|
protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { |
120 |
|
return new CustomTask<V>(r, v); |
121 |
< |
} |
122 |
< |
|
121 |
> |
} |
122 |
> |
|
123 |
|
CustomTPE(int corePoolSize, |
124 |
|
int maximumPoolSize, |
125 |
|
long keepAliveTime, |
126 |
|
TimeUnit unit, |
127 |
|
BlockingQueue<Runnable> workQueue) { |
128 |
< |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
128 |
> |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
129 |
|
workQueue); |
130 |
|
} |
131 |
|
CustomTPE(int corePoolSize, |
154 |
|
BlockingQueue<Runnable> workQueue, |
155 |
|
ThreadFactory threadFactory, |
156 |
|
RejectedExecutionHandler handler) { |
157 |
< |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
157 |
> |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
158 |
|
workQueue, threadFactory, handler); |
159 |
|
} |
160 |
|
|
173 |
|
protected void terminated() { |
174 |
|
terminatedCalled = true; |
175 |
|
} |
176 |
< |
|
176 |
> |
|
177 |
|
} |
178 |
|
|
179 |
|
static class FailingThreadFactory implements ThreadFactory{ |
181 |
|
public Thread newThread(Runnable r){ |
182 |
|
if (++calls > 1) return null; |
183 |
|
return new Thread(r); |
184 |
< |
} |
184 |
> |
} |
185 |
|
} |
186 |
< |
|
186 |
> |
|
187 |
|
|
188 |
|
/** |
189 |
|
* execute successfully executes a runnable |
203 |
|
Thread.sleep(SMALL_DELAY_MS); |
204 |
|
} catch(InterruptedException e){ |
205 |
|
unexpectedException(); |
206 |
< |
} |
206 |
> |
} |
207 |
|
joinPool(p1); |
208 |
|
} |
209 |
|
|
251 |
|
assertEquals(2, p2.getPoolSize()); |
252 |
|
joinPool(p2); |
253 |
|
} |
254 |
< |
|
254 |
> |
|
255 |
|
/** |
256 |
|
* getCompletedTaskCount increases, but doesn't overestimate, |
257 |
|
* when tasks complete |
269 |
|
try { p2.shutdown(); } catch(SecurityException ok) { return; } |
270 |
|
joinPool(p2); |
271 |
|
} |
272 |
< |
|
272 |
> |
|
273 |
|
/** |
274 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
275 |
|
*/ |
278 |
|
assertEquals(1, p1.getCorePoolSize()); |
279 |
|
joinPool(p1); |
280 |
|
} |
281 |
< |
|
281 |
> |
|
282 |
|
/** |
283 |
|
* getKeepAliveTime returns value given in constructor if not otherwise set |
284 |
|
*/ |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
< |
/** |
292 |
> |
/** |
293 |
|
* getThreadFactory returns factory in constructor if not set |
294 |
|
*/ |
295 |
|
public void testGetThreadFactory() { |
299 |
|
joinPool(p); |
300 |
|
} |
301 |
|
|
302 |
< |
/** |
302 |
> |
/** |
303 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
304 |
|
*/ |
305 |
|
public void testSetThreadFactory() { |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
< |
/** |
314 |
> |
/** |
315 |
|
* setThreadFactory(null) throws NPE |
316 |
|
*/ |
317 |
|
public void testSetThreadFactoryNull() { |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
< |
/** |
328 |
> |
/** |
329 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
330 |
|
*/ |
331 |
|
public void testGetRejectedExecutionHandler() { |
335 |
|
joinPool(p); |
336 |
|
} |
337 |
|
|
338 |
< |
/** |
338 |
> |
/** |
339 |
|
* setRejectedExecutionHandler sets the handler returned by |
340 |
|
* getRejectedExecutionHandler |
341 |
|
*/ |
348 |
|
} |
349 |
|
|
350 |
|
|
351 |
< |
/** |
351 |
> |
/** |
352 |
|
* setRejectedExecutionHandler(null) throws NPE |
353 |
|
*/ |
354 |
|
public void testSetRejectedExecutionHandlerNull() { |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
< |
|
365 |
> |
|
366 |
|
/** |
367 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
368 |
|
* multiple threads active |
377 |
|
assertEquals(2, p2.getLargestPoolSize()); |
378 |
|
} catch(Exception e){ |
379 |
|
unexpectedException(); |
380 |
< |
} |
380 |
> |
} |
381 |
|
joinPool(p2); |
382 |
|
} |
383 |
< |
|
383 |
> |
|
384 |
|
/** |
385 |
|
* getMaximumPoolSize returns value given in constructor if not |
386 |
|
* otherwise set |
390 |
|
assertEquals(2, p2.getMaximumPoolSize()); |
391 |
|
joinPool(p2); |
392 |
|
} |
393 |
< |
|
393 |
> |
|
394 |
|
/** |
395 |
|
* getPoolSize increases, but doesn't overestimate, when threads |
396 |
|
* become active |
402 |
|
assertEquals(1, p1.getPoolSize()); |
403 |
|
joinPool(p1); |
404 |
|
} |
405 |
< |
|
405 |
> |
|
406 |
|
/** |
407 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
408 |
|
*/ |
415 |
|
assertEquals(1, p1.getTaskCount()); |
416 |
|
} catch(Exception e){ |
417 |
|
unexpectedException(); |
418 |
< |
} |
418 |
> |
} |
419 |
|
joinPool(p1); |
420 |
|
} |
421 |
< |
|
421 |
> |
|
422 |
|
/** |
423 |
|
* isShutDown is false before shutdown, true after |
424 |
|
*/ |
425 |
|
public void testIsShutdown() { |
426 |
< |
|
426 |
> |
|
427 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
428 |
|
assertFalse(p1.isShutdown()); |
429 |
|
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
431 |
|
joinPool(p1); |
432 |
|
} |
433 |
|
|
434 |
< |
|
434 |
> |
|
435 |
|
/** |
436 |
|
* isTerminated is false before termination, true after |
437 |
|
*/ |
448 |
|
assertTrue(p1.isTerminated()); |
449 |
|
} catch(Exception e){ |
450 |
|
unexpectedException(); |
451 |
< |
} |
451 |
> |
} |
452 |
|
} |
453 |
|
|
454 |
|
/** |
469 |
|
assertFalse(p1.isTerminating()); |
470 |
|
} catch(Exception e){ |
471 |
|
unexpectedException(); |
472 |
< |
} |
472 |
> |
} |
473 |
|
} |
474 |
|
|
475 |
|
/** |
560 |
|
try { |
561 |
|
l = p1.shutdownNow(); |
562 |
|
} catch (SecurityException ok) { return; } |
563 |
< |
|
563 |
> |
|
564 |
|
} |
565 |
|
assertTrue(p1.isShutdown()); |
566 |
|
assertTrue(l.size() <= 4); |
567 |
|
} |
568 |
|
|
569 |
|
// Exception Tests |
570 |
– |
|
570 |
|
|
571 |
< |
/** |
572 |
< |
* Constructor throws if corePoolSize argument is less than zero |
571 |
> |
|
572 |
> |
/** |
573 |
> |
* Constructor throws if corePoolSize argument is less than zero |
574 |
|
*/ |
575 |
|
public void testConstructor1() { |
576 |
|
try { |
579 |
|
} |
580 |
|
catch (IllegalArgumentException success){} |
581 |
|
} |
582 |
< |
|
583 |
< |
/** |
584 |
< |
* Constructor throws if maximumPoolSize is less than zero |
582 |
> |
|
583 |
> |
/** |
584 |
> |
* Constructor throws if maximumPoolSize is less than zero |
585 |
|
*/ |
586 |
|
public void testConstructor2() { |
587 |
|
try { |
590 |
|
} |
591 |
|
catch (IllegalArgumentException success){} |
592 |
|
} |
593 |
< |
|
594 |
< |
/** |
595 |
< |
* Constructor throws if maximumPoolSize is equal to zero |
593 |
> |
|
594 |
> |
/** |
595 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
596 |
|
*/ |
597 |
|
public void testConstructor3() { |
598 |
|
try { |
602 |
|
catch (IllegalArgumentException success){} |
603 |
|
} |
604 |
|
|
605 |
< |
/** |
606 |
< |
* Constructor throws if keepAliveTime is less than zero |
605 |
> |
/** |
606 |
> |
* Constructor throws if keepAliveTime is less than zero |
607 |
|
*/ |
608 |
|
public void testConstructor4() { |
609 |
|
try { |
613 |
|
catch (IllegalArgumentException success){} |
614 |
|
} |
615 |
|
|
616 |
< |
/** |
617 |
< |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
616 |
> |
/** |
617 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
618 |
|
*/ |
619 |
|
public void testConstructor5() { |
620 |
|
try { |
623 |
|
} |
624 |
|
catch (IllegalArgumentException success){} |
625 |
|
} |
626 |
< |
|
627 |
< |
/** |
628 |
< |
* Constructor throws if workQueue is set to null |
626 |
> |
|
627 |
> |
/** |
628 |
> |
* Constructor throws if workQueue is set to null |
629 |
|
*/ |
630 |
|
public void testConstructorNullPointerException() { |
631 |
|
try { |
632 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
633 |
|
shouldThrow(); |
634 |
|
} |
635 |
< |
catch (NullPointerException success){} |
635 |
> |
catch (NullPointerException success){} |
636 |
|
} |
637 |
– |
|
637 |
|
|
638 |
< |
|
639 |
< |
/** |
640 |
< |
* Constructor throws if corePoolSize argument is less than zero |
638 |
> |
|
639 |
> |
|
640 |
> |
/** |
641 |
> |
* Constructor throws if corePoolSize argument is less than zero |
642 |
|
*/ |
643 |
|
public void testConstructor6() { |
644 |
|
try { |
646 |
|
shouldThrow(); |
647 |
|
} catch (IllegalArgumentException success){} |
648 |
|
} |
649 |
< |
|
650 |
< |
/** |
651 |
< |
* Constructor throws if maximumPoolSize is less than zero |
649 |
> |
|
650 |
> |
/** |
651 |
> |
* Constructor throws if maximumPoolSize is less than zero |
652 |
|
*/ |
653 |
|
public void testConstructor7() { |
654 |
|
try { |
658 |
|
catch (IllegalArgumentException success){} |
659 |
|
} |
660 |
|
|
661 |
< |
/** |
662 |
< |
* Constructor throws if maximumPoolSize is equal to zero |
661 |
> |
/** |
662 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
663 |
|
*/ |
664 |
|
public void testConstructor8() { |
665 |
|
try { |
669 |
|
catch (IllegalArgumentException success){} |
670 |
|
} |
671 |
|
|
672 |
< |
/** |
673 |
< |
* Constructor throws if keepAliveTime is less than zero |
672 |
> |
/** |
673 |
> |
* Constructor throws if keepAliveTime is less than zero |
674 |
|
*/ |
675 |
|
public void testConstructor9() { |
676 |
|
try { |
680 |
|
catch (IllegalArgumentException success){} |
681 |
|
} |
682 |
|
|
683 |
< |
/** |
684 |
< |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
683 |
> |
/** |
684 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
685 |
|
*/ |
686 |
|
public void testConstructor10() { |
687 |
|
try { |
691 |
|
catch (IllegalArgumentException success){} |
692 |
|
} |
693 |
|
|
694 |
< |
/** |
695 |
< |
* Constructor throws if workQueue is set to null |
694 |
> |
/** |
695 |
> |
* Constructor throws if workQueue is set to null |
696 |
|
*/ |
697 |
|
public void testConstructorNullPointerException2() { |
698 |
|
try { |
699 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
700 |
|
shouldThrow(); |
701 |
|
} |
702 |
< |
catch (NullPointerException success){} |
702 |
> |
catch (NullPointerException success){} |
703 |
|
} |
704 |
|
|
705 |
< |
/** |
706 |
< |
* Constructor throws if threadFactory is set to null |
705 |
> |
/** |
706 |
> |
* Constructor throws if threadFactory is set to null |
707 |
|
*/ |
708 |
|
public void testConstructorNullPointerException3() { |
709 |
|
try { |
711 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
712 |
|
shouldThrow(); |
713 |
|
} |
714 |
< |
catch (NullPointerException success){} |
714 |
> |
catch (NullPointerException success){} |
715 |
|
} |
716 |
< |
|
717 |
< |
|
718 |
< |
/** |
719 |
< |
* Constructor throws if corePoolSize argument is less than zero |
716 |
> |
|
717 |
> |
|
718 |
> |
/** |
719 |
> |
* Constructor throws if corePoolSize argument is less than zero |
720 |
|
*/ |
721 |
|
public void testConstructor11() { |
722 |
|
try { |
726 |
|
catch (IllegalArgumentException success){} |
727 |
|
} |
728 |
|
|
729 |
< |
/** |
730 |
< |
* Constructor throws if maximumPoolSize is less than zero |
729 |
> |
/** |
730 |
> |
* Constructor throws if maximumPoolSize is less than zero |
731 |
|
*/ |
732 |
|
public void testConstructor12() { |
733 |
|
try { |
737 |
|
catch (IllegalArgumentException success){} |
738 |
|
} |
739 |
|
|
740 |
< |
/** |
741 |
< |
* Constructor throws if maximumPoolSize is equal to zero |
740 |
> |
/** |
741 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
742 |
|
*/ |
743 |
|
public void testConstructor13() { |
744 |
|
try { |
748 |
|
catch (IllegalArgumentException success){} |
749 |
|
} |
750 |
|
|
751 |
< |
/** |
752 |
< |
* Constructor throws if keepAliveTime is less than zero |
751 |
> |
/** |
752 |
> |
* Constructor throws if keepAliveTime is less than zero |
753 |
|
*/ |
754 |
|
public void testConstructor14() { |
755 |
|
try { |
759 |
|
catch (IllegalArgumentException success){} |
760 |
|
} |
761 |
|
|
762 |
< |
/** |
763 |
< |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
762 |
> |
/** |
763 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
764 |
|
*/ |
765 |
|
public void testConstructor15() { |
766 |
|
try { |
770 |
|
catch (IllegalArgumentException success){} |
771 |
|
} |
772 |
|
|
773 |
< |
/** |
774 |
< |
* Constructor throws if workQueue is set to null |
773 |
> |
/** |
774 |
> |
* Constructor throws if workQueue is set to null |
775 |
|
*/ |
776 |
|
public void testConstructorNullPointerException4() { |
777 |
|
try { |
778 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
779 |
|
shouldThrow(); |
780 |
|
} |
781 |
< |
catch (NullPointerException success){} |
781 |
> |
catch (NullPointerException success){} |
782 |
|
} |
783 |
|
|
784 |
< |
/** |
785 |
< |
* Constructor throws if handler is set to null |
784 |
> |
/** |
785 |
> |
* Constructor throws if handler is set to null |
786 |
|
*/ |
787 |
|
public void testConstructorNullPointerException5() { |
788 |
|
try { |
790 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
791 |
|
shouldThrow(); |
792 |
|
} |
793 |
< |
catch (NullPointerException success){} |
793 |
> |
catch (NullPointerException success){} |
794 |
|
} |
795 |
|
|
796 |
< |
|
797 |
< |
/** |
798 |
< |
* Constructor throws if corePoolSize argument is less than zero |
796 |
> |
|
797 |
> |
/** |
798 |
> |
* Constructor throws if corePoolSize argument is less than zero |
799 |
|
*/ |
800 |
|
public void testConstructor16() { |
801 |
|
try { |
805 |
|
catch (IllegalArgumentException success){} |
806 |
|
} |
807 |
|
|
808 |
< |
/** |
809 |
< |
* Constructor throws if maximumPoolSize is less than zero |
808 |
> |
/** |
809 |
> |
* Constructor throws if maximumPoolSize is less than zero |
810 |
|
*/ |
811 |
|
public void testConstructor17() { |
812 |
|
try { |
816 |
|
catch (IllegalArgumentException success){} |
817 |
|
} |
818 |
|
|
819 |
< |
/** |
820 |
< |
* Constructor throws if maximumPoolSize is equal to zero |
819 |
> |
/** |
820 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
821 |
|
*/ |
822 |
|
public void testConstructor18() { |
823 |
|
try { |
827 |
|
catch (IllegalArgumentException success){} |
828 |
|
} |
829 |
|
|
830 |
< |
/** |
831 |
< |
* Constructor throws if keepAliveTime is less than zero |
830 |
> |
/** |
831 |
> |
* Constructor throws if keepAliveTime is less than zero |
832 |
|
*/ |
833 |
|
public void testConstructor19() { |
834 |
|
try { |
838 |
|
catch (IllegalArgumentException success){} |
839 |
|
} |
840 |
|
|
841 |
< |
/** |
842 |
< |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
841 |
> |
/** |
842 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
843 |
|
*/ |
844 |
|
public void testConstructor20() { |
845 |
|
try { |
849 |
|
catch (IllegalArgumentException success){} |
850 |
|
} |
851 |
|
|
852 |
< |
/** |
853 |
< |
* Constructor throws if workQueue is set to null |
852 |
> |
/** |
853 |
> |
* Constructor throws if workQueue is set to null |
854 |
|
*/ |
855 |
|
public void testConstructorNullPointerException6() { |
856 |
|
try { |
857 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
858 |
|
shouldThrow(); |
859 |
|
} |
860 |
< |
catch (NullPointerException success){} |
860 |
> |
catch (NullPointerException success){} |
861 |
|
} |
862 |
|
|
863 |
< |
/** |
864 |
< |
* Constructor throws if handler is set to null |
863 |
> |
/** |
864 |
> |
* Constructor throws if handler is set to null |
865 |
|
*/ |
866 |
|
public void testConstructorNullPointerException7() { |
867 |
|
try { |
869 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
870 |
|
shouldThrow(); |
871 |
|
} |
872 |
< |
catch (NullPointerException success){} |
872 |
> |
catch (NullPointerException success){} |
873 |
|
} |
874 |
|
|
875 |
< |
/** |
876 |
< |
* Constructor throws if ThreadFactory is set top null |
875 |
> |
/** |
876 |
> |
* Constructor throws if ThreadFactory is set top null |
877 |
|
*/ |
878 |
|
public void testConstructorNullPointerException8() { |
879 |
|
try { |
881 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
882 |
|
shouldThrow(); |
883 |
|
} |
884 |
< |
catch (NullPointerException successdn8){} |
884 |
> |
catch (NullPointerException successdn8){} |
885 |
|
} |
886 |
< |
|
886 |
> |
|
887 |
|
|
888 |
|
/** |
889 |
|
* execute throws RejectedExecutionException |
892 |
|
public void testSaturatedExecute() { |
893 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
894 |
|
try { |
895 |
< |
|
895 |
> |
|
896 |
|
for(int i = 0; i < 5; ++i){ |
897 |
|
p.execute(new MediumRunnable()); |
898 |
|
} |
908 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
909 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
910 |
|
try { |
911 |
< |
|
911 |
> |
|
912 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
913 |
|
for(int i = 0; i < 5; ++i){ |
914 |
|
tasks[i] = new TrackedNoOpRunnable(); |
936 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
937 |
|
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
938 |
|
try { |
939 |
< |
|
939 |
> |
|
940 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
941 |
|
for(int i = 0; i < 5; ++i){ |
942 |
|
tasks[i] = new TrackedNoOpRunnable(); |
983 |
|
* execute throws RejectedExecutionException if shutdown |
984 |
|
*/ |
985 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
986 |
< |
ThreadPoolExecutor tpe = |
986 |
> |
ThreadPoolExecutor tpe = |
987 |
|
new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
988 |
|
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
989 |
|
try { |
990 |
|
tpe.execute(new NoOpRunnable()); |
991 |
|
shouldThrow(); |
992 |
|
} catch(RejectedExecutionException success){} |
993 |
< |
|
993 |
> |
|
994 |
|
joinPool(tpe); |
995 |
|
} |
996 |
|
|
1063 |
|
tpe.execute(null); |
1064 |
|
shouldThrow(); |
1065 |
|
} catch(NullPointerException success){} |
1066 |
< |
|
1066 |
> |
|
1067 |
|
joinPool(tpe); |
1068 |
|
} |
1069 |
< |
|
1069 |
> |
|
1070 |
|
/** |
1071 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1072 |
|
*/ |
1083 |
|
try { tpe.shutdown(); } catch(SecurityException ok) { return; } |
1084 |
|
} |
1085 |
|
joinPool(tpe); |
1086 |
< |
} |
1086 |
> |
} |
1087 |
|
|
1088 |
|
/** |
1089 |
|
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1090 |
|
* given a value less the core pool size |
1091 |
< |
*/ |
1091 |
> |
*/ |
1092 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1093 |
|
ThreadPoolExecutor tpe = null; |
1094 |
|
try { |
1103 |
|
} |
1104 |
|
joinPool(tpe); |
1105 |
|
} |
1106 |
< |
|
1106 |
> |
|
1107 |
|
/** |
1108 |
|
* setMaximumPoolSize throws IllegalArgumentException |
1109 |
|
* if given a negative value |
1122 |
|
} |
1123 |
|
joinPool(tpe); |
1124 |
|
} |
1125 |
< |
|
1125 |
> |
|
1126 |
|
|
1127 |
|
/** |
1128 |
|
* setKeepAliveTime throws IllegalArgumentException |
1133 |
|
try { |
1134 |
|
tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1135 |
|
} catch(Exception e){} |
1136 |
< |
|
1136 |
> |
|
1137 |
|
try { |
1138 |
|
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
1139 |
|
shouldThrow(); |
1381 |
|
l.add(new NPETask()); |
1382 |
|
List<Future<String>> result = e.invokeAll(l); |
1383 |
|
assertEquals(1, result.size()); |
1384 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1384 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1385 |
|
it.next().get(); |
1386 |
|
} catch(ExecutionException success) { |
1387 |
|
} catch(Exception ex) { |
1402 |
|
l.add(new StringTask()); |
1403 |
|
List<Future<String>> result = e.invokeAll(l); |
1404 |
|
assertEquals(2, result.size()); |
1405 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1405 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1406 |
|
assertSame(TEST_STRING, it.next().get()); |
1407 |
|
} catch (ExecutionException success) { |
1408 |
|
} catch(Exception ex) { |
1591 |
|
l.add(new NPETask()); |
1592 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1593 |
|
assertEquals(1, result.size()); |
1594 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1594 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1595 |
|
it.next().get(); |
1596 |
|
} catch(ExecutionException success) { |
1597 |
|
} catch(Exception ex) { |
1612 |
|
l.add(new StringTask()); |
1613 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1614 |
|
assertEquals(2, result.size()); |
1615 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1615 |
> |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1616 |
|
assertSame(TEST_STRING, it.next().get()); |
1617 |
|
} catch (ExecutionException success) { |
1618 |
|
} catch(Exception ex) { |
1634 |
|
l.add(new StringTask()); |
1635 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1636 |
|
assertEquals(3, result.size()); |
1637 |
< |
Iterator<Future<String>> it = result.iterator(); |
1637 |
> |
Iterator<Future<String>> it = result.iterator(); |
1638 |
|
Future<String> f1 = it.next(); |
1639 |
|
Future<String> f2 = it.next(); |
1640 |
|
Future<String> f3 = it.next(); |