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?
Bass
  • 4,977
  • 2
  • 36
  • 82

1 Answers1

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://stackoverflow.com/a/2625565/8951886

piotr szybicki
  • 1,532
  • 1
  • 11
  • 12
  • 1
    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? – Bass Jan 16 '19 at 20:42
  • 1
    @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](https://kernel.dk/io_uring.pdf) File I/O implementation with io_uring - [jasyncfio](https://github.com/ikorennoy/jasyncfio) – ikorennoy May 14 '22 at 09:44