I was going through the differences between scanner and BufferedReader in Java and one point which I could not understand was that said
Scanner is not synchronized while BufferedReader is.
Now can anyone please explain what it means?
I was going through the differences between scanner and BufferedReader in Java and one point which I could not understand was that said
Scanner is not synchronized while BufferedReader is.
Now can anyone please explain what it means?
Literally, it means what it says. Key operations of the BufferedReader
API are implemented using synchronized
blocks, and the equivalent operations in Scanner
are not.
This means that a BufferedReader
can be "safely" shared between multiple threads1, whereas a Scanner
cannot. A Scanner
is inherently non-thread-safe, even if it wraps a thread-safe input source.
1 - Actually, this does not absolve you from thinking about threading. If you have multiple threads calling read(...)
operations on the same BufferedReader
without some form of coordination, then there is no way to know which thread will read which characters from the stream. By some definitions, that would make the usage non-thread-safe. The disposition of the characters to the right threads is usually important to the correctness of the application.