java < 处理_java多线程处理

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 filelist = new ArrayList();

private static ArrayList contextList22 = new ArrayList();

private static ArrayList> mubiaoContext = new ArrayList>();

/**

* @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

//for(int i=0;i

find2(filelist.get(i));

}

}

public static void find2(String filePath){

File file = new File(filePath);

Map> _map = new HashMap>();

ArrayList contextList = new ArrayList();

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值