RFR: 8340830: Console.readLine() and Console.printf() are mutually blocking [v5]

Jaikiran Pai jpai at openjdk.org
Tue Feb 3 10:19:41 UTC 2026


On Mon, 2 Feb 2026 22:05:03 GMT, Naoto Sato <naoto at openjdk.org> wrote:

>> Fixing an issue in Console where write is blocked if other thread is waiting to read, which is caused by unnecessary read/write locks. Removing those would solve the problem, as the read/write synchronization is performed at the StreamEn/Decoder level. One unrelated change is to refactor double-checked locking with LazyConstant.
>
> Naoto Sato has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Added @requires condition

test/jdk/java/io/Console/ReadWriteBlockingTest.java line 76:

> 74:             try {
> 75:                 // give some time for main thread to invoke readLine()
> 76:                 Thread.sleep(1000);

Instead of `Thread.sleep(...)` would it be better to do something like (untested code):


var con = System.console();
CountDownLatch latch = new CountDownLatch(2);

Thread.ofVirtual().start(() -> {
    try {
        latch.countDown(); // announce our arrival
        try {
            latch.await(); // wait for other thread to arrive
        } catch (InterruptedException _) {
            // ignore
        }
        con.printf("printf() invoked");
    } catch (Throwable t) {
        t.printStackTrace();
        throw t;
    }
});

latch.countDown(); // announce our arrival
latch.await(); // wait for other thread to arrive
con.readLine("");

That way the chances to the read and write threads racing, increases.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/29493#discussion_r2758313096


More information about the core-libs-dev mailing list