简体   繁体   English

为什么Java中的FileChannel不是非阻塞的?

[英]Why FileChannel in Java is not non-blocking?

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. 但FileChannel不支持非阻塞模式。 Does anybody know why? 有人知道为什么吗?

UNIX does not support non-blocking I/O for files, see Non-blocking I/O with regular files . UNIX不支持文件的非阻塞I / O,请参阅使用常规文件的非阻塞I / O. As Java should (at least try to) provide the same behaviour on all platforms, the FileChannel does not implement SelectableChannel . 由于Java应该(至少尝试)在所有平台上提供相同的行为,因此FileChannel不会实现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. 但是Java 7将包含一个新的AsynchronousFileChannel类,它支持异步文件I / O,这是一种与非阻塞I / O不同的机制。

In general only sockets and pipes truly support non-blocking I/O via select() mechanism. 通常,只有套接字和管道通过select()机制真正支持非阻塞I / O.

简而言之,大多数操作系统不会将常规文件视为可以阻止的内容 - 因此它们不允许您将它们显式设置为非阻塞状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM