Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why FileChannel in Java is not non-blocking?

Tags:

I wanted to write a program which writes to multiple files simultaneously; thought it will be possible with one thread by using non-blocking mode. But FileChannel does not support non-blocking mode. Does anybody know why?

like image 350
user467158 Avatar asked Oct 17 '10 20:10

user467158


People also ask

Does Java have non blocking IO?

Java NIO is an asynchronous IO or non-blocking IO. For instance, a thread needs some data from the buffer. While the channel reads data into the buffer, the thread can do something else. Once data is read into the buffer, the thread can then continue processing it.

What is blocking and non blocking in Java?

Java IO's various streams are blocking. It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written. Non blocking I/O. Non blocking IO does not wait for the data to be read or write before returning.


1 Answers

UNIX does not support non-blocking I/O for files, see Non-blocking I/O with regular files. As Java should (at least try to) provide the same behaviour on all platforms, the FileChannel does not implement SelectableChannel.

However Java 7 will include a new AsynchronousFileChannel class that supports asynchronous file I/O, which is a different mechanism to non-blocking I/O.

In general only sockets and pipes truly support non-blocking I/O via select() mechanism.

like image 76
Stephen C Avatar answered Oct 31 '22 13:10

Stephen C