当打开一个FIFO(命名为管道)进行读取时,异步文件通道.open挂起。

问题描述 投票:0回答:1

在Linux上,我在shell中使用以下方法创建了一个命名的管道 mkfifo /tmp/test.

我想用它来进行异步读取,所以作为第一步,我用 AsynchronousFileChannel:

java.nio.channels.AsynchronousFileChannel.open(
    java.nio.file.Paths.get("/tmp/test"),
    java.nio.file.StandardOpenOption.READ)
)

但是调用挂起。

"main" #1 prio=5 os_prio=0 cpu=1830.11ms elapsed=686.74s tid=0x00007f444c016800 nid=0x6ae8 runnable  [0x00007f44527c2000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.fs.UnixNativeDispatcher.open0([email protected]/Native Method)
    at sun.nio.fs.UnixNativeDispatcher.open([email protected]/UnixNativeDispatcher.java:71)
    at sun.nio.fs.UnixChannelFactory.open([email protected]/UnixChannelFactory.java:267)
    at sun.nio.fs.UnixChannelFactory.newAsynchronousFileChannel([email protected]/UnixChannelFactory.java:180)
    at sun.nio.fs.UnixFileSystemProvider.newAsynchronousFileChannel([email protected]/UnixFileSystemProvider.java:199)
    at java.nio.channels.AsynchronousFileChannel.open([email protected]/AsynchronousFileChannel.java:253)
    at java.nio.channels.AsynchronousFileChannel.open([email protected]/AsynchronousFileChannel.java:311)

在Java中是否有可能为异步读或写打开一个命名管道?

java nio named-pipes
1个回答
3
投票

man open 说。

   FIFOs
       Opening  the read or write end of a FIFO blocks until the other end is also opened (by an‐
       other process or thread).  See fifo(7) for further details.

因此,当打开一个命名的管道时,当另一端没有其他东西连接时,Java调用的表现是预期的。看来没有办法通过 O_NONBLOCK 来获得一个非阻塞的理想行为。open.


1
投票

fifo上的读取总是会被阻塞,直到有东西要读取。我怀疑这也适用于打开异步通道的时候。你有没有试过向fifo写东西?

© www.soinside.com 2019 - 2024. All rights reserved.