如何通过多个线程编写CSV文件而线程之间没有任何中断?

时间:2017-08-28 14:59:44

标签: java csv

我注意到当我尝试通过多个线程同时写入CSV文件时,有一些数据丢失或数据合并。那么我怎么能避免这个丢失或错误的数据?

1 个答案:

答案 0 :(得分:1)

我的建议是在线程外部创建一个编写器处理程序并同步println / write方法,如下所示:

package snippet;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MultiThreadWriter implements Runnable {
    private SyncWriter sw;

    public MultiThreadWriter(SyncWriter sw) {
        this.sw = sw;
    }

    public void run() {
        // ...
        sw.println("whatever");
        // ....
    }

    public static void main(String[] args) {
        SyncWriter writer = null;
        try {
            writer = new SyncWriter("foo.csv");
            ExecutorService pool = Executors.newFixedThreadPool(10);
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.submit(new MultiThreadWriter(writer));
            pool.shutdown();
            while (pool.awaitTermination(1000, TimeUnit.MILLISECONDS)) {
                ;
            }
            writer.close();
        } catch (Exception e) {
            // ..
        }
    }
}

class SyncWriter {
    private PrintWriter pw;

    public SyncWriter(String path) throws FileNotFoundException {
        pw = new PrintWriter(path);
    }

    public void close() {
        pw.close();
    }

    public synchronized void println(String x) {
        pw.println(x);
    }
}