JOX - Virtual-thread-friendly go-like channels for Java, Adam Warski SoftwareMill
Ғылым және технология
Check on the GitHub: github.com/softwaremill/jox/
The goal of the jox project is to facilitate communication between virtual threads:
- competitive in terms of performance to the state-of-the-art (on the JVM, and generally)
- with a low memory footprint so that the number of channels might scale together with the number of virtual threads used
- offering a feature set inspired by Go channels
More about virtual threads • #UncoverJava: Project ...
Пікірлер: 9
Another question: why is Jox pretty slower than the Kotlin channel when they both use the same design? Is it possible that the jvm bytecode generated by Kotlin is better than the one generate by javac?
@AdamWarski2
13 күн бұрын
Great question! :) There might be several reasons: 1. the runtime is different. In Kotlin, the compiler transforms code which has suspendable functions into a finite state machine. That's known as a continuation-passing-style (CPS) transform. Then, Kotlin's coroutine runtime manages multiplexing of running subsequent steps in the evaluation of these state machine, onto a finite thread pool. In Java 21, when we run on a virtual thread and encounter a blocking operation, there's might be a context switch of the virtual thread that is running; this involves stashing the current stack trace on the side, restoring another one, and resuming this other virtual thread. Even if the effects are similar, both mechanisms are different, and might have different performance characteristics. 2. the differences aren't *that* big (we're talking about 9ns and 14ns), and the benchmark might be simply skewed. Another benchmark might give different results. The main take-away from the benchmark is that all implementations have very good performance - that is, we didn't screw up in jox, or Kotlin didn't screw up comparing to Go.
Does this work ok in Scala 2/3, given the correct JDK?
@Jankoekepannekoek
17 күн бұрын
It's a Java library, so it's independent of the Scala version.
@AdamWarski2
17 күн бұрын
As already mentioned, it works with Scala 2 and 3, requires JDK 17+. Jox is also used in the Scala Ox library.
So select gives us the result of the 1st completed task (if there are n tasks, each of them must have its own channel, right?). Will it cancel other tasks when 1 task finishes?
@AdamWarski2
16 күн бұрын
Yes, you pass N clauses to the `select` method. The 1st one that can be completed (either immediately, or after blocking) will be the result of the `select`. All other clauses are cancelled, and that's the whole point: *exactly* one clause will be completed. Each clause must correspond to a different channel, yes, otherwise you'll get an exception.
@avalagum7957
16 күн бұрын
@@AdamWarski2 how about the cancelation? Will other tasks be canceled when one task completes?
@AdamWarski2
16 күн бұрын
@@avalagum7957 Yes, if by "task" you mean the clauses that are passed to the `select`. So if you have `select(ch1.receiveClause(), ch2.receiveClause())`, and a value is received from `ch1`, the "task" represented by `ch2.receiveClause()` will be cancelled, and it's guaranteed that it won't receive a value from `ch2`.