简体   繁体   English

Java中非阻塞写入的顺序

[英]Order of non-blocking writes in Java

Java's NIO provides lots of useful features. Java的NIO提供了许多有用的功能。 One of them is the possibility of non blocking write operations on channels. 其中之一是可以不阻塞通道上的写操作。 This means that the write method will not block until the write is done. 这意味着在完成写入之前,write方法将不会阻塞。

But is it still provided that all writes are written in the same order as the writes are called even though the methods don't block? 但是,即使方法没有阻塞,是否仍然提供所有写入的顺序与调用写入的顺序相同?

The scenario you're considering is unlikely, considering the fact that the NIO api does not allow a program to write to the same channel, within the same thread, before a previous write completes. 考虑到NIO api不允许程序在上一次写入完成之前将程序写入同一线程内的同一通道这一事实,因此您正在考虑的场景不太可能。 A WritePendingException is thrown. 引发WritePendingException From the docs: 从文档:

Unchecked exception thrown when an attempt is made to write to an asynchronous socket channel and a previous write has not completed. 尝试写入异步套接字通道并且先前的写入未完成时,引发未经检查的异常。

The way the Async API is built, the most you can do is either attempt to write from a different thread, or wait until the first write is complete before initiating another 异步API的构建方式,您最能做的就是尝试从其他线程进行写入,或者等到第一次写入完成后再发起另一个

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

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