多线程到达,最后一个应该做处理
问题描述:
我正在实现一个日志记录,其中多个线程可以写入一个List
日志.最后一个线程应将List
的内容写入文件.因此,最后写入List
的线程应将List
刷新到文件中.最好的方法是什么?
I'm implementing a logging where multiple threads can write into one List
of log. The last thread should write the contents of the List
to a file. So the thread which is the last to write into the List
should flush the List
into a file. What is the best way to accomplish this?
对于List
,我只需要对多个作者和一个读者有效的并发类之一.
For the List
I just need one of the concurrent classes that is efficient for multiple writers and one reader.
答
对于我来说,简单的解决方案是实现Closeable
,然后使用close
方法进行刷新.
In my case the simple solution was to implement Closeable
and then do the flushing in the close
method.