本文介绍了Java NIO Pipe vs BlockingQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现只有一个NIO工具,Java NIO Pipe,用于在线程之间传递数据。使用此机制是否优于通过队列传递的更传统的消息,例如ArrayBlockingQueue?

I just discovered that just has an NIO facility, Java NIO Pipe that's designed for passing data between threads. Is there any advantage of using this mechanism over the more conventional message passing over a queue, such as an ArrayBlockingQueue?

推荐答案

通常,为另一个线程传递数据的最简单方法是使用ExecutorService。这将包装队列和线程池(可以有一个线程)

Usually the simplest way to pass data for another thread to process is to use an ExecutorService. This wraps up both a queue and a thread pool (can have one thread)

当您拥有支持NIO通道的库时,可以使用管道。如果你想在线程之间传递数据的ByteBuffers也很有用。

You can use a Pipe when you have a library which supports NIO channels. It is also useful if you want to pass ByteBuffers of data between threads.

否则使用ArrayBlockingQueue通常很简单/更快。

Otherwise its usually simple/faster to use a ArrayBlockingQueue.

如果你想在线程之间更快地交换数据,我建议你看一下但它不像ArrayBlockingQueue那样通用。

If you want a faster way to exchange data between threads I suggest you look at the Exchanger however it is not as general purpose as an ArrayBlockingQueue.

这篇关于Java NIO Pipe vs BlockingQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:31