News for hackers. Stuff that matters.
Re:Java 21 makes me like Java again by insanitybit on Saturday September 16, 2023 @06:26PM (#37539402)
`Executor.newVirtualThreadPerTaskExecutor` versus `go` really gets to the heart of why I think that Go developers aren't going to be switching.

edit: Sorry, it's actually:

    try (var executor = 
        Executors.newVirtualThreadPerTaskExecutor()) {
       executor.submit(...)
    )
instead of `go`
Re:Java 21 makes me like Java again by kaba0 on Saturday September 16, 2023 @06:29PM (#37539430)
So basically executor.submit(). And you still have a language that is significantly less verbose than go, objectively.
Re:Java 21 makes me like Java again by insanitybit on Saturday September 16, 2023 @06:40PM (#37539511)
Yes, if you ignore more than half of the code it's quite concise.
Re:Java 21 makes me like Java again by kaba0 on Saturday September 16, 2023 @07:11PM (#37539754)
How many thread pools do you want to use? You only need that once.
Re:Java 21 makes me like Java again by ivan_gammel on Saturday September 16, 2023 @06:30PM (#37539440)
Wrap it in class Go { static void go(…) } and it will look better with “import static”
Re:Java 21 makes me like Java again by insanitybit on Saturday September 16, 2023 @06:44PM (#37539541)
I assume `Go` would have to be Closeable and you'd still need the try-with-resources, right?
Re:Java 21 makes me like Java again by kaba0 on Saturday September 16, 2023 @07:14PM (#37539778)
You don't need a try-with-resources, you can manually call close if you want, or has to work in a non-lexical scope.

But you often want the concurrently running threads to return some results, that try-with-resources is part of the structured concurrency that helps you with closing off a branching point, similarly to how we use while loops instead of gotos. `go` in itself corresponds to `goto` basically, with many of the same negatives.

Re:Java 21 makes me like Java again by ivan_gammel on Sunday September 17, 2023 @01:00AM (#37541673)
Maybe you re-read my comment, check the signature of Go::go() and think again?
Re:Java 21 makes me like Java again by erik_seaberg on Saturday September 16, 2023 @06:47PM (#37539565)
You’re likely to need a lot of boilerplate that “go” statement won’t generate: pass and wait for completed results, report errors, timeouts, cancellation, bounded parallelism, pushback, service monitoring.
Re:Java 21 makes me like Java again by insanitybit on Saturday September 16, 2023 @06:48PM (#37539576)
My comment is perhaps too glib. I'm not trying to say that Go is better, I frankly like Java more. I only mean that if I'm in a Go mindset and I read the linked document on virtual threads, and I see that code example, I'm going to close the tab.
Re:Java 21 makes me like Java again by chrisdinn on Saturday September 16, 2023 @08:19PM (#37540212)
Been a while for me but is that three line Java snippet doing service monitoring? Go has simple tools for all those purposes, too, obviously.

I rarely see anyone mention this but having built the exact same large system on the JVM and then in Go (post Google acquisition forced to rewrite) the biggest benefit of Go at scale for us (besides dev velocity) was gc performance.

Also, Go has to be saving people a ton on LLM tokens.

Re:Java 21 makes me like Java again by mattbee on Saturday September 16, 2023 @07:46PM (#37539987)
Starting a virtual thread in Java isn't that clumsy:

   Thread.ofVirtual().start(() -> System.out.println("Hello"))
Re:Java 21 makes me like Java again by zaphirplane on Saturday September 16, 2023 @07:50PM (#37540010)
The go example is missing the channels, select, wait group, context, cancellation

Doesn’t sound like a fair comparison thou the Java version is missing things

Re:Java 21 makes me like Java again by insanitybit on Sunday September 17, 2023 @12:03AM (#37541414)
> The go example is missing the channels, select, wait group, context, cancellation

The Java version would require all of those in the exact same way though.

Re:Java 21 makes me like Java again by za3faran on Sunday September 17, 2023 @04:06AM (#37542670)
Java has structured concurrency in the works, which will not be much more verbose, unlike golang.
Re:Java 21 makes me like Java again by foolfoolz on Saturday September 16, 2023 @11:17PM (#37541188)
go will always lose vs java when it comes to code verbosity
Re:Java 21 makes me like Java again by lenkite on Sunday September 17, 2023 @03:11AM (#37542324)
Extend that to a full function and the Go code will be 2x more verbose than Java with all the `if err != nil` stuff.
Re:Java 21 makes me like Java again by mike_hearn on Sunday September 17, 2023 @06:15AM (#37543433)
If you want to use that with more concise syntax, there is Kotlin for that. In which case abstracting that is one line of code tucked away in a utility file:

    fun <T> go(body: ExecutorService.() -> T): T =
        Executors
            .newVirtualThreadPerTaskExecutor()
            .use(body)
Now you can write:

    val result = go {
       val result1 = submit { slowOp() }
       val result2 = submit { blockingOp() }
       result1.get() + result2.get()
    }
or words to that effect. You can also reduce it a lot in Java too, as mentioned by another commenter.

    class Shortcuts {
        static <R> R go(Function<ExecutorService, R> task) throws RuntimeException {
            try (....) { return task.apply(service) }
        }
    }
and then you can write:

    var result = go(service -> {
        var result1 = service.submit(() -> slowOp());
        var result2 = service.submit(() -> blockingOp());
        result1.get() + result2.get();
    });
using static imports.

Now if you're making a cultural point then sure, Java APIs tend to be named quite explicitly. It has advantages when searching or auto-completing, and it's easy to wrap with aliases if you want to abstract a boilerplate pattern away.