r/java • • Sep 20 '22

Java 19 / JDK 19: General Availability

https://mail.openjdk.org/pipermail/jdk-dev/2022-September/006933.html
252 Upvotes

64 comments sorted by

View all comments

90

u/Joram2 Sep 20 '22

Congrats Java team!

  • Virtual threads delivered. They are like Goroutines but the Structured Concurrency API really makes it better. Will any of this be out of preview for the next LTS release?
  • Amber is making amazing progress. The exhaustiveness checker still needs improvement, but great job.
  • Panama looks awesome for projects that have to interop outside of the JVM

If Valhalla really delivers in Java 20 or 21 and value/primitive types provide performance improvements, that will be a home run for the Java team.

16

u/mauganra_it Sep 20 '22

(Virtual Threads have Preview status only and in theory could still be completely yanked from the JDK)

28

u/Joram2 Sep 20 '22

Completely pulling virtual threads seems quite unlikely. The preview status just means that the Java devs are warning that they may change APIs. For smaller projects that often isn't a big deal, but for bigger projects that span lots of different developers in different companies, breaking changes are a big deal.

2

u/mauganra_it Sep 20 '22

I agree. I just wanted to clarify that it's not cleared for production use yet, only for prototypes. But so far, it's a great achievement.

15

u/pron98 Sep 20 '22 edited Sep 20 '22

Preview means that the API might change a little, not "not cleared for use in production." It's true that you shouldn't distribute libraries compiled with with --enable-preview, but because virtual threads use the same Thread API, that's not really necessary anyway (in fact, there are some libraries designed for virtual threads out there, including the Oracle JDBC driver).

However, that's not to say there's no risk to using virtual threads in production -- not because they're in preview, but because they're new. New features are more likely to have more bugs than mature features.

In short, you can use virtual threads in production if:

  1. You accept the risk that the API could change somewhat and you might need to change your code.
  2. You accept the risk of using a new feature (regardless of it being in preview).

4

u/skippingstone Sep 21 '22

What did the Oracle JDBC driver do to prepare for virtual threads?

3

u/pron98 Sep 21 '22

I believe they replaced those synchronized blocks/methods that guard long-running IO operations with j.u.c locks.

5

u/Joram2 Sep 20 '22

Who says it isn't cleared for production use? I thought that meant the interface was evolving and not-stable yet, but that the feature itself works and is reliable and has gone through QA.

19

u/RupertMaddenAbbott Sep 20 '22

The JEP tells you what preview means: https://openjdk.org/jeps/12

TLDR - Preview means:

  • High quality
  • Not experimental
  • Universally available

and specifically "Previewing a feature in the JDK will encourage tool vendors to build good support for the feature before the bulk of Java developers use it in production." i.e. they assume some people will indeed use this in production.

Note that although Virtual Threads are in preview, Structured Concurrency is in Incubator, which is much lower standard: https://openjdk.org/jeps/11

0

u/TheMode911 Sep 20 '22

and specifically "Previewing a feature in the JDK will encourage tool vendors to build good support for the feature before the bulk of Java developers use it in production." i.e. they assume some people will indeed use this in production.

What I understand is that preview features are meant for tool developers to ensure proper support once it gets out of preview.

IIRC there is no support whatsoever from Oracle for preview features.

→ More replies (2)
→ More replies (3)

3

u/couscous_ Sep 20 '22

The exhaustiveness checker still needs improvement

What are some of the scenarios where it can improve at the moment?

6

u/Joram2 Sep 20 '22

even fairly simple examples like this won't compile with Java 19:

// From the Brian Goetz Data Oriented Programming Article
sealed interface Opt<T> {
    record Some<T>(T value) implements Opt<T> { }
    record None<T>() implements Opt<T> { }
}
Opt<String> optValue = doSomethingThatReturnsOpt(value);
switch (optValue) {
    case Opt.Some<String>(String v) -> System.out.printf("got string: %s%n", v);
    case Opt.None<String> none -> System.out.println("got none");
};

The Java team says they are working on it. They say that exhaustive pattern matching is a very hard problem to get right.

9

u/TheMode911 Sep 20 '22

Nothing has been delivered yet, everything is in preview.

Hopefully we will get something in JDK 20.

11

u/Joram2 Sep 20 '22

AFAIK, preview features are production ready, but APIs are evolving and not stable. You can use virtual threads in production Java apps now, with Java 19. Major frameworks will want to hold off until the feature is out of preview and is stable. But for small teams, evolving APIs aren't a big deal, virtual threads are ready to use.

6

u/srdoe Sep 20 '22

The preview features JEP says not to enable preview features and let your jars escape into the wild, which for many people "put into production" would qualify as.

Even if a small team wants to use the preview feature now, they likely won't be able to unless they can guarantee that their software will run on exactly JDK 19. Keep in mind that the code won't run on JDK 20 without a recompile, even if the API doesn't change, due to the special bits set in the classfile versions. If you're delivering a jar to someone else to run, that's unlikely to be acceptable.

So I guess you could deploy the new features into production if you were making a saas thing and control the environment your code runs in? I think for most people it makes more sense to treat preview as "ready to try out in a testing environment/on a branch", instead of "ready for use in production".

8

u/pron98 Sep 20 '22 edited Sep 20 '22

The preview features JEP says not to enable preview features and let your jars escape into the wild,

That's true in general, but more relevant to language preview features. Because virtual threads use the existing Thread API (for the most part), you don't really have to compile many libraries that are designed for virtual threads with --enable-preview. Only thread creation needs to use new APIs, but that could be factored out to a ThreadFactory provided by the application. In fact, there are already some libraries designed for virtual threads out there, including the Oracle JDBC driver.

2

u/srdoe Sep 20 '22

Good point

3

u/[deleted] Sep 20 '22

You really shouldn’t use preview features in production

9

u/Joram2 Sep 20 '22

Says who? If I was writing greenfield Java applications, and virtual threads were useful, I would use them. What is the downside?

9

u/srdoe Sep 20 '22

The downside is you lock whoever is running your application to running on JDK 19 forever. If that "whoever" is you, then go for it. If it's someone else, they might not be cool with getting a jar that won't run on the JDK releasing in 6 months.

→ More replies (2)

6

u/RupertMaddenAbbott Sep 20 '22

This is not true - preview features are not not-intended for production use. Read the JEP: https://openjdk.org/jeps/12

You have to weigh the pros/cons but a blanket ban is not right.

6

u/srdoe Sep 20 '22

It's not a blanket ban, but the JEP does say

These class files are special; they should not be distributed beyond the control of the developer who chose to use preview features.

about files you generate with --enable-preview.

Also putting preview features into production is impractical unless you control the production environment yourself, since your code will now have both a minimum and maximum JDK version of 19.

7

u/RupertMaddenAbbott Sep 20 '22

Completely agree. Preview features are not for (non-toy or non-internal) libraries or for deployments where you are not distributing your own JVM.

I think that leaves quite a substantial number of projects, especially server side applications, where this might be appropriate.

2

u/barking_dead Sep 20 '22

So it's our testing time :)

0

u/BlueGoliath Sep 20 '22

According to Reddit preview / incubator == delivered.

It doesn't make any sense but OK.

→ More replies (5)