新公司入職一個多月了,至今沒有事情能夠作,十來個新同事都同樣抓狂,因此你們都本身學習一些新東西,我最近在看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,望大神勿噴!