当打开一个命名管道时,FileInputStream构造函数会阻塞。

4
使用Fedora 12:mkfifo pipe 创建了一个命名管道。
当我使用以下代码尝试打开针对该命名管道的输入流时,除非我创建一个写入器向该管道写入数据,否则它会在FileInputStream构造函数处阻塞,例如在另一个终端窗口中运行:
tee pipe


public static void main(String[] args){
    try {
        File pipe = new File("/tmp/pipes_debugging/pipeToJava");
        System.out.println( pipe.canRead() );
        FileInputStream fis = new FileInputStream(pipe);
        System.out.println("exiting.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

输出:

true
<blocks - thread trace shown below>


Thread [main] (Suspended)   
    FileInputStream.open(String) line: not available [native method]    
    FileInputStream.<init>(File) line: 137  
    PipesDebugging.main(String[]) line: 12  

4
看起来这是正确的行为。你想要做什么? - Piotr Praszmo
是的,我认为这个线程可能已经回答了我的问题:https://dev59.com/7UvSa4cB1Zd3GeqPiOuy - 我正在尝试从管道中读取,我期望流会打开并在read()上阻塞,而不是在打开流时阻塞。 - David Parks
1个回答

2

从评论中复制:

这似乎是正确的行为。你想做什么?- Banthar Jun 20 at 7:42

是的,我认为这个线程可能已经回答了我的问题:stackoverflow.com/questions/2246862/… - 我试图从管道中读取,我原本期望流会打开并在read()上阻塞,而不是在打开流时阻塞。- David Parks Jun 20 at 7:52


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接