Shutdown thread pool in case of unexpected error.

This commit is contained in:
Gary Gregory 2020-01-01 08:54:48 -05:00
parent b475986174
commit 6f358e9e8d
2 changed files with 45 additions and 39 deletions

View File

@ -81,7 +81,7 @@ public void testCopyOnWriteArrayList() throws InterruptedException, ExecutionExc
}
private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException,
ExecutionException {
ExecutionException {
final List<Integer> list = holder.collection;
// make a big array that takes a long time to toString()
for (int i = 0; i < DATA_SIZE; i++) {
@ -89,29 +89,32 @@ private void testConcurrency(final CollectionHolder<List<Integer>> holder) throw
}
// Create a thread pool with two threads to cause the most contention on the underlying resource.
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
// Consumes toStrings
final Callable<Integer> consumer = () -> {
for (int i = 0; i < REPEAT; i++) {
final String s = ReflectionToStringBuilder.toString(holder);
assertNotNull(s);
try {
// Consumes toStrings
final Callable<Integer> consumer = () -> {
for (int i = 0; i < REPEAT; i++) {
final String s = ReflectionToStringBuilder.toString(holder);
assertNotNull(s);
}
return Integer.valueOf(REPEAT);
};
// Produces changes in the list
final Callable<Integer> producer = () -> {
for (int i = 0; i < DATA_SIZE; i++) {
list.remove(list.get(0));
}
return Integer.valueOf(REPEAT);
};
final Collection<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(consumer);
tasks.add(producer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) {
assertEquals(REPEAT, future.get().intValue());
}
return Integer.valueOf(REPEAT);
};
// Produces changes in the list
final Callable<Integer> producer = () -> {
for (int i = 0; i < DATA_SIZE; i++) {
list.remove(list.get(0));
}
return Integer.valueOf(REPEAT);
};
final Collection<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(consumer);
tasks.add(producer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) {
assertEquals(REPEAT, future.get().intValue());
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
}

View File

@ -82,28 +82,31 @@ public void testCopyOnWriteArrayList() throws InterruptedException, ExecutionExc
}
private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException,
ExecutionException {
ExecutionException {
final List<Integer> list = holder.collection;
// make a big array that takes a long time to toString()
list.addAll(LIST);
// Create a thread pool with two threads to cause the most contention on the underlying resource.
final ExecutorService threadPool = Executors.newFixedThreadPool(2);
// Consumes toStrings
final Callable<Integer> consumer = () -> {
for (int i = 0; i < REPEAT; i++) {
// Calls ToStringStyle
new ToStringBuilder(holder).append(holder.collection);
try {
// Consumes toStrings
final Callable<Integer> consumer = () -> {
for (int i = 0; i < REPEAT; i++) {
// Calls ToStringStyle
new ToStringBuilder(holder).append(holder.collection);
}
return Integer.valueOf(REPEAT);
};
final Collection<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(consumer);
tasks.add(consumer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) {
future.get();
}
return Integer.valueOf(REPEAT);
};
final Collection<Callable<Integer>> tasks = new ArrayList<>();
tasks.add(consumer);
tasks.add(consumer);
final List<Future<Integer>> futures = threadPool.invokeAll(tasks);
for (final Future<Integer> future : futures) {
future.get();
} finally {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
}