Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips September 2023
JProfiler
|
Get rid of your performance problems and memory leaks!
|
JProfiler
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 274 contents
https://www.youtube.com/watch?v=WsCJYQDPrrE
The Challenges of Introducing Virtual Threads to the Java Platform - Project Loom (Page last updated August 2023, Added 2023-09-28, Author Alan Bateman, Publisher JVM Language Summit). Tips:
- When using virtual threads: don't pool them; don't just replace all your platform threads with virtual threads, they are aimed at being used for a task; there are pinning issues with monitors, but don't change all your synchronized blocks with locks, this only needs to happen in very targeted cases.
- When using virtual threads: use simpler more maintainable synchronous blocking code style code; migrate tasks to virtual threads, not platform threads to virtual threads; use Semaphores to limit concurrency; don't cache in thread locals; monitor for pinned threads.
- Avoid caching objects in thread locals when using virtual threads.
- File IO in JDK 21 is still blocking; the carrier thread pool for virtual threads is temporarily increased in size to transparently handle this, but this is not optimal and should be fixed in future JDKs.
https://blog.rockthejvm.com/ultimate-guide-to-java-virtual-threads/
The Ultimate Guide to Java Virtual Threads (Page last updated February 2023, Added 2023-09-28, Author Daniel Ciocirlan, Publisher Rock the JVM). Tips:
- Virtual threads are much more lightweight (in memory cost) than old style platform threads.
- The straightforward way to write concurrent programs is to create a new virtual thread for every concurrent task. This model is called one task per thread and is what virtual threads excel at.
- Virtual threads by default don't have a useful name, so for observability it's better to use the instantiators that let you set a name.
- Virtual threads run on a pool of platform threads. The pool size defaults to the number of CPU cores, but can increase up and down to handle platform threads that get blocked (pinned) by some inefficiencies in the current Virtual thread implementation.
- Typically when the virtual thread blocks, it is unmounted from from the platform carrier thread it was running on, leaving the platform carrier thread to run run another virtual thread.
- Virtual threads currently (JDK21) implement cooperative scheduling. This isn't a limitation of virtual threads, it's a choice of the current implementation. Virtual threads can implement fully pre-emptive scheduling (and may in future).
- You can trace when virtual threads are pinned by setting the property -Djdk.tracePinnedThreads=full/short - full prints the full trace.
- If you identify pinned threads from synchronization in your application, and those pinned threads are impacting the efficiency of the application (maybe requiring too many additional platform carrier threads), you can migrate the synchronized blocks to use a ReentrantLock. However this is only needed if there is an actual issue, as the code change is effortful.
- Don't pool virtual threads, that defeats the aim and would be inefficient.
- Because there are expected to be many more virtual threads than platform threads, and the virtual threads aren't expected to share tasks, ThreadLocals are not as useful in virtual threads and can cause high memory use if used in the patterns previously used for platform threads. Certainly don't cache objects in virtual thread thread locals for sharing across threads, that is an anti-pattern.
https://www.youtube.com/watch?v=UjzLaVI87mA
Introduction and pitfalls of Java's new concurrency model (Page last updated May 2023, Added 2023-09-28, Author David Vlijmincx, Publisher Devoxx). Tips:
- Some operations (eg synchronized blocks, file IO) in JDK21 block the carrier/platform thread running a virtual thread. Too many of these happening concurrently can lead to inefficient overall execution.
- Pitfalls of using virtual threads (ie avoid these with virtual threads): long running CPU bound operations; pooling them; using a threadpool as a lock; old libraries.
- Structured concurrency is designed to handle interrupted (for whatever reason) parallel operations that are inter-dependent. This optimizes efficiency by terminating all threads as soon as possible on any interruption of any thread.
- Structured concurrency let's you finely control the lifetime of virtual threads.
Jack Shirazi
Back to newsletter 274 contents
Last Updated: 2025-01-27
Copyright © 2000-2025 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/newtips274.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us