java 命名管道_Java中命名管道的并发读/写(在Windows上)
我正在嘗試使用命名管道在Windows上使用命名管道提供C#應用程序和Java應用程序之間的通信,使用v01ver在此問題中描述的方法:
How to open a Windows named pipe from Java?
我在Java方面遇到了一個問題,因為我有一個讀者線程經常在管道上等待輸入,當我嘗試從我的主線程寫入管道時,它會永遠被卡住.
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...
然后它會永遠等待.
如何在另一個線程中等待輸入時寫入命名管道?
總結
以上是生活随笔為你收集整理的java 命名管道_Java中命名管道的并发读/写(在Windows上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java的svn插件maver_ecli
- 下一篇: Java的后缀分类_JAVA根据文件后缀