Is it possible to read and write in file at the same time?

后端 未结 1 1686
攒了一身酷
攒了一身酷 2020-12-16 04:19

Here\'s the scenario:

  • ThreadA is going to read from some socket, and write data to \"MyFile.txt\"
  • ThreadB is going to read \"MyFile\", and when it rea
相关标签:
1条回答
  • 2020-12-16 04:46

    The problem you mention is a famous Producer Consumer Problem

    Common solution to this is to use BlockingQueue

    An example of real world usage is in AjaxYahooSearchEngineMonitor

    What Thread A does is, it will submit a string to queue, and then return immediately.

    What Thread B does is, it will pick up the item from queue one by one, and process them. When there is no item in the queue, Thread B will just wait there. See line 83 of the source code.

    0 讨论(0)
提交回复
热议问题