Skip to content

Use ThreadLocalRandom for thread selection in OrderedExecutor#4832

Merged
merlimat merged 1 commit into
apache:masterfrom
merlimat:ordered-executor-thread-local-random
Jul 14, 2026
Merged

Use ThreadLocalRandom for thread selection in OrderedExecutor#4832
merlimat merged 1 commit into
apache:masterfrom
merlimat:ordered-executor-thread-local-random

Conversation

@merlimat

Copy link
Copy Markdown
Contributor

Motivation

OrderedExecutor.chooseThread() — the path behind the keyless submit()/execute() overloads and chooseThread(null) — picks a thread using a single shared java.util.Random instance. Random.nextInt() is a CAS retry loop over one shared AtomicLong seed, so when many threads submit keyless tasks to a shared executor they all contend on the same cache line, purely to produce a thread index.

Changes

Use ThreadLocalRandom.current().nextInt(...) for the random routing (no shared state, no CAS contention) and remove the now-unused rand field.

Verified with TestOrderedExecutor and TestOrderedExecutorDecorators (all pass) and module checkstyle.

@merlimat
merlimat merged commit 970f56e into apache:master Jul 14, 2026
20 checks passed
merlimat added a commit that referenced this pull request Jul 14, 2026
@merlimat merlimat added this to the 4.18.0 milestone Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants