java多线程处理

时间:2021-11-12 18:31:33

package com.copyFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


//50090
//41896
//10303
//10131
public class CopyFile {

private static String filePath1 = "E:\\test\\yuanshi.txt";
private static String filePath2 = "E:\\test\\mubiao\\";
private static ArrayList<String> filelist = new ArrayList<String>();
private static ArrayList<String> contextList22 = new ArrayList<String>();
private static ArrayList<ArrayList<String>> mubiaoContext = new ArrayList<ArrayList<String>>();
/**
* @param args
* 作者: liu.yj
* 时间: Jun 1, 2016
*/
public static void main(String[] args) {
System.out.println("==========执行耗时startData:"+System.currentTimeMillis());
getFiles(filePath2);
readLineFile();
//findContext(0);
execute();
Long endTime = System.currentTimeMillis();
System.out.println("执行耗时:"+(1464834022605L-1464834011597L));
}

public static String readLineFile(){
File file = new File(filePath1);
String lineContext = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
lineContext = tempString;
contextList22.add(lineContext);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return "";
}
/**
* 在文件中查找
*
* 作者: liu.yj
* 时间: Jun 1, 2016
*/
public static void findContext(int index,int stape){
for(int i=index;i<index+stape;i++){
//for(int i=0;i<filelist.size();i++){
find2(filelist.get(i));
}
}
public static void find2(String filePath){
File file = new File(filePath);
Map<String,ArrayList<String>> _map = new HashMap<String,ArrayList<String>>();
ArrayList<String> contextList = new ArrayList<String>();
String lineContext = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
lineContext = tempString;
if(lineContext != null && !"".equals(lineContext)){
String args[] = lineContext.split(" ");
if(contextList22.contains(args[0])){
if(args.length >2){
System.out.println("KEY:\t"+args[0]+"\t值:\t"+args[2]);
}
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
//_map.put(filePath, contextList);
mubiaoContext.add(contextList);
}
static void getFiles(String filePath){
File root = new File(filePath);
File[] files = root.listFiles();
for(File file:files){
if(file.isDirectory()){
/*
* 递归调用
*/
getFiles(file.getAbsolutePath());

}else{
filelist.add(file.getAbsolutePath());
//System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
}
}
}

public static void execute(){
//创建二十个线程
ExecutorService pool = Executors.newFixedThreadPool(20);
for(int i=0;i<20;i++){
Thread t1 = new MyThread(i*51,51);
pool.execute(t1);
}
// 关闭线程池
pool.shutdown();
}

}
class MyThread extends Thread {
private int countIndex = 0;
private int stape = 0;
public MyThread(int myIndex,int mStape){
countIndex = myIndex;
stape = mStape;
}
@Override
public void run() {
Long startTime = System.currentTimeMillis();
CopyFile.findContext(countIndex,stape);
Long endTime = System.currentTimeMillis();
System.out.println("==========执行耗时:"+endTime);
}
}