在Java中,在并行线程中写入文件的最佳方式是什么



我有一个程序,可以执行大量计算并经常将其报告到文件中。我知道频繁的写操作会大大降低程序的速度,所以为了避免这种情况,我希望有第二个线程专门用于写操作。

现在我正在用我写的这门课做这件事(不耐烦的人可以跳到问题的末尾):

public class ParallelWriter implements Runnable {
    private File file;
    private BlockingQueue<Item> q;
    private int indentation;
    public ParallelWriter( File f ){
        file = f;
        q = new LinkedBlockingQueue<Item>();
        indentation = 0;
    }
    public ParallelWriter append( CharSequence str ){
        try {
            CharSeqItem item = new CharSeqItem();
            item.content = str;
            item.type = ItemType.CHARSEQ;
            q.put(item);
            return this;
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }
    public ParallelWriter newLine(){
        try {
            Item item = new Item();
            item.type = ItemType.NEWLINE;
            q.put(item);
            return this;
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }
    public void setIndent(int indentation) {
        try{
            IndentCommand item = new IndentCommand();
            item.type = ItemType.INDENT;
            item.indent = indentation;
            q.put(item);
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }
    public void end(){
        try {
            Item item = new Item();
            item.type = ItemType.POISON;
            q.put(item);
        } catch (InterruptedException ex) {
            throw new RuntimeException( ex );
        }
    }
    public void run() {
        BufferedWriter out = null;
        Item item = null;
        try{
            out = new BufferedWriter( new FileWriter( file ) );
            while( (item = q.take()).type != ItemType.POISON ){
                switch( item.type ){
                    case NEWLINE:
                        out.newLine();
                        for( int i = 0; i < indentation; i++ )
                            out.append("   ");
                        break;
                    case INDENT:
                        indentation = ((IndentCommand)item).indent;
                        break;
                    case CHARSEQ:
                        out.append( ((CharSeqItem)item).content );
                }
            }
        } catch (InterruptedException ex){
            throw new RuntimeException( ex );
        } catch  (IOException ex) {
            throw new RuntimeException( ex );
        } finally {
            if( out != null ) try {
                out.close();
            } catch (IOException ex) {
                throw new RuntimeException( ex );
            }
        }
    }
    private enum ItemType {
        CHARSEQ, NEWLINE, INDENT, POISON;
    }
    private static class Item {
        ItemType type;
    }
    private static class CharSeqItem extends Item {
        CharSequence content;
    }
    private static class IndentCommand extends Item {
        int indent;
    }
}

然后我用它做:

ParallelWriter w = new ParallelWriter( myFile );
new Thread(w).start();
/// Lots of
w.append(" things ").newLine();
w.setIndent(2);
w.newLine().append(" more things ");
/// and finally
w.end();

虽然这非常有效,但我想知道:有更好的方法来实现这一点吗

您的基本方法看起来不错。我会将代码结构如下:

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.TimeUnit;
    
    public interface FileWriter {
        FileWriter append(CharSequence seq);
    
        FileWriter indent(int indent);
    
        void close();
    }
    
    class AsyncFileWriter implements FileWriter, Runnable {
        private final File file;
        private final Writer out;
        private final BlockingQueue<Item> queue = new LinkedBlockingQueue<Item>();
        private volatile boolean started = false;
        private volatile boolean stopped = false;
    
        public AsyncFileWriter(File file) throws IOException {
            this.file = file;
            this.out = new BufferedWriter(new java.io.FileWriter(file));
        }
    
        public FileWriter append(CharSequence seq) {
            if (!started) {
                throw new IllegalStateException("open() call expected before append()");
            }
            try {
                queue.put(new CharSeqItem(seq));
            } catch (InterruptedException ignored) {
            }
            return this;
        }
    
        public FileWriter indent(int indent) {
            if (!started) {
                throw new IllegalStateException("open() call expected before append()");
            }
            try {
                queue.put(new IndentItem(indent));
            } catch (InterruptedException ignored) {
            }
            return this;
        }
    
        public void open() {
            this.started = true;
            new Thread(this).start();
        }
    
        public void run() {
            while (!stopped) {
                try {
                    Item item = queue.poll(100, TimeUnit.MICROSECONDS);
                    if (item != null) {
                        try {
                            item.write(out);
                        } catch (IOException logme) {
                        }
                    }
                } catch (InterruptedException e) {
                }
            }
            try {
                out.close();
            } catch (IOException ignore) {
            }
        }
    
        public void close() {
            this.stopped = true;
        }
    
        private static interface Item {
            void write(Writer out) throws IOException;
        }
    
        private static class CharSeqItem implements Item {
            private final CharSequence sequence;
    
            public CharSeqItem(CharSequence sequence) {
                this.sequence = sequence;
            }
    
            public void write(Writer out) throws IOException {
                out.append(sequence);
            }
        }
    
        private static class IndentItem implements Item {
            private final int indent;
    
            public IndentItem(int indent) {
                this.indent = indent;
            }
    
            public void write(Writer out) throws IOException {
                for (int i = 0; i < indent; i++) {
                    out.append(" ");
                }
            }
        }
    }

如果您不想在单独的线程中编写(可能是在测试中?),您可以实现FileWriter,它在调用方线程的Writer上调用append

与单个使用者线程交换数据的一个好方法是使用交换器。

您可以使用StringBuilder或ByteBuffer作为与后台线程交换的缓冲区。产生的延迟可能在1微秒左右,不需要创建任何对象,使用BlockingQueue会更低。

从我认为值得在这里重复的例子来看。

class FillAndEmpty {
   Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
   DataBuffer initialEmptyBuffer = ... a made-up type
   DataBuffer initialFullBuffer = ...
   class FillingLoop implements Runnable {
     public void run() {
       DataBuffer currentBuffer = initialEmptyBuffer;
       try {
         while (currentBuffer != null) {
           addToBuffer(currentBuffer);
           if (currentBuffer.isFull())
             currentBuffer = exchanger.exchange(currentBuffer);
         }
       } catch (InterruptedException ex) { ... handle ... }
     }
   }
   class EmptyingLoop implements Runnable {
     public void run() {
       DataBuffer currentBuffer = initialFullBuffer;
       try {
         while (currentBuffer != null) {
           takeFromBuffer(currentBuffer);
           if (currentBuffer.isEmpty())
             currentBuffer = exchanger.exchange(currentBuffer);
         }
       } catch (InterruptedException ex) { ... handle ...}
     }
   }
   void start() {
     new Thread(new FillingLoop()).start();
     new Thread(new EmptyingLoop()).start();
   }
 }

使用LinkedBlockingQueue是个不错的主意。不确定我是否喜欢代码的某些风格。。。但这个原则似乎是合理的。

我可能会给LinkedBlockingQueue添加一个容量,相当于您总内存的某个百分比。。比如10000个项目。。这样,如果你的编写速度太慢,你的工作线程就不会继续添加更多的工作,直到堆被填满。

我知道频繁的写操作可以使程序减慢很多

可能没有你想象的那么多,只要你使用缓冲。

相关内容

  • 没有找到相关文章

最新更新