5

I have a multi-threaded program, in which I open a BufferedReader to read content from FIFO(named Pipe) file. As I want to implement stream type of solution to read text from FIFO file continuously, I have created a BufferedReader outside of thread task run, and want to keep that open forever as long as application is running.(No close() on bufferedReader) With the limited(let say 10) threads in ThreadPool will keep look for text in FIFO file and process that text for further. As I am using FIFO it will never reach END OF FILE.

By doing this, For a smaller input file it reads successfully, for a large input file it throws Stream closed IOexception(sporadically). It close automatically, I do not have close() statement. I have a code in place to acquire and close the semaphore lock at the place where i use br.readLine() to handle race condition issue

java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(BufferedReader.java:122) ~[?:1.8.0_152]
    at java.io.BufferedReader.readLine(BufferedReader.java:317) ~[?:1.8.0_152]
    at java.io.BufferedReader.readLine(BufferedReader.java:389) ~[?:1.8.0_152]

Question:

  1. For this solution I do not want to close the BufferedReader. What are the consequences?
  2. Can I have a bufferedReader which never be closed? if so what steps I should consider in code.
Nomad
  • 751
  • 4
  • 13
  • 34
  • I suggest having one reader thread which closes the file when it is finished. – Peter Lawrey Oct 05 '18 at 17:43
  • @PeterLawrey: Here I am trying to read that is streamed, Not like a set of data that fit in file. That is the reason I am using FIFO to read continuously as new data getting added. – Nomad Oct 11 '18 at 19:49
  • That is the sort of thing we do with Chronicle Queue, you will find it much simpler to have one reading thread. – Peter Lawrey Oct 12 '18 at 16:49

1 Answers1

5

BufferedReader is not a thread-safe class, so, we can get an uncountable number of various error on attempt to use the same object of that class from different threads.

denis.zhdanov
  • 3,734
  • 20
  • 25
  • That is right. For this type of problem, What file reader would be a good fit? How about using InputStream with synchronization – Nomad Oct 05 '18 at 17:55
  • 1
    It does not make sense to have multiple threads competing for the file. Read file in one thread put items to be processed one by one into some queue and process them using a pool of workers (if processing is a bottleneck) – Roman-Stop RU aggression in UA Oct 05 '18 at 18:14
  • I also have code in place to acquire and release semaphore lock during br.readLine() – Nomad Oct 05 '18 at 18:23
  • @RomanKonoval I am not reading a regular file. FIFO in linux is work as a queue. – Nomad Oct 05 '18 at 18:28