BCNJUG-Revolutionize Java DB AppDev With Reactive Streams and Virtual Threads
BCNJUG-Revolutionize Java DB AppDev With Reactive Streams and Virtual Threads
BarcelonaJUG
Juarez Barbosa Junior @juarezjunior
Sr. Principal Java Developer Evangelist
ORACLE
•
Database access with blocking threads
•
A JDBC call blocks a thread for 100s of milliseconds
(thread-per-request style)
•
Thread count increases linearly with the number of
JDBC calls
•
Performance degrades as the number of threads
increases
•
1 MB of stack memory per thread typically
•
Scheduling many platform threads is expensive
•
Preemptive scheduling vs time-slicing
Copyright © 2023, Oracle and/or its affiliates
Virtual Threads - JEPs 425,
436, 444
•
Virtual Threads – JEPs (JDK Enhancement Proposals)
•
Lightweight (user mode) threads that dramatically reduce the effort of writing, maintaining,
and observing high-throughput concurrent applications
•
Enable applications written in the simple thread-per-request style to scale with near-
optimal hardware utilization
•
Enable easy troubleshooting, debugging, and profiling of virtual threads with existing JDK
tools
•
Do not remove the traditional implementation of threads
•
Do not alter the basic concurrency model of Java
•
Enable existing code that uses Java threads (java.lang.Thread) to adopt virtual threads with
minimal change
Threads •
java.lang.Thread
•
static Thread.Builder.OfVirtual - ofVirtual()
425, •
static Thread.Builder.OfPlatform - ofPlatform()
static Thread - startVirtualThread(Runnable task)
436, 444
•
•
java.lang.Thread.Builder
•
java.util.concurrent.Executors
•
static ExecutorService -
newVirtualThreadPerTaskExecutor()
•
java.lang.Thread.Builder
•
Thread defines a
Thread.Builder API for
creating and starting
both platform and virtual
threads. The following
are examples that use
the builder:
Record JDBC
HTTP / REST Streaming
over Containers Direct Path
IoT Devices /
static String queryJdbc(java.sql.Connection connection) throws SQLException { static Publisher<String> queryR2dbc(io.r2dbc.spi.Connection connection) {
•Want Reactive?
• Oracle R2DBC 1.1.1 is available now
• Consume Flow interfaces directly
from Oracle JDBC’s Reactive
Extensions
Copyright © 2023, Oracle and/or its affiliates
Virtual Threads or Reactive?
•Benefits of Virtual Threads: Benefits of Reactive:
• Easier to read and write • Reactive Libraries (Reactor, RxJava, Akka,
Vert.x)
• Easier to debug
• Stream-like API with a functional style
• Integration with JDK tools
• • Low-level concurrency is handled for you
Do not alter the basic concurrency model of Java
(locks, atomics, queues)
• Now available in JDK 21
Database
Oracle’s mission to make it simple
to develop and run all data-driven
apps
23 c
• Provides developers easy early
access to 23c app dev features
600+
events run
using
LiveLabs
workshops
developer.oracle.com/livelabs
learn something new …at your pace!
500+ technical experts &
community leaders helping peers
globally
3 membership tiers
Nominate
yourself or a candidate:
ace.oracle.com/nominate
Learn more -
ace.oracle.com
Connect: @oracleace
[email protected] facebook.com/OracleACEs blogs.oracle.com/ace
Create your FREE
Cloud Account
• Go to
https://signup.cloud.oracle.com/