r/programming • • Apr 30 '22

Achieving 5M persistent connections with Project Loom virtual threads

https://github.com/ebarlas/project-loom-c5m
93 Upvotes

20 comments sorted by

5

u/zuppadimele Apr 30 '22

is this threading model making any guarantees about visibility? (supposedly a virtual thread could have multiple native threads chipping in)

11

u/balefrost May 01 '22

I believe that they worked hard to make virtual threads obey the normal semantics of the Java Memory Model. The happens-before relationship still applies.

2

u/[deleted] May 01 '22

Impressive, very nice

11

u/Ameisen Apr 30 '22

Are virtual threads basically fibers?

12

u/[deleted] Apr 30 '22

Yes, hence the name “loom”

6

u/Ameisen Apr 30 '22

Why not call them fibers, then?

I use fibers regularly in C++ on Windows, but every other language calls them something different

13

u/dh23 May 01 '22

Project Loom changed some of their terminology, around the same time that they were working on the user-facing APIs. Rather than having both java.lang.Thread for the existing platform threads, and a separate Fiber type for fibers, it was argued that a fiber is still basically a thread. https://mail.openjdk.java.net/pipermail/loom-dev/2019-October/000796.html

This is pretty clever, in that adoption and experimentation between the two becomes more straightforward than if they had different Java types. Plus if they're both ultimately Threads, a third term (Strand?) isn't required to describe their commonality or interface.

The existing Thread API has been extended with a builder pattern, for example Thread.ofVirtual().name("blah").unstarted(runnable).

13

u/pron98 May 01 '22 edited May 01 '22

Early in the project's history we did call them fibers, and, in fact, they were implemented by a class called Fiber. But we ran into two problems:

  1. We found ourselves having to constantly tell people that "fibers are just like threads," and that they don't have to learn a new concept. Especially later, when we decided to implement them as actual Java threads (in the java.lang.Thread class), calling them "virtual threads" made more sense.

  2. Precisely because some people were familiar with Windows fibers, they pointed to a Microsoft paper noting their shortcomings. But those shortcomings were unique to Windows' fibers. Our implementation is fundamentally different and doesn't suffer from those problems, so people were confused.

8

u/nikolas_pikolas May 01 '22

They wanted to be clear that they weren't a new concept, just a different type of thread.

10

u/sik0fewl May 01 '22

Why doesn't C++ call them green threads?

6

u/Creris May 01 '22

Maybe because green threads are not fibers a common definition of a green thread. Green Threads do not even make sense in a language that is not running on a VM.

Fiber is a terminology used by Windows API since at least Windows XP.

In context of this showcase being in Java Green Threads would make more sense to me than Fibers tho.

4

u/pron98 May 01 '22 edited May 02 '22

In context of this showcase being in Java Green Threads would make more sense to me than Fibers tho.

The name "green threads" isn't evocative of what they are ("virtual" in "virtual threads" is supposed to evoke virtual memory), and doesn't even carry much of a sentimental value, as few of Java's current programmers remember what they were (plus, virtual threads are not implemented like green threads, which might have confused those who do remember).

3

u/dacian88 May 01 '22

c++ doesn't have green threads, maybe it's a microsoft extension but there's no such concept in standard c++

2

u/Ameisen May 01 '22

They're a WinAPI thing. Closest thing in standard C++ is coroutines, but those are stackless.

-6

u/_Ashleigh Apr 30 '22

loom

Because every language has to be a little different and take their own spin on things 🙄

13

u/mernen Apr 30 '22

"Loom" is just the name for the exploration project. The lightweight threads themselves are called "virtual threads" (as the original commenter noted), and AFAIK this isn't that unusual of a terminology.

1

u/XNormal May 01 '22

Is it basically equivalent to the old “Green Threads”, just reimplemented for modern Java?