File IO and the Swing Event Dispatch Thread

Alan Bateman Alan.Bateman at oracle.com
Tue Dec 1 14:41:44 UTC 2020


On 01/12/2020 13:39, Dávid Karnok wrote:
> :
>
> I've written Swing apps following the guidelines of doing IO on a 
> background thread and post back to the EDT. From memory, I encountered 
> a few uses of isDispatchThread: for example, if not on the EDT, post 
> the work to the EDT. Another rare example was to fail with an 
> exception if not on the EDT. Sometimes, the former was created to 
> avoid the exception from the latter.
Sergey Bylokhov has notes in JDK-8253546 [1] about the challenges with 
state and baggage such as thread groups and AppContext [1]. So no 
conclusion yet on whether it is feasible yet.


>
> As for the File IO, if I open several thousand files with 
> FileInputStream, each in its own virtual thread, and the reads go 
> slowly, can I possibly end up with thousands of OS threads? In other 
> terms, would it be better with virtual threads than doing the same 
> thing today (with JDK 15) and submitting to a newCachedThreadPool?
With the default scheduler the number of carrier threads is bounded. The 
current limit is 128 but that might be too low, it is configurable on 
the command line with the jdk.defaultScheduler.maxPoolSize property. So 
you shouldn't end up with thousands of threads even if there are many 
threads taking a long to open a file or reading it with a FileInputStream.

-Alan.

[1] https://bugs.openjdk.java.net/browse/JDK-8253546


More information about the loom-dev mailing list