Java NIO Pipe vs BlockingQueue

后端 未结 4 2022
广开言路
广开言路 2021-02-04 12:04

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 co

4条回答
  •  清歌不尽
    2021-02-04 12:31

    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)

    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.

    Otherwise its usually simple/faster to use a 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.

    The Exchanger and GC-less Java

提交回复
热议问题