Skip to content

Use ThreadLocalRandom for thread selection in OrderedExecutor#4832

Open
merlimat wants to merge 1 commit into
apache:masterfrom
merlimat:ordered-executor-thread-local-random
Open

Use ThreadLocalRandom for thread selection in OrderedExecutor#4832
merlimat wants to merge 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.

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