我最近在研究多线程和线程池 求JAVA多线程读取文件-提供下载的DEMO 最好带注释 O(∩_∩)O谢谢!!!!!
1个回答
展开全部
import java.io.*;
public class ThreadsSum6{
public static void main(String[] args) throws IOException {
String str;
long sum1=0;
long start = System.currentTimeMillis();
FileReader fr=new FileReader("arrays-1.txt");
//建立文件输入流
BufferedReader br=new BufferedReader(fr);
//建立缓冲输入流
while ((str=br.readLine())!=null) {
sum1+=Integer.parseInt(str);
}
fr=new FileReader("arrays-2.txt");
br=new BufferedReader(fr);
while ((str=br.readLine())!=null) {
sum1+=Integer.parseInt(str);
}
fr.close();
long end = System.currentTimeMillis();
System.out.println("sum1="+sum1);
System.out.println("time1="+(end - start));
start = System.currentTimeMillis();
ReadFileThread th1=new ReadFileThread("arrays-1.txt");
ReadFileThread th2=new ReadFileThread("arrays-2.txt");
th1.start();
th2.start();
try{//等待线程结束
th1.join();
th2.join();
}catch(Exception e){}
long sum2=th1.getSum()+th2.getSum();
end = System.currentTimeMillis();
System.out.println("sum2="+sum2);
System.out.println("time2="+(end - start));
}
}
class ReadFileThread extends Thread{
long sum=0;
FileReader fr;
BufferedReader br;
String str;
public ReadFileThread(String fileName){
try{
fr=new FileReader(fileName);
//建立文件输入流
br=new BufferedReader(fr);
//建立缓冲输入流
}
catch(Exception e){System.out.println("error1");}
}
public void run(){
try{
while((str=br.readLine())!=null) {
sum+=Integer.parseInt(str);
}
fr.close();
}
catch(Exception e){System.out.println("error2");}
}
long getSum(){
return sum;
}
}
public class ThreadsSum6{
public static void main(String[] args) throws IOException {
String str;
long sum1=0;
long start = System.currentTimeMillis();
FileReader fr=new FileReader("arrays-1.txt");
//建立文件输入流
BufferedReader br=new BufferedReader(fr);
//建立缓冲输入流
while ((str=br.readLine())!=null) {
sum1+=Integer.parseInt(str);
}
fr=new FileReader("arrays-2.txt");
br=new BufferedReader(fr);
while ((str=br.readLine())!=null) {
sum1+=Integer.parseInt(str);
}
fr.close();
long end = System.currentTimeMillis();
System.out.println("sum1="+sum1);
System.out.println("time1="+(end - start));
start = System.currentTimeMillis();
ReadFileThread th1=new ReadFileThread("arrays-1.txt");
ReadFileThread th2=new ReadFileThread("arrays-2.txt");
th1.start();
th2.start();
try{//等待线程结束
th1.join();
th2.join();
}catch(Exception e){}
long sum2=th1.getSum()+th2.getSum();
end = System.currentTimeMillis();
System.out.println("sum2="+sum2);
System.out.println("time2="+(end - start));
}
}
class ReadFileThread extends Thread{
long sum=0;
FileReader fr;
BufferedReader br;
String str;
public ReadFileThread(String fileName){
try{
fr=new FileReader(fileName);
//建立文件输入流
br=new BufferedReader(fr);
//建立缓冲输入流
}
catch(Exception e){System.out.println("error1");}
}
public void run(){
try{
while((str=br.readLine())!=null) {
sum+=Integer.parseInt(str);
}
fr.close();
}
catch(Exception e){System.out.println("error2");}
}
long getSum(){
return sum;
}
}
追问
呃...貌似我还是看不大懂 而且设置了文件路径之后 没跑起来 乱码了......求详解 O(∩_∩)O谢谢!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询