[External] : Re: Experience of adding JDK21 virtual thread support to a DB application

Ron Pressler ron.pressler at oracle.com
Sat Jul 6 20:36:37 UTC 2024



> On 6 Jul 2024, at 13:24, Matthew Swift <matthew.swift at gmail.com> wrote:
> 
> Thanks for the great tip Ron.
> 
> I had a look at how spinning is performed for Object monitors (https://github.com/openjdk/jdk/blob/master/src/hotspot/share/runtime/objectMonitor.cpp#L1790) in order to get an idea of how much spinning should be performed before blocking. Judging by the adaptive heuristics, it's obviously quite a complex topic, especially in the general case. Despite that, I wondered if a fixed number of spin iterations would suffice in my specific use-case given that it is a single hot critical section with a well defined fixed length. Sure enough a few experiments revealed that spinning for 200-500 iterations seemed to get the best results for new and old multi-core hardware, so I now have performance back on a par with Object monitors.

Excellent.

> 
> I will be able to revert back to using Object monitors once the recent monitor support has been officially released, because the Object monitor spin heuristics are more likely to work well across a wider range of hardware both now and in the future.
> 

Unfortunately, while monitors would no longer pin virtual threads, their performance would suffer significantly when using virtual threads, and in general we recommend using j.u.c locks in new code anyway, because that implementation is likely to see further improvement, while monitors probably won’t.

I don’t think there’s a good general way to determine how long to spin, and even the strategy for monitor is a heuristic based on whatever benchmarks were used at the time when it was designed. A spinning strategy for virtual threads would probably be different, and even then, it would differ from one workload to another.

If you are very sensitive to spinning, my recommedation would be to do what you’re doing now — explicit spinning with j.u.c locks — pick a number that’s right for your workload, and revisit it every few years.

— Ron


More information about the loom-dev mailing list