Java中命名管道的并发读/写(在Windows上) [英] Concurrent read/write of named pipe in Java (on windows)
问题描述
我正在尝试使用命名管道在Windows上的C#应用程序和Java应用程序之间提供通信,使用v01ver在此问题中描述的方法:如何从Java打开Windows命名管道?
I'm trying to provide communication between a C# app and a Java app on windows using named pipes with the method described by v01ver in this question: How to open a Windows named pipe from Java?
我'我遇到了一个问题,因为我有一个读取器线程不断等待管道上的输入,当我尝试从我的主线程写入管道时,它会永远卡住。
I'm running into a problem on the Java side because I have a reader thread constantly waiting for input on the pipe and when I try to write to the pipe from my main thread it gets stuck forever.
final RandomAccessFile pipe;
try {
pipe = new RandomAccessFile("\\\\.\\pipe\\mypipe", "rw");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
return;
}
Thread readerThread = new Thread(new Runnable() {
@Override
public void run() {
String line = null;
try {
while (null != (line = pipe.readLine())) {
System.out.println(line);
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
readerThread.start();
try { Thread.sleep(500); } catch (InterruptedException e) {}
try {
System.out.println("Writing a message...");
pipe.write("Hello there.\n".getBytes());
System.out.println("Finished.");
}
catch (IOException ex) {
ex.printStackTrace();
}
输出为:
Writing a message...
然后它会永远等待。
and then it waits forever.
如何在另一个线程中等待输入时写入命名管道?
How can I write to a named pipe while waiting for input in another thread?
推荐答案
这是管道的预期行为。它应该挂起,直到其他进程连接到管道并读取它。
This is expected behaviour of pipes. It is supposed to hang untill other process connects to the pipe and reads it.
这篇关于Java中命名管道的并发读/写(在Windows上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!