🔏
Tech
  • 🟢App aspects
    • Software architecture
      • Caching
      • Anti-patterns
      • System X-ability
      • Coupling
      • Event driven architecture
        • Command Query Responsibility Segregation (CQRS)
        • Change Data Capture (CDC)
      • Distributed transactions
      • App dev notes
        • Architecture MVP
      • TEMP. Check list
      • Hexagonal arch
      • Communication
        • REST vs messaging
        • gRPC
        • WebSocket
      • Load balancers
      • Storage limits
      • Event storming
    • Authentication
    • Deployment strategy
  • Databases
    • Classification
    • DB migration tools
    • PostreSQL
    • Decision guidance
    • Index
      • Hash indexes
      • SSTable, LSM-Trees
      • B-Tree
      • Engines, internals
    • Performance
  • System design
    • Interview preparation
      • Plan
        • Instagram
        • Tinder
        • Digital wallet
        • Dropbox
        • Live video streaming
        • Uber
        • Whatsup
        • Tiktok
        • Twitter
        • Proximity service
    • Algorithms
    • Acronyms
  • 🟢Programming languages
    • Java
      • Features
        • Field hiding
        • HashCode() and Equals()
        • Reference types
        • Pass by value
        • Atomic variables
      • Types
      • IO / NIO
        • Java NIO
          • Buffer
          • Channel
        • Java IO: Streams
          • Input streams
            • BufferedInputStream
            • DataInputStream
            • ObjectInputStream
            • FilterInputStream
            • ByteArrayInputStream
        • Java IO: Pipes
        • Java IO: Byte & Char Arrays
        • Java IO: Input Parsing
          • PushbackReader
          • StreamTokenizer
          • LineNumberReader
          • PushbackInputStream
        • System.in, System.out, System.error
        • Java IO: Files
          • FileReader
          • FileWriter
          • FileOutputStream
          • FileInputStream
      • Multithreading
        • Thread liveness
        • False sharing
        • Actor model
        • Singleton
        • Future, CompletableFuture
        • Semaphore
      • Coursera: parallel programming
      • Coursera: concurrent programming
      • Serialization
      • JVM internals
      • Features track
        • Java 8
      • Distributed programming
      • Network
      • Patterns
        • Command
      • Garbage Collectors
        • GC Types
        • How GC works
        • Tools for GC
    • Kotlin
      • Scope functions
      • Inline value classes
      • Coroutines
      • Effective Kotlin
    • Javascript
      • Javascript vs Java
      • TypeScript
    • SQL
      • select for update
    • Python
  • OS components
    • Network
      • TCP/IP model
        • IP address in action
      • OSI model
  • 🟢Specifications
    • JAX-RS
    • REST
      • Multi part
  • 🟢Protocols
    • HTTP
    • OAuth 2.0
    • LDAP
    • SAML
  • 🟢Testing
    • Selenium anatomy
    • Testcafe
  • 🟢Tools
    • JDBC
      • Connection pool
    • Gradle
    • vim
    • git
    • IntelliJ Idea
    • Elastic search
    • Docker
    • Terraform
    • CDK
    • Argo CD
      • app-of-app setup
    • OpenTelemetry
    • Prometheus
    • Kafka
      • Consumer lag
  • 🟢CI
    • CircleCi
  • 🟢Platforms
    • AWS
      • VPC
      • EC2
      • RDS
      • S3
      • IAM
      • CloudWatch
      • CloudTrail
      • ELB
      • SNS
      • Route 53
      • CloudFront
      • Athena
      • EKS
    • Kubernetes
      • Networking
      • RBAC
      • Architecture
      • Pod
        • Resources
      • How to try
      • Kubectl
      • Service
      • Tooling
        • ArgoCD
        • Helm
        • Istio
    • GraalVM
    • Node.js
    • Camunda
      • Service tasks
      • Transactions
      • Performance
      • How it executes
  • 🟢Frameworks
    • Hibernate
      • JPA vs Spring Data
    • Micronaut
    • Spring
      • Security
      • JDBC, JPA, Hibernate
      • Transactions
      • Servlet containers, clients
  • 🟢Awesome
    • Нейробиология
    • Backend
      • System design
    • DevOps
    • Data
    • AI
    • Frontend
    • Mobile
    • Testing
    • Mac
    • Books & courses
      • Path: Java Concurrency
    • Algorithms
      • Competitive programming
    • Processes
    • Finance
    • Electronics
  • 🟢Electronics
    • Arduino
    • IoT
  • Artificial intelligence
    • Artificial Intelligence (AI)
  • 🚀Performance
    • BE
  • 📘Computer science
    • Data structures
      • Array
      • String
      • LinkedList
      • Tree
    • Algorithms
      • HowTo algorithms for interview
  • 🕸️Web dev (Frontend)
    • Trends
    • Web (to change)
  • 📈Data science
    • Time series
