打开命名管道时 FileInputStream 会阻塞构造函数
使用 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
Using Fedora 12: mkfifo pipe
creates a pipe.
When I use the following code to try to open an input stream against the named pipe it blocks on the FileInputStream constructor unless I create a writer to the pipe, such as opening another terminal and running:
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();
}
}
Output:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从评论中复制:
这似乎是正确的行为。你想做什么? – Banthar 6 月 20 日 7:42
是的,我认为这个帖子可能回答了我的问题:stackoverflow.com/questions/2246862/… - 我试图从管道中读取数据,我期望流打开并在 read() 上阻塞,而不是阻止打开流。 – 大卫·帕克斯 6 月 20 日 7:52
Copied from comments:
It seems like a correct behavior. What are you trying to do? – Banthar Jun 20 at 7:42
Yep, I think this thread might just have answered my question: stackoverflow.com/questions/2246862/… - I'm trying to read from the pipe, I was expecting the stream to open and block on read(), not block on opening the stream. – David Parks Jun 20 at 7:52