In this presentation, we will explain the motivation, added values, challenges and current status of the Valhalla and Loom projects.
In the Valhalla project, Inline Type is introduced in Java. Inline Type is an immutable type that differs only by the state of its properties. The purpose is to reduce memory consumption and access times for such data types. Also as a part of this project Java type system will be unified so that Java will become a pure object-oriented programming language.
In the Loom project, lightweight threads are implemented in Java. The purpose is to no longer trade off between simplicity and scalability of the source code and to reconcile both.
2. Contact
Vadym Kazulkin
ip.labs GmbH Bonn, Germany
Co-Organizer of the Java User Group Bonn
and Serverless Bonn Meetup
v.kazulkin@gmail.com
@VKazulkin
https://www.linkedin.com/in/vadymkazulkin
https://www.iplabs.de/
7. Project Valhalla
(Initial) Goal:
Reboot the layout of data in memory
Source: Brian Goetz, Oracle „Evolving the Java Language” https://www.youtube.com/watch?v=A-
mxj2vhVAA
8. Project Valhalla
Motivation:
Hardware has changed
• Multi-core
• The cost of cache misses has increased
Source: Brian Goetz, Oracle „Evolving the Java Language” https://www.youtube.com/watch?v=A-
mxj2vhVAA
12. Project Valhalla
Storing objects in the Java Heap has its price, because storing
object’s metadata consumes additional memory for :
• flags facilitating synchronization/locking
• Identity and polymorphismus
• garbage collection
14. Project Valhalla
Inline Types
Immutable: an instance of an inline-type can’t change, once it’s
been created
Identity-less: inline-types of the same type with the same contents
are indistinguishable from each other
Flattenable: JVMs are allowed to flatten an inline-type inside of its
container
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
17. Project Valhalla
Benefits:
• Reduced memory usage
• Reduced indirection
• Increased locality
Codes like a class, works like a primitive
(Brian Goetz)
18. Project Valhalla
Benefit: Reduced Memory Usage
No additional memory to store object metadata, such as
flags facilitating synchronization, identity, polymorphism and
garbage collection
19. Project Valhalla
Benefit: Reduced indirection
Since objects are stored as reference types in Java, each time
an object is accessed it must first be dereferenced, causing
additional instructions to be executed
The flattened data associated with inline types are
immediately present in the location in which they are needed
and therefore, require no dereferencing
20. Project Valhalla
Benefit: Increased locality
Flattened value objects remove indirection which increases
the likelihood that values are adjacently stored in memory–
especially for arrays or other contiguous memory structures
such as classes (i.e. if a class contains inline type fields)
Increases the chance of cache hits, because of hardware
prefetch of the cache lines
22. Project Valhalla
Inline Types
Can
• have method and field
• implement interfaces
• extend “well-formed” abstract class
• no fields
• empty no-arg constructor
• no synchronized methods
• use encapsulation
• be generic
Can’t
• be mutated
• be sub-classed (inline class is final)
• be cloned
• be Enums
• be used synchronization (IllegalMonitorStateException)
• be null
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
23. Project Valhalla
Types Hierarchy
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
Object
InlineObject
Inline Types
2 new interfaces IdentityObject
Reference Types
25. Project Valhalla
Reference and Value Projection
inline class V {} our code
sealed abstract class V.ref permits V.val {}
will be generated
inline class V.val extends V.ref {}
V – inline type
V.ref - reference projection for V
V.val - value projection for V
Source: Sergej Kuksenko „Valhalla is coming“ https://www.youtube.com/watch?v=ri5i3mnSNk8
26. Project Valhalla
Migrations of existing code
Map <K,V> V get (Object key)
Returns the value to which the specified key is mapped, or null if this map
contains no mapping for the key.
-> Map <K,V> V.ref get (Object key)
Optional <T> o;
o=null;
-> reference projection of Optional will be used by default. Rewrite
your code to use value projection for Optional with Inline classes
Source: Sergej Kuksenko „Valhalla is coming“ https://www.youtube.com/watch?v=ri5i3mnSNk8
27. Project Valhalla
Types Hierarchy
Source: Tobi Ajila “Welcome to LWorld: The current state of value types in Java “
https://www.youtube.com/watch?v=Xf22I16jVyE&list=LLYgjRSI2oCzI9eooyFrWR7A&index=11
Object
InlineObject
Inline Types
2 new interfaces IdentityObject
Reference Types
Primitive
types
28. Project Valhalla
Migrations of existing code
Yes, what about primitives?
inline class int {….}
Integer == int.ref
Integer.val==int
Benefits:
• Java becomes true OOP language
• Full covariant arrays : int[] <: Integer [] <: Object []
• Future support of things like List<int> will become easier
Source: Sergej Kuksenko „Valhalla is coming“ https://www.youtube.com/watch?v=ri5i3mnSNk8
https://openjdk.java.net/jeps/402
29. Project Valhalla
Migrations of the primitives
Source: https://openjdk.java.net/jeps/401, https://openjdk.java.net/jeps/402
30. Project Valhalla
(Initial) Goal:
Reboot the layout of data in memory
shifts to
Unify the Java type system
Source: Brian Goetz, Oracle „Evolving the Java Language” https://www.youtube.com/watch?v=A-
mxj2vhVAA
31. Project Inline classes vs Records
• A record requires you to give up on extension, mutability, and the ability to
decouple the representation from the API.
• In return, you get implementations of constructors, accessors and identity-based
equals and hashCode
• Inline class requires you to give up on identity, which includes giving up on
extension and mutability, as well as some other things (e.g., synchronization).
• In return, you get a different set of benefits: flattened representation, optimized
calling sequences, and state-based equals and hashCode.
Source: https://stackoverflow.com/questions/63352151/are-java-records-intended-to-
eventually-become-value-types
34. Project Loom
Motivation:
Developers currently have 2 choices to write concurrent
code:
• use blocking/synchronous API, which is simple, but less scalable (number of
threads, that OS supports is far less that open and concurrent connections
required)
• asynchronous API (Spring Project Reactor, RXJava 2), which is scalable, but
complex, harder to debug and profile and limited (no asynchronous JDBC
standard in this area)
Sources: Alan Bateman, Oracle „Project Loom: Fibers and Continuations for Java”
https://www.youtube.com/watch?v=vbGbXUjlRyQ
36. Project Loom
Lightweight Thread
Virtual thread scheduled not by the OS, but by the Java Runtime
with low memory footprint and low task-switching cost
Sources: https://inside.java/2020/07/29/loom-accentodev/
38. Continuation
package java.lang;
public class Continuation {
public Continuation (ContinuationScope scope, Runnable target)
public final void run()
public static void yield (ContinuationScope scope)
public boolean isDone()
}
39. Project Loom
Continuations
example () {
var scope = new ContinuationScope(„Example_Scope“);
var continuation = new Continuation (scope, () -> {
out.print(„1“);
Continuation.yield(scope);
out.print(„2“);
Continuation.yield(scope);
out.print(„3“);
Continuation.yield(scope);
});
while (! continuation.isDone()) {
out.print(„ run.. “);
continuation.run();
}
}
Output: run.. 1 run.. 2 run.. 3
41. Project Loom
Schedular
Schedular executes the task on a pool of carrier threads
java.util.concurrent.Executor API exposes the Schedular
Default schedular is a ForJoinPool
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
42. Project Loom
Virtual Thread Implementation
Currently Thread and Virtual Thread don’t have a common
supertype
Thread.currentThread() in context of Virtual Thread
• Creates adaptor (Shadow Thread)
• Adaptor emulates legacy Thread API (except deprecated methods like stop,
suspend and resume)
• Thread Local becomes Virtual Thread Local
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
43. Project Loom
Virtual Thread Implementation
Thread t = Thread.startVirtualThread (() -> {
System.out.println(„Hello World“);
});
Thread t = Thread.builder().virtual().task( () -> { … }).build();
Thread t = Thread.builder().virtual().task( () -> { … }).start();
ThreadFactory factory = Thread.builder().virtual().factory();
Source: Ron Pressler: “Project Loom: Modern Scalable Concurrency for the Java”
https://www.youtube.com/watch?v=23HjZBOIshY
https://cr.openjdk.java.net/~rpressler/loom/loom/
44. Project Loom
Structured Concurrency
Basic idea: Everytime that the control splits into multiple
concurrent paths, we want to guarantee that they join up again
try (var executor= Executors.newVirtualThreadExecutor()) {
executor.submit(task1);
executor.submit(task2);
} //blocks until task1 and task2 terminate
Sources: Nathanial J. Smith „Notes on structured concurrency, or: Go statement considered harmful”
https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/
R. Elizarov: “Structured concurrency with Coroutines in Kotlin” https://medium.com/@elizarov/structured-concurrency-
722d765aa952
45. Project Loom
Structured Concurrency
Cancelation :
We can also give all tasks a deadline that will interrupt those children
that have yet to terminate by the time it expires (as well as the current
thread)
try (var executor= Executors.newVirtualThreadExecutor().
withDeadline(Instant.now().plusSeconds(60))) {
executor.submit(task1);
executor.submit(task2);
}
Source: Ron Pressler: “Project Loom: Modern Scalable Concurrency for the Java Platform”
https://www.youtube.com/watch?v=EO9oMiL1fFo https://cr.openjdk.java.net/~rpressler/loom/loom/
47. Project Loom
Structured Executor Class
Sources: StructuredExecutor Class
https://download.java.net/java/early_access/loom/docs/api/java.base/java/util/concurrent/StructuredExecutor.html
49. Project Loom
Virtual Thread
Current Status:
Virtual Thread currently supports:
• scheduling
• parking/unparking
• waiting for a Virtual Thread to terminate
Virtual Thread -friendly APIs
• java.util.concurrent Locks
• java.io.Console, Reader, Writer Buffered/File/Piped I/O Streams
• Java.util.concurrent Executors, SynchronousQueue,
• java.net.Socket/ServerSocket/InetAdress (
• java.nio.channels.SocketChannel and Pipes
• Thread.sleep
• JSSE implementation of TLS
• AccessControl.doPrivileged
Source: Ron Pressler, Project Loom: Helping Write Concurrent Applications on the Java Platform
https://www.youtube.com/watch?v=lIq-x_iI-kc
50. Project Loom
Current Status:
• Refine the prototype with Continuation and Virtual Thread
support
• Current prototype of Continuations and Virtual Thread can
run existing code
• Debugger Support
Current focus on:
• Performance improvements
• Stable Virtual Thread API
• Java Flight Recorder support
Source: Alan Bateman, Oracle „Project Loom Update” https://www.youtube.com/watch?v=NV46KFV1m-4
51. Project Loom
Open Questions:
Should the existing Thread API be completely re-examined?
Can all existing code be run on top of Virtual Threads?
Current answers:
• Since Java 5 and 6 developers and library creators are
encouraged to use Executors and ThreadFactory APIs instead of
Thread directly
• In order to use Virtual Thread instead of Thread another Executor
implementation must be chosen
ThreadFactory factory = Thread.builder().virtual().factory();
Executor executor= Executors.newVirtualThreadExecutor(factory);