Powered by GitBook
On this page

Was this helpful?

  1. Programming languages
  2. Java

Features track

PreviousJVM internalsNextJava 8

Last updated 1 year ago

Was this helpful?

Java 19:

Java 19:

  • Pattern Matching for switch supporting type patterns and guarded patterns (Preview 🔍)

    String formatted = switch (o) {
        case Integer i && i > 10 -> String.format("a large Integer %d", i);
        case Integer i -> String.format("a small Integer %d", i);
        case Long l    -> String.format("a Long %d", l);
        default        -> o.toString();
    };

    → Related:

  • Sealed Classes can restrict which other classes may extend them (Preview 🔍 in )

    public abstract sealed class Shape
        permits Circle, Rectangle {...}
    
    public final class Circle extends Shape {...} // OK
    public final class Rectangle extends Shape {...} // OK
    public final class Triangle extends Shape {...} // Compile error
    
    // No need for default case if all permitted types are covered
    double area = switch (shape) {
        case Circle c    -> Math.pow(c.radius(), 2) * Math.PI
        case Rectangle r -> r.a() * r.b()
    };
  • Record Classes, terse syntax to define immutable DTOs (Preview 🔍 in )

    record Point(int x, int y) { }
    
    var point = new Point(1, 2);
    point.x(); // returns 1
    point.y(); // returns 2

    → Related:

  • Pattern Matching for instanceof to eliminate the need for explicit casts after a type check (Preview 🔍 in )

    if (obj instanceof String s && s.length() > 5) {
        System.out.println("obj is a String with more than 5 characters: " + s.toUpperCase());
    }
  • Text Blocks (Preview 🔍 in )

    String html = """
                <html>
                    <body>
                        <p>Hello, world</p>
                    </body>
                </html>
                """;
  • Helpful NullPointerExceptions describing precisely which variable was null (Enabled with -XX:+ShowCodeDetailsInExceptionMessages in )

    a.b.c.i = 99;
    ---
    Exception in thread "main" java.lang.NullPointerException:
          Cannot read field "c" because "a.b" is null
  • Switch Expressions (Preview 🔍 in )

    int numLetters = switch (day) {
        case MONDAY, FRIDAY, SUNDAY -> 6;
        case TUESDAY                -> 7;
        default      -> {
          String s = day.toString();
          int result = s.length();
          yield result;
        }
    };
  • Introduction of var to make local variable declarations less ceremonious (Without lambda support in )

    var greeting = "Hello World!";
  • Opt-in and backwards-compatible Module System to avoid ClassDefNotFoundErrors at runtime and create internal APIs (Project Jigsaw)

    module hu.advancedweb.helloworld {
        requires hu.advancedweb.somedependency;
        exports hu.advancedweb.hello
    }
  • Private methods in interfaces (Milling Project Coin)

  • Diamond operator for anonymous inner classes (Milling Project Coin)

  • Try-with-resources allows effectively final variables (Milling Project Coin)

  • @SafeVargs on private instance methods (Milling Project Coin)

  • No deprecation warnings on import statements

