문제

Based on the documentation of java.NIO, buffers are thread-unsafe, while channels in most of the cases (FileChannel, SocketChannel etc.) are safe to be used by multiple threads. What I cannot find in the documentation is a clear description of what happens when e.g. two threads start to read from a FileChannel concurrently. Does the first operation read first N bytes, or is it possible to get first M bytes to the first channel's buffer, and then the next couple would go to the second buffer, before we context switch would read some more into the first etc. Does anyone know what the guarantees are for multithreaded access to channels?

도움이 되었습니까?

해결책

A read "involves the file's position" and therefore blocks subsequent reads by other threads. However nothing guarantees how many bytes a read returns whether single- or multi-threaded, other than that it is greater than zero if there are bytes left to read.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top