Java multithreaded processing

Source: Internet
Author: User

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
* Time: June 1, 2016
*/
public static void Main (string[] args) {
System.out.println ("========== Execution time Startdata:" +system.currenttimemillis ());
GetFiles (FILEPATH2);
Readlinefile ();
Findcontext (0);
Execute ();
Long endTime = System.currenttimemillis ();
System.out.println ("Execution Time:" + (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;
Read one line at a time until NULL is read to the end of the file
while ((tempstring = Reader.readline ()) = null) {
Show line Numbers
Linecontext = tempstring;
Contextlist22.add (Linecontext);
}
Reader.close ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (reader! = null) {
try {
Reader.close ();
} catch (IOException E1) {
}
}
}
Return "";
}
/**
* Find in Files
*
* Liu.yj
* Time: June 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;
Read one line at a time until NULL is read to the end of the file
while ((tempstring = Reader.readline ()) = null) {
Show line Numbers
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-value: \ 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 ()) {
/*
* Recursive invocation
*/
GetFiles (File.getabsolutepath ());

}else{
Filelist.add (File.getabsolutepath ());
System.out.println ("Show" +filepath+ "All Subdirectories" +file.getabsolutepath ());
}
}
}

public static void Execute () {
Create 20 threads
Executorservice pool = Executors.newfixedthreadpool (20);
for (int i=0;i<20;i++) {
Thread T1 = new MyThread (i*51,51);
Pool.execute (t1);
}
Close the thread pool
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 ("========== Execution Time:" +endtime);
}
}

Java multithreaded processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.