The JavaDocs of java.util.Scanner currently says...
* <p>A {@code Scanner} is not safe for multithreaded use without
* external synchronization.
...but the source code actually instantiates StringReader...
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
...and StringReader *is* synchronized.
As synchronization is a *non*-target of the Scanner class, and the Scanner does *not* leak the Readable it is reading from, we can safely replace "new StringReader" by "Reader.of(CharSequence);" here, which effectively provides a *non*-synchronized (hence potentially more efficient) Reader.
* <p>A {@code Scanner} is not safe for multithreaded use without
* external synchronization.
...but the source code actually instantiates StringReader...
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
...and StringReader *is* synchronized.
As synchronization is a *non*-target of the Scanner class, and the Scanner does *not* leak the Readable it is reading from, we can safely replace "new StringReader" by "Reader.of(CharSequence);" here, which effectively provides a *non*-synchronized (hence potentially more efficient) Reader.
- links to
-
Review(master)
openjdk/jdk/29627