I have a file. It sometimes happens that one thread has finsihed reading the file. The moment he is closing the reader another thread comes and try to read from the same stream associted with the reader. But then i get some
stream already close exception.
How i can resolve such issues.
Like
new BufferedReader(new InputStreamReader(in));
How i can i create seprate stream for different threads.
Well, in my opinion you should not close the stream when a thread has finished its work. Just leave it open and close it when all the work is done.
Of course, since you are using the same stream you will need to synchronize access to the stream to prevent conflicts. You just need to use an object as a monitor. Like:
Each thread will run this code. Then the main thread will call notify for the first thread to allow it to read and then if you start other threads they will wait for the previous one to finish.
Note that here I’m assuming you don’t want to read from the file in parallel.