简体   繁体   English

同步写入Java中同一文件的多个线程

[英]Synchronizing several threads writing to the same file in java

Of course there is the obvious way of using "synchronized". 当然,有使用“同步”的明显方法。 But I'm creating a system designed for running on several cores and writing to that file various times at the same milisecond. 但是我正在创建一个旨在在多个内核上运行的系统,并在同一毫秒内多次写入该文件。 So I believe that using synchronize will hurt performance badly. 因此,我相信使用同步会严重损害性能。 I was thinking of using the Pipe class of java (but not sure if it will help) or having each thread write to a different file and an additional thread collecting those writings, creating the final result. 我当时正在考虑使用Java的Pipe类(但不确定是否会有所帮助),或者让每个线程写入不同的文件,并让另一个线程收集这些著作,从而创建最终结果。 I should mention that the order of the writings isn't important and it is timestamped in nanotime anyway. 我应该指出,写作的顺序并不重要,而且无论如何它都在纳米时间加盖了时间戳。 What is the better idea of those two? 这两个更好的主意是什么? have any other suggestions? 还有其他建议吗? thanks. 谢谢。

Using some sort of synchronization (eg. single mutexes) is quite easy to implement. 使用某种同步(例如单个互斥锁)非常容易实现。

If I had enough RAM I would create a queue of some sort for each log-producer thread , and a log-consumer thread to read from all the queues in a round-robin fashion (or something like that). 如果我有足够的RAM,我将为每个日志生产者线程创建一个某种队列,并创建一个日志消费者线程以循环方式(或类似方式)从所有队列中读取。

Not a direct answer to your question, but the logback project has synchronization facilities built into it for writing to the same file from different threads, so you might try to use it if it suits your needs, or at least take a look at it's source code. 不是直接回答您的问题,但是logback项目内置了同步功能,可以从不同的线程写入同一文件,因此,如果需要,您可以尝试使用它,或者至少看看它的来源码。 Since it's built for speed, I'm pretty sure the implementation isn't to be taken for granted. 由于它是为提高速度而构建的,因此我很确定实现不会被视作理所当然。

You are right to be concerned, you are not going to be able to have all the threads write to the same file without a performance problem. 您应该担心,您将无法使所有线程写入同一文件而不会出现性能问题。

When I had this problem (writing my own logging, way back before Log4j) I created two fixed-size buffers in memory and had all the producer threads write to one buffer while a dedicated consumer thread read from the other buffer and wrote to a file. 当我遇到这个问题(写自己的日志,在Log4j之前返回)时,我在内存中创建了两个固定大小的缓冲区,并让所有生产者线程写入一个缓冲区,而专用的使用者线程则从另一个缓冲区读取并写入了文件。 。 That way the writer threads had to synchronize only on getting and incrementing the index to the buffer and when the buffers were being swapped, and it only blocked when the current buffer was full. 这样,编写器线程仅需在获取和递增到缓冲区的索引时以及在交换缓冲区时进行同步,并且仅在当前缓冲区已满时才阻塞。 It was memory-intensive but fast. 它占用大量内存,但速度很快。

For other ideas you could check out how loggers like Log4j and Logback work, they would have had to solve this problem. 对于其他想法,您可以查看Log4j和Logback等记录器如何工作,他们必须解决此问题。

Try to use JMS. 尝试使用JMS。 All your processes running on different machines may send JMS message instead of writing to file. 您在不同计算机上运行的所有进程可能会发送JMS消息,而不是写入文件。 Create only one queue receiver that receives messages and writes them to file. 仅创建一个队列接收器来接收消息并将其写入文件。 Log4J already has this functionality: see JMSAppender. Log4J已经具有此功能:请参阅JMSAppender。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM