为什么 BufferedReader 的两个引用在这里不独立运行?
这是一个尝试从文件中读取 2 个句子的程序。由于某种原因,程序尝试从新线程读取数据。在启动新线程之前,我初始化 BufferedReader 对象并将其作为对启动线程的函数的引用传递。现在我有 2 个 BufferedReader 对象的引用,我尝试从主线程和创建的新线程中读取句子。
在文件中,新行上的 2 个句子是:
D:\UnderTest\wavtester_1.wav D:\UnderTest\wavtester_2.wav
但输出令人惊讶:
FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav ON A NEW THREAD D:\UnderTest\wavtester_2.wav FROM THE MAIN THREAD
如果我们注意到,新线程不会读取第一个句子,而第一个句子已被主线程读取。当新线程读取第二个语句时,第三个输出也会在 FROM THE MAIN THREAD
前面打印空格。那么,我应该解释这两个参考文献实际上是一个吗?
如果我错了那么到底发生了什么?
import java.io.*;
class tester {
public static void main( String args[] ) {
try {
BufferedReader bfr = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
// pass a reference of bfr and start a new thread
startThread( bfr );
String y = null;
while( ( y = bfr.readLine() ) != null ) {
System.out.println("FROM THE MAIN THREAD" + " " + y );
}
} catch( Exception exc ) {
System.out.println( exc );
}
}
private static void startThread( final BufferedReader bfr ) {
Runnable r = new Runnable() {
@Override
public void run() {
fnctn( bfr );
}
};
new Thread(r).start();
}
private static void fnctn( BufferedReader bfr ) {
String x = null;
try {
//BufferedReader bfr1 = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
while( ( x = bfr.readLine() ) != null ) {
System.out.println("ON A NEW THREAD" + " " + x);
}
} catch( Exception exc ) {
System.out.println( exc );
}
}
}
与此相反,如果在方法 fnctn
中我初始化一个新的 BufferedReader
对象并从该引用中读取输出,则输出为:
FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav FROM THE MAIN THREAD D:\UnderTest\wavtester_2.wav FROM THE MAIN THREAD ON A NEW THREAD D:\UnderTest\wavtester_1.wav ON A NEW THREAD D:\UnderTest\wavtester_2.wav ON A NEW THREAD
此输出表明什么?
Here is a program that attempts to read 2 sentences from a file. For some reason the program attempts to read from a new thread . Before starting a new thread i initialize the BufferedReader
object and pass it as a reference to a function that initiates a thread. Now i have 2 references of the BufferedReader
object and i try to read the sentences from the main thread and the new thread created.
In the file the 2 sentences on new lines are :
D:\UnderTest\wavtester_1.wav D:\UnderTest\wavtester_2.wav
But the output is surprising:
FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav ON A NEW THREAD D:\UnderTest\wavtester_2.wav FROM THE MAIN THREAD
If we notice, the new thread does not read the first sentence and the first sentence has been read by the main thread. Also the 3rd output prints white space in front of FROM THE MAIN THREAD
when the 2nd statement has been read by the new thread. So, should i interpret that the 2 references are actually one ?
If i am wrong then what actually is happening ?
import java.io.*;
class tester {
public static void main( String args[] ) {
try {
BufferedReader bfr = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
// pass a reference of bfr and start a new thread
startThread( bfr );
String y = null;
while( ( y = bfr.readLine() ) != null ) {
System.out.println("FROM THE MAIN THREAD" + " " + y );
}
} catch( Exception exc ) {
System.out.println( exc );
}
}
private static void startThread( final BufferedReader bfr ) {
Runnable r = new Runnable() {
@Override
public void run() {
fnctn( bfr );
}
};
new Thread(r).start();
}
private static void fnctn( BufferedReader bfr ) {
String x = null;
try {
//BufferedReader bfr1 = new BufferedReader( new FileReader( new File( "e:\\AVS Player\\AudioPlaylists\\tester.txt") ) );
while( ( x = bfr.readLine() ) != null ) {
System.out.println("ON A NEW THREAD" + " " + x);
}
} catch( Exception exc ) {
System.out.println( exc );
}
}
}
As against this if in the method fnctn
i initialize a new BufferedReader
object and read from that reference the output is :
FROM THE MAIN THREAD D:\UnderTest\wavtester_1.wav FROM THE MAIN THREAD D:\UnderTest\wavtester_2.wav FROM THE MAIN THREAD ON A NEW THREAD D:\UnderTest\wavtester_1.wav ON A NEW THREAD D:\UnderTest\wavtester_2.wav ON A NEW THREAD
What does this output indicate ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个引用引用 BufferedReader 的同一个实例。
因此,如果一个线程读取某些内容,则下一个或同一线程将继续从最后一个位置读取。
在第二个示例中,您创建了两个不同的读取器,它们具有不同的内部状态(读取位置)。
The two references refer to the same instance of BufferedReader.
Therefore, if one thread reads something the next or the same thread continues to read from the last position.
In your second example you create two different readers which have different internal states (read position).