JEP 477: println blocking

Alan Bateman alan.bateman at oracle.com
Mon Sep 23 06:06:51 UTC 2024


On 23/09/2024 06:42, Adam Sotona wrote:
>
> Hi,
>
> I’m updating my classroom examples to run on JDK 23 and with 
> IO.println I experience different behavior than with 
> System.out.println. Following example explains the issue.
>
> Thank you,
>
> Adam
>
> staticvoid*/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/("");
>
> }
>
IO uses Console which has its own read + write locks to coordinate input 
and output from concurrent threads. The readln is both output (for the 
prompt) and input and it seems to hold both locks when waiting for 
input.   System.out has its own route to print the output, doesn't 
coordinate with Console, so isn't locked out by readln(prompt). It's 
always been possible to mix Console and System.in/out, but maybe time to 
re-examine to avoid puzzlers when mixing uses of the new textual I/O 
methods with code copied from a book or online resource that uses 
System.out.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240923/c3a0e686/attachment.htm>


More information about the amber-dev mailing list