Java: Concurrent reads on an InputStream

后端 未结 3 1510
半阙折子戏
半阙折子戏 2021-01-13 00:42

Been looking around for a little while now and I\'m a bit confused on this issue. I want to be able to take an input stream and read it concurrently in segments. The segme

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 01:17

    I don't think you can read an InputStream concurrently. That is why the contract defines read, reset, and mark - the idea is that the stream keeps track internally what has been read and what has not.

    If you're reading a file, just open multiple streams. You could use the skip() method to move the marker ahead for other threads to avoid duplicate line processing. BufferedReader may help some too, as it offers reading line by line.

提交回复
热议问题