It was recently announced that virtual threads, a new JDK feature developed by the OpenJDK project Loom, will be available as a preview feature in the forthcoming Java 19 release.
One of the design goals of this new feature is to enable existing multi-threaded Java apps to adopt virtual threads with little or no change. This has to all intents and purposes been achieved in a couple of ways. Firstly, virtual threads are able to run any existing Java code or native code – there are no restrictions in that respect. Secondly, there are no breaking changes to the existing Thread API, or associated language constructs such as synchronized blocks, should your code use these lower level thread management APIs. However, attaining the full benefits of virtual threads requires Java developers to make a few changes to the way they have solved certain problems in the past. This is euphemistically described by the Loom team as needing to ‘unlearn’ certain coding practices and ways of doing things. This article outlines these cases, highlighting how you can prepare your existing applications to maximise the benefits of virtual threads when they are finalised in a future release of Java; and more generally considers how the introduction of virtual threads may change the way we develop multithreaded Java apps in the future.Continue reading