Java中的异步IO?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/592303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:37:31  来源:igfitidea点击:

Asynchronous IO in Java?

javanetworkingsocketsasynchronous

提问by thr

What options for async io (socket-based) are there in java other then java.nio? Also does java.nio use threads in the backround (as I think .NET's async-socket-library does, maybe it's been changed) or is it "true" async io using a proper select call?

除了 java.nio,java 中有哪些异步 io(基于套接字)选项?java.nio 是否也在后台使用线程(正如我认为 .NET 的 async-socket-library 所做的那样,也许它已被更改)还是使用正确的选择调用“真正的”异步 io?

采纳答案by JLR

Java's NIO package (as of Java6), provides support for non-blocking I/Oonly, via Selectors. Java7 is hopefully going to ship with NIO.2, which includes asynchronous I/O support. Today, your best bet is to make use of a framework. ARMistice mentioned Mina. Here are some others.

Java 的 NIO 包(从 Java6 开始),仅通过Selector提供对非阻塞 I/O的支持。Java7 有望与 NIO.2 一起发布,其中包括异步 I/O 支持。今天,最好的办法是使用框架。ARMistice 提到了米娜。这里有一些其他的。

  1. Grizzly. This is the I/O core for Sun's GlassFishserver. Grizzly provides a facility for doing asynchronous reads/writes (via a queue model). It supports TCP and UDP alike. I've used Grizzly in a couple of projects. There are things I like and dislike about the framework, but to detail this is really another topic. I will say that it's quite easy to get something up and running and Grizzly does a lot of the heavy lifting for you.
  2. Netty. This project comes from one of the original authors on the Mina project. I haven't used this one so I don't know about about its support for asynchronous I/O. You should take a look.
  1. 灰熊。这是 Sun 的GlassFish服务器的 I/O 核心。Grizzly 提供了进行异步读/写的工具(通过队列模型)。它支持 TCP 和 UDP。我在几个项目中使用了 Grizzly。这个框架有我喜欢和不喜欢的地方,但要详细说明这确实是另一个话题。我会说启动和运行一些东西很容易,Grizzly 为你做了很多繁重的工作。
  2. 内蒂。该项目来自 Mina 项目的原始作者之一。我没有使用过这个,所以我不知道它对异步 I/O 的支持。你应该看看。

Now, with regard to your question about threads, NIO Selectors do not use threads for non-blocking I/O. In JDK6 they use select()under Windows and the epoll facility on newer Linux kernels. For asynchronous I/O, threading details depend on the framework.

现在,关于您关于线程的问题,NIO Selectors 不使用线程进行非阻塞 I/O。在 JDK6 中,它们在 Windows 下使用select()和在较新的 Linux 内核上使用 epoll 工具。对于异步 I/O,线程细节取决于框架。

回答by Yuval Adam

java.niois just a package - a collection of "dumb" classes - by itself it does not employ any use of threads. When used properly, such as in the Reactor design pattern, you can achieve proper, fully-scalable, asynchronous I/O.

java.nio只是一个包——一个“哑”类的集合——它本身不使用任何线程。如果使用得当,例如在Reactor 设计模式中,您可以实现适当的、完全可扩展的、异步 I/O。

回答by Yuval Adam

If you are interested in using it for Network Stuff. A really good choice is:

如果您有兴趣将其用于 Network Stuff。一个非常好的选择是:

http://mina.apache.org/

http://mina.apache.org/

Have a look there its easy to use and very powerfull.

看看那里它易于使用且非常强大。

回答by Nuoji

Another suggestion in regards to libs would be Naga (http://naga.googlecode.com). It is a bit less like a framework and more like a library. It tries to look more like the ordinary java sockets, if that is your cup of tea. It's minimalistic compared to Grizzly, Mina and Netty.

关于库的另一个建议是 Naga ( http://naga.googlecode.com)。它有点不像一个框架,而更像是一个库。它试图看起来更像普通的 java 套接字,如果那是你的一杯茶的话。与 Grizzly、Mina 和 Netty 相比,它是简约的。

回答by Alan

To the original question, the implementation only consumes a thread per I/O operation in one case, AsynchronousFileChannel on Unix/Linux systems.

对于最初的问题,在 Unix/Linux 系统上的 AsynchronousFileChannel 一种情况下,该实现仅在每个 I/O 操作中消耗一个线程。

回答by Waldemar Wosiński

JAVA 7 arrived so new answer is NIO.2 with Future class. Example :

JAVA 7 到来了,所以新的答案是带有 Future 类的 NIO.2。例子 :

On server side:

在服务器端:

final AsynchronousServerSocketChannel serverSocket=
  AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("127.0.0.1", 2587)); // Listening on port 2587 for client connection
Future<AsynchronousSocketChannel> future= serverSocket.accept();
final AsynchronousSocketChannel clientSocket= future.get(); // now it's blocking, useful: future.isDone() and .isCancelled()

//Do whatever you want ..
InputStream stream = Channels.newInputStream(clientSocket) (...)

On client side:

在客户端:

AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open();
Future connected = localSocket.connect(ourServerSocketAddress);
// later: if(future.isDone())
connected.get();

//Send something
OutputStream os = Channels.newOutputStream(clientChannel );
os.write (...)

Update:If you can use actor model then AKKA TCP IOwould be even better.

更新:如果您可以使用 actor 模型,那么AKKA TCP IO会更好。