扫描仪的含义不同步 [英] Meaning of scanner isn't synchronized
问题描述
我正在浏览Java中的扫描器和BufferedReader之间的区别,我无法理解的一点是
I was going through the differences between scanner and BufferedReader in Java and one point which I could not understand was that said
在BufferedReader时扫描仪未同步
现在任何人都可以解释它的意思吗?
Now can anyone please explain what it means?
推荐答案
从字面上看,它表示它的意思。 BufferedReader
API的关键操作是使用 synchronized
块实现的,而等效操作在 Scanner中进行
不是。
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.
这意味着 BufferedReader
可以在安全之间共享多线程 1 ,而 Scanner
不能。 扫描器
本质上是非线程安全的,即使它包装了线程安全的输入源。
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-实际上,这不会使您不必考虑线程。如果您有多个线程在同一个 BufferedReader
上调用 read(...)
操作而没有某种形式的协调,那么无法知道哪个线程将从流中读取哪些字符。根据一些定义,这将使使用成为非线程安全的。将字符分配到正确的线程通常对于应用程序的正确性很重要。
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.
这篇关于扫描仪的含义不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!