【问题标题】:Java File Locking with 2 Processess带有 2 个进程的 Java 文件锁定
【发布时间】:2011-06-30 11:41:11
【问题描述】:

首先,我有两个同时运行且相互支持的进程。一个进程读取一个简单的平面文件,其中包含由时间戳分隔的数据快照。这个应用程序只是打开这个文件(没有文件锁定),读取一个快照并将其放入另一个名为topology.netviz 的文件中(有文件锁定)。第二个应用程序读取topology.netziv(带有文件锁定)并将数据传输到一个临时文件中,以减少其他进程之间持有锁定的编程延迟。

我的问题很简单: 当我在第二个过程中将数据传输到临时文件时,会传输奇怪的字符/损坏的数据。我在下面提供了一些代码,让你们了解可能存在的问题。

流程一:

try {
    // Determine if File Exists
    topologyFile = new File(Settings.NODE_TOPOLOGY_PATH);
    if (!topologyFile.exists())
        topologyFile.createNewFile();

    // FileChannel Gives the Ability to Create a File Lock
    FileChannel channel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = channel.lock();

    // Delete Files Contents
    channel.truncate(0);

    // Convert 'data' into ByteBuffer
    ByteBuffer buffer = ByteBuffer.wrap(data);

    // Write 'buffer' to 'channel'
    channel.write(buffer, 0);

    // Release Lock
    lock.release();

    // Close Channel
    channel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.
        out.println("Topology Thread: FileChannel; File is not Writeable");
}

流程2:

try {
    // Determine if File Exists
    topologyFileTemp = new File("tmp/topology.dat");
    if (topologyFileTemp.exists())
        topologyFileTemp.delete();  // Should Never Occur Unless Program Crashes

    // Recreate 'topologyFileTemp'
    topologyFileTemp = new File("tmp/topology.dat");
    topologyFileTemp.createNewFile();

    // Determine if File Exists
    topologyFile = new File("topology.netviz");
    if (!topologyFile.exists())
        topologyFile.createNewFile();   // Should Never Occur

    // Initialize Data Container from 'topology.netviz' in the Form of Bytes
    ByteBuffer topologyData =
        ByteBuffer.allocate((int)topologyFile.length());

    // FileChannel Gives the Ability to Create a File Lock for 'topology.netviz'
    FileChannel rChannel =
        new RandomAccessFile(topologyFile, "rwd").getChannel();

    // Use the FileChannel to Create a Lock on the 'distance.dat' (Blocking Method)
    FileLock lock = rChannel.lock();

    // Grab Data from 'topology.netviz'
    rChannel.read(topologyData);

    // Release Lock
    lock.release();

    // Close Channel
    rChannel.close();

    // FileChannel Gives the Ability to Create a File Lock for 'tmp/topology.dat'
    FileChannel wChannel =
        new RandomAccessFile(topologyFileTemp, "rw").getChannel();

    // Reset Buffers Position
    topologyData.position(0);

    // Write 'topologyData' to 'tmp/topology.dat'
    wChannel.write(topologyData);

    // Close the file
    wChannel.close();
}

catch(IOException error)
{
    System.out.println("Topology Thread: FileChannel; I/O Error Occured");
}

catch(NonWritableChannelException error)
{
    System.out.
        println("Topology Thread: FileChannel; File is not Writeable");
}

【问题讨论】:

    标签: java file synchronization filechannel


    【解决方案1】:

    一个潜在的问题是进程#1 在关闭通道之前释放了锁。由于关闭通道会刷新未完成的写入,这可能意味着数据在锁被释放后正在写入,这可能会导致损坏。

    另一种可能性是字节在写入之前已损坏......或者写入器和读取器使用的格式不匹配。

    【讨论】:

    • 我切换了两行代码,收到了IOExceptions。进程 1 似乎正确地吐出数据。
    • @Mitchell - 有哪些例外情况?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多