File data = new File("data.txt")
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
data), "UTF-8")
final BufferedReader bufferedReader = new BufferedReader(read)
for (int i = 0i <5i++) {
new Thread(new Runnable() {
@Override
public void run() {
String lineTXT = null
synchronized (bufferedReader) {
try {
while ((lineTXT = bufferedReader.readLine()) != null) {
System.out.println(Thread.currentThread()+":"+lineTXT)
bufferedReader.notify()
bufferedReader.wait()
}
} catch (IOException e) {
e.printStackTrace()
} catch (InterruptedException e) {
e.printStackTrace()
}finally{
bufferedReader.notifyAll()
}
}
}
}).start()
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace()
} catch (FileNotFoundException e) {
e.printStackTrace()
}
}
1、采用public static的变量存储这一数值,每个线程都改吵往这一共有静态变量里写入已复制大小侍弯。 2、采用Callable方式实现多线程,将结果作为返回值返回到主线程。这一方法核谈侍只能在所有子线程都完成之后才能通过future获取。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)