JDK-8340830 : Console.readLine() and Console.printf() are mutually blocking
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 23
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2024-09-24
  • Updated: 2025-04-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
Following example demonstrates how IO::readln and IO::println are mutually blocking and how IO.println behaved differently than System.out.println in multithreaded code.


```
static void doSomething() {
    try {
       Thread.sleep(1000);
   } catch (InterruptedException ignore) {}
}
 
void main() {
   Thread.ofVirtual().start(() -> {
        while (true) {
            doSomething();
           System.out.println("I do something until you press Enter");
       }
   });
   Thread.ofVirtual().start(() -> {
        while (true) {
            doSomething();
            println("I'm blocked until you press Enter");
       }
   });
    readln("");
}
```
Comments
With the JEP 512 JDK-8344699), this will no longer an issue in IO class, as it uses System.in/out directly. However it still is an issue with Console methods. Repurposing this to address the Console issue, i.e., ``` static void doSomething() { try { Thread.sleep(1000); } catch (InterruptedException ignore) {} } void main() { Thread.ofVirtual().start(() -> { while (true) { doSomething(); System.out.println("I do something until you press Enter"); } }); Thread.ofVirtual().start(() -> { while (true) { doSomething(); System.console().printf("I'm blocked until you press Enter"); } }); System.console().readLine(""); } ```
30-04-2025

`readln("")` at the last line has both read and write lock and waiting for the user to press Enter. The `println()` call on the virtual thread is waiting forever to acquire the write lock. Thus nothing is printed until the Enter when `readln()` releases the write lock.
24-09-2024