Details

JEP 425 has three goals:

  • Enable server applications written in the simple thread-per-request style to scale with near-optimal hardware utilization

  • Enable existing code that uses the java.lang.Thread API to adopt virtual threads with minimal change

  • Enable easy troubleshooting, debugging, and profiling of virtual threads with existing JDK tools

Non-Goals

  • It is not a goal to remove the traditional implementation of threads, or to silently migrate existing applications to use virtual threads.

  • It is not a goal to change the basic concurrency model of Java.

Before virtual threads, each thread in a Java application is mapped directly to an operating system (OS) thread. This makes sense because the JVM does not need to be concerned with things like direct scheduling and context switching of the threads. However, it does become an issue for certain common types of applications. Take the example of a web server that handles connections that do nothing for long periods while a user decides what to do next. In this case, the Java thread blocks, but this also prevents the OS thread from doing anything. When the number of simultaneous connections grows to a very high number, we can exhaust the available OS threads, even though many of these threads are doing nothing.

Virtual threads solve this by having multiple Java threads map to a single OS thread. As a result, OS threads can now be used efficiently to service Java threads that need to do work, and an application can support literally millions of simultaneous connections. (This is just one example of how virtual threads can be used; there are plenty of others).

In current implementation each Java thread is a wrapper around OS thread. Instead of handling a request on one thread from start to finish, request-handling code returns its thread to a pool when it waits for an I/O operation to complete so that the thread can service other requests. This fine-grained sharing of threads — in which code holds on to a thread only when it performs calculations, not when it waits for I/O — allows a high number of concurrent operations without consuming a high number of threads. While it removes the limitation on throughput imposed by the scarcity of OS threads, it comes at a high price: It requires what is known as an asynchronous programming style

Application code in the thread-per-request style can run in a virtual thread for the entire duration of a request, but the virtual thread consumes an OS thread only while it performs calculations on the CPU. The result is the same scalability as the asynchronous style, except it is achieved transparently: When code running in a virtual thread calls a blocking I/O operation in the java.* API, the runtime performs a non-blocking OS call and automatically suspends the virtual thread until it can be resumed later.

Virtual threads are not faster threads — they do not run code any faster than platform threads. They exist to provide scale (higher throughput), not speed (lower latency).

Virtual threads can significantly improve application throughput when

  • The number of concurrent tasks is high (more than a few thousand), and

  • The workload is not CPU-bound, since having many more threads than processor cores cannot improve throughput in that case.

Threads, which are implemented as OS threads, the JDK relies on the scheduler in the OS. By contrast, for virtual threads, the JDK has its own scheduler. Rather than assigning virtual threads to processors directly, the JDK's scheduler assigns virtual threads to platform threads (this is the M:N scheduling of virtual threads mentioned earlier). The platform threads are then scheduled by the OS as usual.

It is not a goal to offer a new data parallelism construct in either the Java language or the Java libraries. The remains the preferred way to process large data sets in parallel.

🟢
👍
⭐
JEP 425: Virtual Threads (preview)
overview article about virtual threads
JEP 428: Structured concurrency (incubator)
Awesome feature track for all versions with examples
JDK 18
JDK 17
Inside Java - Episode 17 “Pattern Matching for switch” with Gavin Bierman
JDK 17
JDK 16
JDK 15
JDK 16
JDK 15
JDK 14
Inside Java - Episode 14 “Records Serialization” with Julia Boes and Chris Hegarty
JDK 16
JDK 15
JDK 14
JDK 15
JDK 14
JDK 13
JDK 15
JDK 14
JDK 14
JDK 12
JDK 13
JDK 11
JDK 10
JDK 9
JDK 9
JDK 9
JDK 9
JDK 9
JDK 9
Stream API