简体   繁体   English

Java同步外部文件读写

[英]Java sync external files read write

I would like to create a pipe for read/write data between 2 JVM process (not the same process). 我想为2个JVM进程(不是同一进程)之间的读/写数据创建管道。 Im using a FIFO pipe in Unix to share the data. 我在Unix中使用FIFO管道共享数据。

I did a Reader: 我做了一个读者:

/** Read a named pipe file */
public class PipeReader {

private String path;

public PipeReader (String path) {
    this.path = path;
}

public String readpipe () throws IOException { 
    String res = null;
    RandomAccessFile pipe = null;

    try {
        // Connect to the named pipe
        pipe = new RandomAccessFile (path, "r");

        // Read response from pipe
        while (true) {
           res = pipe.readLine();
           System.out.println("Read message:" + res);   
        }    
    } catch (Exception e) { 
        e.printStackTrace();
    } finally {
        pipe.close();
    }
    return res;
}

And a Writer using FileLock: 还有一个使用FileLock的Writer:

public class PipeWriter {

private String path;

public PipeWriter (String path) {
    this.path = path;
}

public String writepipe (String mm) throws IOException { 
    String res = null;
    RandomAccessFile pipe = null;
    try {
        // Connect to the named pipe
        pipe = new RandomAccessFile (path, "rw");
        FileChannel channel = pipe.getChannel();
        int i = 0;
        while (i<5) {   
            // Write request to the pipe
            FileLock lock = channel.lock();
            pipe.write(mm.getBytes());
            lock.release();
            System.out.println("PipeWriten" + mm);
            Thread.sleep(3000);
            i++;
        }



        // do something with res
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Close the pipe
        pipe.close();
    }


    return res;
}

The first time pipe.readLine() block this thread and wait until PipeWriter write a line in the pipe and release the lock, when the writer finishes the reader read it. 第一次pipe.readLine()阻塞此线程,并等到PipeWriter在管道中写一行并释放锁,当写入器完成读取器读取时,才释放该锁。 This is the behavior until PipeWriter finishes (after the 5 writings). 这是直到PipeWriter完成(在5篇文章之后)为止的行为。 After that, pipe.readLine() don't block the thread and continues looping the reading so I get "Read message: null" a lot of times. 之后,pipe.readLine()不会阻塞线程并继续循环读取,因此很多时候我都会收到“读取消息:空”消息。 How could I fix it? 我该如何解决? I think I am missing something in the Reader. 我认为我在阅读器中缺少某些内容。 Are there any method to synchronize a file shared by 2 process, not using FileLock? 是否有任何方法可以同步2个进程共享的文件,而不使用FileLock? (something like semaphores but for processes, not threads). (类似于信号灯,但用于进程,而不是线程)。

Thanks in advance. 提前致谢。

A pipe's not like a file (which is good, since you can't wait for a file) it's a connection between the reader and the writer. 管道不像文件(这很好,因为您不能等待文件),它是读取器和写入器之间的连接。 It's more like a telephone. 更像是电话。 When the other side hangs up, you have to hang up too. 当另一侧挂断电话时,您也必须挂断电话。 Then you can wait for the next call. 然后,您可以等待下一个呼叫。

When read gets zero, that means the connection is now closed, and you should close the pipe. read变为零时,这意味着连接现在已关闭,您应该关闭管道。 If you want to wait for a new connection, re-open the pipe. 如果要等待新的连接,请重新打开管道。

This is what I get: In the beginning, Reader is blocked waiting in pipe.readLine(). 这就是我得到的:开始时,Reader在pipe.readLine()中被阻止等待。 When Writer write a line and release the locker it read it. 当Writer写一行并释放储物柜时,它将读取它。 But then the Writer finishes, the Reader don't wait in readLine() anymore, and read null all the time. 但是然后Writer完成,Reader不再在readLine()中等待,并且一直读取null。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM