Skip to content

CAMEL-20199: Replace synchronized with ReentrantLock for virtual thread compatibility#21703

Open
gnodet wants to merge 2 commits intomainfrom
virtual-thread-pinning-fixes
Open

CAMEL-20199: Replace synchronized with ReentrantLock for virtual thread compatibility#21703
gnodet wants to merge 2 commits intomainfrom
virtual-thread-pinning-fixes

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Mar 4, 2026

Summary

  • Replace synchronized blocks/methods with ReentrantLock or better concurrent data structures across core modules and components to prevent virtual thread pinning (JDK 21+)
  • Virtual threads get pinned to carrier threads when entering synchronized blocks, degrading throughput. ReentrantLock does not cause pinning.
  • Where possible, use lock-free data structures instead of locks: ConcurrentLinkedQueue, CopyOnWriteArrayList, volatile with double-checked locking

Core changes

File Change
Suppliers.java synchronizedReentrantLock for memoization
DefaultModel.java 9 synchronized methods → single ReentrantLock (reentrant for nested calls)
DefaultCamelContext.java synchronized(model) → uses DefaultModel's lock

Component changes

Component Change
camel-aws2-sqs volatile + ReentrantLock double-checked locking, removed redundant queueUrlInitialized flag
camel-mock synchronizedReentrantLock
camel-google-firestore synchronized + LinkedListConcurrentLinkedQueue (lock-free)
camel-google-pubsub synchronizedListCopyOnWriteArrayList (lock-free)
camel-milo 12 synchronized constructs → ReentrantLock across SubscriptionManager, MiloClientConnection, CamelNamespace

Intentionally skipped

  • CxfConsumer: synchronizes on per-request continuation object — brief pinning, CXF API constraint
  • KameletComponent, CamelWebSocketHandler: already use ReentrantLock
  • XML converters: per-document synchronization, brief lock scope
  • ThreadLocal usages: same-thread semantics, work correctly with virtual threads

Test plan

  • Existing unit tests pass for all modified modules
  • CI build passes (formatting, sourcecheck)
  • Verify no regressions in core routing tests

🤖 Generated with Claude Code

…al thread compatibility

Virtual threads (JDK 21+) can be pinned to carrier threads when they
enter synchronized blocks, degrading throughput. This replaces
synchronized blocks/methods with ReentrantLock or better concurrent
data structures across core and components:

Core:
- Suppliers.java: synchronized → ReentrantLock for memoization
- DefaultModel.java: 9 synchronized methods → ReentrantLock
- DefaultCamelContext.java: synchronized(model) → model lock

Components:
- Sqs2Endpoint: volatile + ReentrantLock double-checked locking,
  removed redundant queueUrlInitialized flag
- MockEndpoint: synchronized → ReentrantLock
- GoogleFirestoreConsumer: synchronized + LinkedList → ConcurrentLinkedQueue
- GooglePubsubConsumer: synchronizedList → CopyOnWriteArrayList
- SubscriptionManager: 12 synchronized constructs → ReentrantLock
- MiloClientConnection: synchronized → ReentrantLock
- CamelNamespace: synchronized → ReentrantLock

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

🌟 Thank you for your contribution to the Apache Camel project! 🌟
🤖 CI automation will test this PR automatically.

🐫 Apache Camel Committers, please review the following items:

  • First-time contributors require MANUAL approval for the GitHub Actions to run
  • You can use the command /component-test (camel-)component-name1 (camel-)component-name2.. to request a test from the test bot although they are normally detected and executed by CI.
  • You can label PRs using build-all, build-dependents, skip-tests and test-dependents to fine-tune the checks executed by this PR.
  • Build and test logs are available in the summary page. Only Apache Camel committers have access to the summary.

⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

…entConsumers description

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants