package comparison;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class CompareTest {
public static void main(String args[]){
read read = new read();
new Thread(read, "线程1").start();
new Thread(read, "线程2").start();
new Thread(read, "线程3").start();
new Thread(read, "线程4").start();
new Thread(read, "线程5").start();
// Listlist = read.readXML();
// for(String s:list){
// System.out.println(s);
// }
}
}
class read implements Runnable{
@Override
public void run() {
File f = new File("d:"+File.separator+"comparetest");
File[] filePaths = f.listFiles();
ListfilePathsList = new ArrayList<>();
for(File s:filePaths){
filePathsList.add(s);
}
//解析xml
SAXBuilder builder = new SAXBuilder();
ListxmlList = new ArrayList<>();
for(int i=0;isynchronized(this){
try {
Thread.sleep(300);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
InputStream is = new FileInputStream(filePathsList.get(i));
System.out.println("当前使用的线程是:"+Thread.currentThread().getName()+",正在读文件:"+filePathsList.get(i)+",列表当前长度:"+filePathsList.size());
Document doc = builder.build(is);
Element root = doc.getRootElement();
Listlist = root.getChildren();
for(Element e:list){
xmlList.add(e.getChildTextTrim("ERROR_FEEDBACK_ID"));
}
xmlList.add("--------------------------");
} catch (JDOMException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
// public synchronized ListreadXML(){
//
//// }
// return xmlList;
// }
}
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class CompareTest {
public static void main(String args[]) {
read read = new read();
new Thread(read, "线程1").start();
new Thread(read, "线程2").start();
new Thread(read, "线程3").start();
new Thread(read, "线程4").start();
new Thread(read, "线程5").start();
}
}
class read implements Runnable {
ListfilePathsList = new ArrayList ();
int index = 0;
public read() {
File f = new File("d:" + File.separator + "tmp");
getFileList(f);
}
private void getFileList(File f) {
File[] filePaths = f.listFiles();
for (File s : filePaths) {
if (s.isDirectory()) {
getFileList(s);
} else {
if (s.isFile() && -1 != s.getName().lastIndexOf(".xml")) {
filePathsList.add(s);
}
}
}
}
@Override
public void run() {
File file = null;
while (index < filePathsList.size()) {
synchronized (this) {
file = filePathsList.get(index);
if (file == null) {
continue;
}
index++;
}
// 解析xml
SAXBuilder builder = new SAXBuilder();
ListxmlList = new ArrayList ();
try {
Thread.sleep(300);
} catch (InterruptedException e2) {
e2.printStackTrace();
}
try {
InputStream is = new FileInputStream(file.getPath());
System.out.println("当前使用的线程是:"
+ Thread.currentThread().getName() + ",正在读文件:"
+ filePathsList.indexOf(file) + ",列表当前长度:"
+ filePathsList.size());
Document doc = builder.build(is);
Element root = doc.getRootElement();
Listlist = root.getChildren();
for (Element e : list) {
xmlList.add(e.getChildTextTrim("ERROR_FEEDBACK_ID"));
}
xmlList.add("--------------------------");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
public class CompareTest {
public static void main(String args[]) {
read read = new read();
new Thread(read, "线程1").start();
new Thread(read, "线程2").start();
new Thread(read, "线程3").start();
new Thread(read, "线程4").start();
new Thread(read, "线程5").start();
}
}
class read implements Runnable {
ListfilePathsList = new ArrayList ();
int index = 0;
public read() {
File f = new File("d:" + File.separator + "tmp");
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(".xml")) {
filePathsList.add(s);
}
}
}
}
@Override
public void run() {
File file = null;
while (index < filePathsList.size()) {
synchronized (this) {
if (index >= filePathsList.size()) {
continue;
}
file = filePathsList.get(index);
index++;
}
// 解析xml
SAXBuilder builder = new SAXBuilder();
ListxmlList = new ArrayList ();
try {
Thread.sleep(300);
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
InputStream is = new FileInputStream(file.getPath());
System.out.println("当前使用的线程是:"
+ Thread.currentThread().getName() + ",正在读文件:"
+ filePathsList.indexOf(file) + ",列表当前长度:"
+ filePathsList.size());
Document doc = builder.build(is);
Element root = doc.getRootElement();
Listlist = root.getChildren();
for (Element e : list) {
xmlList.add(e.getChildTextTrim("ERROR_FEEDBACK_ID"));
}
xmlList.add("--------------------------");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
import java.io.File;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class Test_15 {
public static final int THREAD_POOL_SIZE = 5;
private final File POISON = new File("");
private final BlockingQueuefiles = new LinkedBlockingQueue (1000);
private final ConcurrentLinkedQueueinfos = new ConcurrentLinkedQueue ();
private Thread[] pool = null;
private volatile boolean running = false;
public Test_15() {
this(THREAD_POOL_SIZE);
}
public Test_15(int poolSize) {
pool = new Thread[poolSize];
FileWorker worker = new FileWorker();
for(int i=0;ipool[i] = new Thread(worker,"线程"+(i+1));
pool[i].start();
}
running = true;
}
private class FileWorker implements Runnable {
@Override
public void run() {
File file = null;
try {
while((file=files.take()) != POISON){
try {
doWork(file);
} catch (Exception e) {
onException(e,file);
}
}
files.put(POISON);
} catch (InterruptedException e) {
}
}
private void onException(Exception e, File file) {
e.printStackTrace();
}
private void doWork(File file) {
// 解析XML
SAXBuilder builder = new SAXBuilder();
try {
System.out.println("当前使用的线程是:"
+ Thread.currentThread().getName() + ",正在读文件:"
+ file.getName() + ",列表当前长度:"
+ files.size());
Document doc = builder.build(file);
Element root = doc.getRootElement();
Listlist = root.getChildren();
for (Element e : list) {
infos.add(e.getChildTextTrim("ERROR_FEEDBACK_ID"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void addFile(File file) throws InterruptedException{
files.put(file);
}
public void addFilesIgnoreInterrupted(File[] files) {
for(File file : files){
try {
this.files.put(file);
} catch (InterruptedException e) {
}
}
}
public void shutdown(){
try {
if(running){
running = false;
files.put(POISON);
}
} catch (InterruptedException e) {
}
}
public void waiting(){
if(running || !files.contains(POISON)){
throw new IllegalStateException("You must call shutdown() function before.");
}
for(Thread t : pool){
try {
t.join();
} catch (InterruptedException e) {
}
}
}
public QueuegetInfos(){
return infos;
}
public static void main(String[] args) {
Test_15 instance = new Test_15();
File folder = new File("d:\\comparetest");
instance.addFilesIgnoreInterrupted(folder.listFiles());
instance.shutdown();
instance.waiting();
for(String info : instance.getInfos()){
System.out.println(info);
}
}
}