Asynchronous File I/O via POSIX AIO or Windows Overlapped IO in Java
Asked Answered
C

1

6

System.IO.File in .NET and .NET Core has a family of Read...Async() methods, all of which return either Task<byte[]> or Task<string> (Task<T> is the .NET's equivalent of Java's Future<T>).

This looks largely equivalent to AsynchronousFileChannel APIs (which either consume a CompletionHandler or return a Future), with one major difference.

  • AsynchronousFileChannel uses a managed background thread to perform asynchronous I/O (the thread may be provided either by the default thread pool (sun.nio.ch.ThreadPool) or by the ExecutorService explicitly specified during channel creation).
  • FileStream implementation in .NET, on the other hand, passes FileOptions.Asynchronous flag to the underlying operating system (see also Synchronous and Asynchronous I/O), doesn't spawn any managed background threads and uses what is called an Overlapped I/O.

Questions:

  • Is there any (existing or planned) File I/O API in Java which would use Overlapped I/O on Windows and POSIX AIO on Unices? Update: Windows-specific Java runtime features sun.nio.ch.WindowsAsynchronousFileChannelImpl which is exactly an abstraction layer on top of Overlapped I/O.
  • Are there any plans to provide java.nio.channels.SelectableChannel implementations for File I/O? If no, what are the technical limitations?
Chane answered 16/1, 2019 at 9:17 Comment(0)
A
2

It is not really possible. The Whole IO API would have to be re-implemented. NIO means non blocking I/O it is not the same as Asynchronous I/O. Non blocking is implemented in JAVA and long story short that means the OS has no ability to notify runtime that operation is completed. Isned java uses select() or poll() system calls to check if data is available.

I could talk about it but stollen picture is worth 100 words:

enter image description here

That is why in JAVA the separate thread is required to constantly call check,check,check,check .....

I don't know .NET platform but if what you posted is correct it utilizing asynchronous I/O so the last column. But I don't trust anything that comes from Microsoft.

Hope it answers your question. Also here I a additional reading material: https://mcmap.net/q/74175/-asynchronous-and-non-blocking-calls-also-between-blocking-and-synchronous

Audrieaudris answered 16/1, 2019 at 13:53 Comment(2)
Okay, but how about epoll on Linux, available since 2.6, as well as kernel-accelerated AIO, O_DIRECT and friends? How about kqueue on Mac OS X and the BSDs? Next, I/O Completion Ports (IOCP) are available not only on Windows, but also on AIX and Solaris 10+ (though the interfaces are of course different). Finally, why not use the POSIX AIO which may be not that advanced as any of the above APIs but much more common?Chane
@Bass, The interfaces you listed work well with network I/O, for file I/O epoll, for example, always returns that fd is ready, so it's not very efficient. But, things have changed a bit now that the io_uring interface is available on linux which allows you to implement truly asynchronous file I/O. Read more at io_uring File I/O implementation with io_uring - jasyncfioPrint

© 2022 - 2024 — McMap. All rights reserved.