import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.sysware.framework.utils.ValidateUtil;
import com.sysware.require.item.enmu.TitleTypeEnum;
import com.sysware.require.item.entity.ItemEntity;
import com.sysware.require.util.FileUtil;
/**
* 多线程读取多个文件
*/
public class FileThread extends Thread{
private final CountDownLatch countDownLatch;
private List<String> filelist;
private Map<Long,String> results;
public FileThread(CountDownLatch countDownLatch,List<String> filelist, Map<Long,String> results){
this.countDownLatch = countDownLatch;
this.filelist = filelist;
this.results = results;
}
@Override
public void run() {
for (int i = 0; i < filelist.size(); i++) {
String each = filelist.get(i);
String[] split = each.split("@_@");
String id = split[0];
String htmlpath = split[1];
File file = new File(htmlpath);
if (!file.exists()){
System.out.println("文件查找失败:" + htmlpath + "不是一个目录!");
} else {
StringBuffer htmlSb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlpath)));
while (br.ready()) {
htmlSb.append(br.readLine());
}
results.put(Long.valueOf(id), htmlSb.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
if(countDownLatch != null){
countDownLatch.countDown();
}
if(br != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
public static Map<Long,String> getBody(List<ItemEntity> items, List<Map<String, Object>> list,String docPath){
List<String> fileList = new CopyOnWriteArrayList<String>();
Map<Long,String> result = new ConcurrentHashMap<Long,String>();
if(ValidateUtil.isNotEmpty(list)){
for (Map<String, Object> map : list) {
if(Integer.parseInt(map.get("NODE_TYPE").toString()) == TitleTypeEnum.TYPE_CONTENT.getCode()){
String buildFileHisname = FileUtil.buildHtmlFilename(docPath, map.get("DOCUMENT_ID").toString(), map.get("ENTITY_ID").toString(), "html");
if(!FileUtil.isExist(buildFileHisname)){
map.put("CONTENT", "");
}
fileList.add(map.get("ENTITY_ID").toString()+"@_@"+buildFileHisname);
}
}
}
if (fileList != null && fileList.size() > 0) {
int count = fileList.size() / 10 ;
CountDownLatch latch = new CountDownLatch(count);
long currentTimeMillis = System.currentTimeMillis();
System.out.println("All thread started at " + currentTimeMillis);
for (int i = 0 ;i<count;i++) {
int start = i * 10;
int end = (i+1) * 10-1;
List<String> subList = fileList.subList(start, end);
Thread t = new Thread(new FileThread( latch , subList, result));
t.setName("Thread-" +i);
t.start();
}
try {
latch.await(5000,TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("All thread finished cost " + (System.currentTimeMillis()-currentTimeMillis));
Set<Entry<Long,String>> entrySet = result.entrySet();
for (Entry<Long, String> entry : entrySet) {
System.out.println(entry.getKey()+"======"+entry.getValue());
}
}
return result;
}
/**
* %JAVA_OPTS% -server -Xms800m -Xmx800m -XX:MaxNewSize=256m
*
*<hr>
* @author hanjidong
* @date 2020年12月7日 下午3:40:07
* @since 0.0.1
* @return
* Map<Long,String>
*/
public static Map<Long,String> getBody(){
// List<String> fileList = new CopyOnWriteArrayList<String>();
List<String> fileList = new ArrayList<String>();
Map<Long,String> result = new ConcurrentHashMap<Long,String>();
for(int i= 0;i<1000;i++){
fileList.add(i+"@_@"+"D:\\doc\\10\\10\\html\\100_"+i+".html");
}
if (fileList != null && fileList.size() > 0) {
int count = fileList.size() / 10 ;
CountDownLatch latch = new CountDownLatch(count);
long currentTimeMillis = System.currentTimeMillis();
System.out.println("All thread started at " + currentTimeMillis);
for (int i = 0 ;i<count;i++) {
int start = i * 10;
int end = (i+1) * 10-1;
List<String> subList = fileList.subList(start, end);
Thread t = new Thread(new FileThread( latch , subList, result));
t.setName("Thread-" +i);
t.start();
}
try {
latch.await(5000,TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("All thread finished at " + new Date());
System.out.println("All thread finished cost " + (System.currentTimeMillis()-currentTimeMillis));
// Set<Entry<Long,String>> entrySet = result.entrySet();
// for (Entry<Long, String> entry : entrySet) {
// System.out.println(entry.getKey()+"======"+entry.getValue());
//
// }
}
return result;
}
public static void main(String[] args) throws IOException {
/**
* copy file start 1607325632573
17271 copy file finish
*/
// long currentTimeMillis = System.currentTimeMillis();
// System.out.println(" copy file start "+ currentTimeMillis);
// for (int i=0;i <10000 ;i++) {
// FileUtil.nioTransferCopy("D:\\doc\\10\\10\\html\\10_10.html", "D:\\doc\\10\\10\\html\\100_"+i+".html");
// }
// System.out.println((System.currentTimeMillis()-currentTimeMillis)+" copy file finish ");
getBody();
}
}