Java Reference
In-Depth Information
ants. For example,
ThreadPoolExecutor
makes calls to the task
Runnable
s and to the
ThreadFactory
.
Listing 12.7. Testing for Resource Leaks.
Testing a thread pool involves testing a number of elements of execution policy: that addi-
tional threads are created when they are supposed to, but not when they are not supposed to;
that idle threads get reaped when they are supposed to, etc. Constructing a comprehensive
test suite that covers all the possibilities is a major effort, but many of them can be tested
fairly simply individually.
We can instrument thread creation by using a custom thread factory.
Test-
ingThreadFactory
in
Listing 12.8
maintains a count of created threads; test cases can
then verify the number of threads created during a test run.
TestingThreadFactory
could be extended to return a custom
Thread
that also records when the thread terminates,
so that test cases can verify that threads are reaped in accordance with the execution policy.