Skip to content

Explicitly state that maxThreads does not control created thread count#481

Open
ash211 wants to merge 1 commit intodevelopfrom
aash/maxThreads
Open

Explicitly state that maxThreads does not control created thread count#481
ash211 wants to merge 1 commit intodevelopfrom
aash/maxThreads

Conversation

@ash211
Copy link

@ash211 ash211 commented Sep 16, 2025

Before this PR

There was some confusion in thread about what the maxThreads parameter does.

After this PR

==COMMIT_MSG==
Explicitly state that maxThreads does not control created thread count
==COMMIT_MSG==

This PR adds further Javadoc about the maxThread parameter, and adds a test demonstrating that behavior.

Possible downsides?

  • the test uses real threads with a wall clock timeout for completion, so could have the occasional flake

@ash211 ash211 requested a review from schlosna September 16, 2025 08:08
@changelog-app
Copy link

changelog-app bot commented Sep 16, 2025

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Explicitly state that maxThreads does not control created thread count

Check the box to generate changelog(s)

  • Generate changelog entry

@changelog-app
Copy link

changelog-app bot commented Sep 16, 2025

Successfully generated changelog entry!

What happened?

Your changelog entries have been stored in the database as part of our migration to ChangelogV3.

Need to regenerate?

Simply interact with the changelog bot comment again to regenerate these entries.

* The maximum number of threads that were running at once. A thread is counted as running if it has a start
* instant but not yet an end instant.
*/
private static ThreadPeak peakThreads(List<Instant> starts, List<Instant> ends) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wall-clock time here is brittle. A better implementation would be to use a counter that keeps track of the largest value it has seen.

.succeedsWithin(Duration.ofSeconds(10))
.asInstanceOf(InstanceOfAssertFactories.list(String.class))
.hasSize(tasks)
.allSatisfy(value -> assertThat(value).startsWith("foo-"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for a thread return value, this assertion isn't useful.

List<ListenableFuture<String>> futures = new ArrayList<>(tasks);
List<Instant> threadStarts = Collections.synchronizedList(new ArrayList<>(maxThreads));
List<Instant> threadEnds = Collections.synchronizedList(new ArrayList<>(maxThreads));
ListeningExecutorService executor = MoreExecutors.listeningDecorator(NylonExecutor.builder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for a listening decorator here

Comment on lines +276 to +279
assertThat(Futures.successfulAsList(futures))
.succeedsWithin(Duration.ofSeconds(10))
.asInstanceOf(InstanceOfAssertFactories.list(String.class))
.hasSize(tasks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just assert each of these in a loop. 10 second is also far too long.

futures.forEach(future -> {
    future.succeedsWithin(1, TimeUnit.SECOND);
});

Comment on lines +269 to +270
countDownLatch.countDown();
countDownLatch.await();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CyclicBarrier feels more appropriate here

Suggested change
countDownLatch.countDown();
countDownLatch.await();
barrier.await();

Comment on lines +287 to +291
assertThat(delegate)
.asInstanceOf(InstanceOfAssertFactories.type(ThreadPoolExecutor.class))
.satisfies(threadPoolExecutor -> {
assertThat(threadPoolExecutor.getCompletedTaskCount()).isEqualTo(tasks);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant - we've asserted that all the futures have succeeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants