News for hackers. Stuff that matters.
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by merb on Monday May 02, 2022 @01:36PM (#31238221)
because green threads and async/await have both upsides and downsides. if it would've been as easy as the comment would've described it, they would've shipped it in jdk 8, but they didn't. they ship a preview in jdk 19.

rust's rfc explains some of the drawbacks: https://github.com/rust-lang/rfcs/blob/master/text/0230-remo... (which basically explains why a preview took until jdk 19)

// Edit: also:

    void handle(Request request, Response response) {
    var url1 = ...
    var url2 = ...
 
    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
        var future1 = executor.submit(() -> fetchURL(url1));
        var future2 = executor.submit(() -> fetchURL(url2));
        response.send(future1.get() + future2.get());
    } catch (ExecutionException | InterruptedException e) {
        response.fail(e);
    }
    }
is not better or worse than:

    async Task<IActionResult> Handle()
    {
        var url1 = ...
        var url2 = ...

        try {
            var requestTask1 = FetchURL(url1);
            var requestTask2 = FetchURL(url2);
            return Ok((await requestTask1) + (await requestTask2));
        }
        catch (Exception ex)
        {
            return BadRequest();
        }
    }
of course one uses "colored" functions the other not, but both have one thing in common: a bad programmer can make serious mistakes with both.
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by pron on Monday May 02, 2022 @01:50PM (#31238417)
> is not better or worse than

It is better for two reasons:

1. Virtual threads are threads as far as the Java platform is concerned, meaning you get the same troubleshooting (stack traces), debugging, and profiling support by the runtime and its tools as for platform (OS-backed) threads.

2. There is no split-world of APIs, with separate and largely incompatible flavours, that need to be developed for two constructs that are semantically (almost) equivalent.

You are correct, however, that implementing user-mode threads is harder than implementing async/await. While the latter requires only changes to the frontend compiler (until you want to add tooling support), the former requires deep changes to the backend.

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by merb on Monday May 02, 2022 @02:09PM (#31238653)
> 1. Virtual threads are threads, meaning you get the same troubleshooting (stack traces), debugging, and profiling support by the runtime and its tools as for "platform" (i.e. OS-backed) threads.

I never had any problem with debugging and profiling Futures/async/await. Neither in Rust/C#/Scala. And stack traces like Exceptions were never a problem, in fact go panics were way more troublesome... (and still are, most often you dont run into them...) Most of the time depending how "concurrent" your program is, it also makes just no sense to debug them anyway, if you deal with tons of concurrent code you should learn to make good log statements, it's bread and butter, especially once your in production.

> 2. There is no split-world of APIs, with separate and largely incompatible flavours, that need to be developed for two constructs that are semantically (almost) equivalent.

well yeah... but the question is why do you try to hide it? that programmers don't see that the code might run concurrent? most goroutine bugs I've seen happened because of the hidden concurrency. and it's odd that an important concept gets hidden, for the sake of split-world apis. heck thats exactly why there is a type system, to express the meaning of code. with green threads it's not really obvious on first sight what the code exactly expresses.

(P.S.: I switched pretty early in my career to Scala, now to Rust/Golang/C# so maybe I'm more biased when it comes to a strong type system and I prefer a strong typesystem with all it's quirks, i've seen nasty bugs with goroutines and with async/await, so I doubt there is any real world benefit over the two worlds)

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by pron on Monday May 02, 2022 @04:30PM (#31240501)
> I never had any problem with debugging and profiling Futures/async/await.

When you profile a server written in the asynchornous style (with, say, JFR), the server can be under heavy load and yet the profile will only show idle thread pools. I don't know what you mean by having no problem profiling, but the Java platform offers no mechanism to profile asynchronous code.

> but the question is why do you try to hide it?

We don't. Virtual threads are threads, and just like threads today Java or Scala or Rust, you need some explicit operation to perform a concurrent task on some other thread.

> and I prefer a strong typesystem

Great, but that has nothing to do with threads. Rust and Scala support threads, too, and do so without any indication in the subroutine's type for blocking operations.

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by pie_flavor on Monday May 02, 2022 @02:51PM (#31239233)
If you assume it's not concurrent when it is, that's the programmer's fault. This is true of both goroutines and threads in equal capacity; distinguishing between the two won't stop that from happening. If you assume it is concurrent when it isn't, nothing bad happens. The point of 'function color' is that hiding it implies it needs to be shown, which it doesn't. Rust hides from you which register a local variable is stored in, because it is thoroughly irrelevant to you and exposing it would require you to care when Rust is perfectly capable of managing it for you.

There are legitimate reasons to prefer a colored approach to asynchrony. But the two points are good points and you should prefer the colored approach only when said legitimate reasons outweigh those points given the language's design.

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by Thaxll on Monday May 02, 2022 @03:13PM (#31239498)
Troubleshooting async is a pain in every language I worked with , that being node or Java ( vertx ). It's just terrible to get the real error, stack trace that don't even show the real issue, in Java I remember many times getting 150 lines of useles stacktraces, still figuring out from where the error was thrown.

A panic in Go will always show you exactly where the problem is in the first lines of the panic.

Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by nu11ptr on Monday May 02, 2022 @01:38PM (#31238248)
To be fair, the comment above is about how easy it is for the _user_. Having read docs on go's scheduler design I don't think many would argue it is easy to write such a runtime for the core devs.
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by merb on Monday May 02, 2022 @01:43PM (#31238312)
I added a snippt which also explains the user side, I doubt either has a benefit. both can be misued. I already seen bad go code because of this and I've also seen bad C# code. In fact go code is harder to reason because of this, since you do not know exactly when a function spawns a new goroutine.
Re:JEP proposed to target JDK 19: 425: Virtual Threads (Prev... by nu11ptr on Monday May 02, 2022 @02:00PM (#31238526)
I don't think it is a huge difference, but I still prefer the straight threading model. It uses less keywords, looks like regular blocking code, and most importantly, there is exactly "one" world - not a sync vs. async world that have to coexist. I'm speaking generically, not specific to Java (which I have not written in many years).

> In fact go code is harder to reason because of this, since you do not know exactly when a function spawns a new goroutine.

This is no different in the async/await world, as you never know what function will launch a new "task" (this concept exists in Rust/Python and I assume others). If you don't need a new "thread", both models allow waiting for multiple things simultaneously.