使用 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
最佳答案
从评论中复制:
这似乎是一种正确的行为。你想做什么? – Banthar 6 月 20 日 7:42
是的,我想这个帖子可能刚刚回答了我的问题:stackoverflow.com/questions/2246862/… - 我正在尝试从管道中读取,我期望流打开并在 read() 上阻塞,而不是在打开流时阻塞。 – 大卫·帕克斯 6 月 20 日 7:52
关于java - 打开命名管道时 FileInputStream 在构造函数上阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6408099/