Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Loom is just HyperThreading in Java

  • November 14, 2023
  • 3693 Unique Views
  • 2 min read

While sitting in Cay Horstmann's "Looming Changes in Java Concurrency" talk at BaselOne, I had an epiphany: Aren't virtual threads with Loom just a version of HyperThreading on the JVM?

Both try to utilize a computation resource fully, be it hardware core or platform thread, by multiplexing multiple tasks onto it, despite many tasks waiting regularly for IO operations to complete:

When one task waits, another can be scheduled, improving overall throughput. This works especially well when longer IO operations follow short bursts of computation.

There are, of course, differences between the two, most notably: HyperThreading doesn't need the tasks to cooperate, as Loom does, so a virtual core can't starve other virtual cores. Also noteworthy is that the scheduler for Hyper-Threading is implemented in silicon and cannot be configured or even changed, while the virtual thread execution can be targeted to one's needs.

I hope you found this small insight helpful in understanding virtual threads and putting them into context. You can find more about these topics in resources like JEP 444 (Virtual Threads) and the "Hyper-Threading Technology Architecture and Microarchitecture" paper.

This article is part of my work in the SapMachine team at SAP, making profiling and debugging easier for everyone. This article appeared originally on my personal blog mostlynerdless.de.

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (4)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Andrew Schetinin avatar

Andrew Schetinin

1 year ago

I have a feeling that these two things complement each other and not contradict. IMO they exist on different architectural levels. Virtual threads in Java (together with structured concurrency - JEP 437) greatly simplify writing non-blocking multithreaded code. Technically, at the bottom, hyperthreaded hardware runs it all the same - it does not care what it executes. With Java virtual threads and structured concurrency developing scalable applications becomes much easier.

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Johannes Bechberger avatar

Johannes Bechberger

1 year ago

I agree, structured concurrency will make Project Loom far more useful. Currently it is just useful for a small portion of all use cases of multi-threading.

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Alex avatar

Alex

1 year ago

i'm honestly confuse, while i know it's really great improvement java will have virtual threads. But isn't kotlin already achieve virtual threads, just by using normal thread on java?

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Johannes Bechberger avatar

Johannes Bechberger

1 year ago

Kotlin's virtual threads are implemented differently and offer different pro's and con's. There are many great resources out there covering the differences, for example https://www.baeldung.com/kotlin/java-kotlin-lightweight-concurrency.

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard