java多线程批量读取文件(七)

新公司入职一个多月了,至今没有事情能够作,十来个新同事都同样抓狂,因此你们都本身学习一些新东西,我最近在看zookeeper,感受蛮不错的,和微服务的zuul以及eureka功能相似,只是代码复杂了一些。而今天,我所要说的是java多线程读取文件的两个例子;java

例子1:java多线程批量读取文件安全

package face.thread.ReadFile;多线程

/**
 * 多线程读、写文件
 * 
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;app

public class CompareTest3 {
    public static void main(String args[]) {
     long millis1 = System.currentTimeMillis();
     System.out.println(millis1);
     Read3 read = new Read3(millis1);
     ExecutorService service = Executors.newFixedThreadPool(5);
     for(int i = 1; i <= 3; i++){
      service.execute(new Thread(read, "线程" + i)); 
     }
    }
}
 
class Read3 implements Runnable {
 Object o = new Object();
    List<File> filePathsList = new ArrayList<File>();
    int index = 0;
    private long millis ;
    public Read3(long millis1 ) {
     this.millis = millis1;
        File f = new File("d:" + File.separator + "gc2");
        getFileList(f);
    }
 
    private void getFileList(File f) {
        File[] filePaths = f.listFiles();
        for (File s : filePaths) {
            if (s.isDirectory()) {
                getFileList(s);
            } else {
                if (-1 != s.getName().lastIndexOf(".txt")) {
                    filePathsList.add(s);
                }
            }
        }
    }
 
    public void run() {
        File file = null;
        File f2 = null;
        while (index < filePathsList.size()) {
         //此处,保证了多线程不会交叉读取文件微服务

 

//--1.1方法内的变量是线程安全的
//解释:因为方法内的变量是私有的,本体访问的同时别人访问不了,因此是线程安全的。
//--1.2实例变量是非线程安全的
//解释:因为实例变量能够由多个线程访问,当本体操做变量过程当中,别人也能够抢占资源操做变量,使数据不一样步了,因此是非线程安全的。


            synchronized (o) {
                if (index > filePathsList.size()) {
                    return;
                }
                file = filePathsList.get(index);
                index++;
                //System.out.println("内部index: " + index);
            }性能

 

        //    System.out.println("文件: " + file.getName());
            FileReader fr = null;
            BufferedReader br = null;
            StringBuffer sb = new StringBuffer();
            
            FileWriter fw  = null;
            BufferedWriter bw = null;
            f2 = new File("d:" + File.separator + "gc3" + File.separator + file.getName());
            
   try {
    fr = new FileReader(file);
       br = new BufferedReader(fr);
       
       fw = new FileWriter(f2);
    bw = new BufferedWriter(fw);
     
       String data = "";
    while((data = br.readLine()) != null){
    // sb.append(data + "\r");
     bw.write(data + "\r");
    }
    
    bw.write("---------------" + Thread.currentThread().getName()+"---------------");
    System.out.println(Thread.currentThread().getName() + " : " + file.getName());
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     bw.close();
     br.close();
     
     /*long millis2 = System.currentTimeMillis();
        System.out.println(millis2);
           System.out.println(millis2 - millis);  //大约1-2ms*/
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
        }
    }
}学习

 

例子2:一样的读取文件,改成单线程读取this

package face.thread.ReadFile;线程

/**
 * 单线程读、写文件
 */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;资源

public class CompareTest4 {
    public static void main(String args[]) {
     
     final long millis1 = System.currentTimeMillis();
     final CyclicBarrier cb = new CyclicBarrier(1,new Runnable(){
            public void run() {
                long millis2 = System.currentTimeMillis();
              System.out.println(millis2);
                System.out.println(millis2 - millis1);  //大约1-2ms
            }
        }); 
     
        
     Read4 read = new Read4(cb);
     ExecutorService service = Executors.newFixedThreadPool(1);
     for(int i = 1; i <= 1; i++){
      service.execute(new Thread(read, "线程" + i)); 
     }
    }
}
 
class Read4 implements Runnable {
  
 Object o = new Object();
    List<File> filePathsList = new ArrayList<File>();
    int index = 0;
    CyclicBarrier cb2;
    public Read4(CyclicBarrier cb) {
     this.cb2 = cb;
        File f = new File("d:" + File.separator + "gc2");
        getFileList(f);
    }
 
    private void getFileList(File f) {
        File[] filePaths = f.listFiles();
        for (File s : filePaths) {
            if (s.isDirectory()) {
                getFileList(s);
            } else {
                if (-1 != s.getName().lastIndexOf(".txt")) {
                    filePathsList.add(s);
                }
            }
        }
    }
 
    public void run() {
        File file = null;
        File f2 = null;
        while (index < filePathsList.size()) {
            synchronized (o) {
                if (index > filePathsList.size()) {
                    return;
                }
                file = filePathsList.get(index);
                index++;
                //System.out.println("内部index: " + index);
            }

        //    System.out.println("文件: " + file.getName());
            FileReader fr = null;
            BufferedReader br = null;
            StringBuffer sb = new StringBuffer();
            
            FileWriter fw  = null;
            BufferedWriter bw = null;
            f2 = new File("d:" + File.separator + "gc3" + File.separator + file.getName());
            
   try {
    fr = new FileReader(file);
       br = new BufferedReader(fr);
       
       fw = new FileWriter(f2);
    bw = new BufferedWriter(fw);
     
       String data = "";
    while((data = br.readLine()) != null){
    // sb.append(data + "\r");
     bw.write(data + "\r");
    }
    
    bw.write("---------------" + Thread.currentThread().getName()+"---------------");
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     bw.close();
     br.close();
      try {
      cb2.await();
     } catch (InterruptedException e) {
      e.printStackTrace();
     } catch (BrokenBarrierException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
        }
    }
}

 

两个例子中,打印的时间即表明线程读取每一个代码的时间,性能对比一看就能体现出来,只是个小Demo,望大神勿喷!

相关文章
相关标签/搜索