windows下一个进程写文件ok,命令行下同时运行两个进程,总会丢失几条!!
代码如下:import java.io.BufferedWriter;
import java.io.PrintWriter;import java.io.FileWriter;public class WriteFile {
public static void main(String[] args) {
long times = 3000;
for (int i = 0; i < times; i++) {
//write1(Integer.toString(i+1));
write2(Integer.toString(i+1));
}
System.out.println("finished!");
} private static void write1(String content) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("d:\\1.txt", true);
bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.flush();
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}

private static synchronized void write2(String content) {
FileWriter fw = null;
PrintWriter pw = null;
try {
fw = new FileWriter("d:\\2.txt", true);
pw = new PrintWriter(fw);
pw.println(content);
pw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}

解决方案 »

  1.   

    丢失应该是正常的,调用write1和write2方法应该是一样的
    这里不存在多线程问题,同步不起作用。两个不同的进程是独立的,各自运行于一个独立的jvm中。两个进程同时写一个文件,结果应该依赖于操作系统的文件处理方式。
      

  2.   

    synchronized本来就是对线程进行同步化,对进程不起作用.
    java下面好像没有对进程同步化的控制.
    要么你对文件进行加锁,每个线程写的时候加锁,写完解锁.
      

  3.   

    几位没重现问题的可以加大并发或者循环次数,问题肯定是存在的。我的windows测试机是双核的,不知有无影响。
    通过文件通道和文件锁方式解决,代码如下。谢谢各位!import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.RandomAccessFile;import java.io.FileWriter;
    import java.nio.channels.FileChannel;
    import java.nio.channels.FileLock;public class WriteFile {
    public static void main(String[] args) throws IOException {
    long times = 3000;
    for (int i = 0; i < times; i++) {
    // write1(Integer.toString(i+1));
    // write2(Integer.toString(i + 1));
    write3(Integer.toString(i + 1));
    }
    System.out.println("finished!");
    } private static void write1(String content) {
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
    fw = new FileWriter("d:\\1.txt", true);
    bw = new BufferedWriter(fw);
    bw.write(content);
    bw.newLine();
    bw.flush();
    bw.close();
    fw.close();
    } catch (Exception e) {
    // TODO: handle exception
    }
    } private static synchronized void write2(String content) {
    FileWriter fw = null;
    PrintWriter pw = null;
    try {
    fw = new FileWriter("d:\\2.txt", true);
    pw = new PrintWriter(fw);
    pw.println(content);
    pw.close();
    fw.close();
    } catch (Exception e) {
    // TODO: handle exception
    }
    } private static void write3(String content) throws IOException {
    String fileName = "d:\\3.txt";
    RandomAccessFile raFile = null;
    FileChannel channel = null;
    FileLock fileLock = null;
    try {
    raFile = new RandomAccessFile(fileName, "rw");
    channel = raFile.getChannel();
    fileLock = channel.lock();
    raFile.seek(raFile.length());
    raFile.write((content + System.getProperty("line.separator"))
    .getBytes());
    } catch (Exception e) {
    System.out.println(e.getMessage());
    } finally {
    if (null != fileLock) {
    fileLock.release();
    }
    if (null != channel) {
    channel.close();
    }
    }
    }
    }