package com.daimac.worm;
import com.alibaba.fastjson.JSON;
import com.daimac.worm.entity.PPTEntity;
import com.daimac.worm.thread.PPTThread;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
@Slf4j
public class Test {
public static void main(String[] args) throws IOException {
long t1 = System.currentTimeMillis();
File file = new File("/ppt.txt");
FileOutputStream out = new FileOutputStream(file);
PrintStream printStream = new PrintStream(out);
List<PPTThread> threads = new ArrayList<>();
// 总页数(目前第一ppt是106页)
int pageCount = 106;
// 线程数(并不是越多越好,考虑带宽,线程切换消耗)
int tnum = 30;
// 平均每个线程需要工作数量
int offset = 106 / tnum;
// 所有pptlist
List<PPTEntity> ppts = Collections.synchronizedList(new ArrayList<>());
for (int i = 1; i <= tnum; i++) {
// 获取范围
int begin = (i - 1) * offset;
int end = i * offset;
end = i == tnum ? pageCount : end;
PPTThread thread = new PPTThread(ppts);
thread.setNumBegin(begin);
thread.setNumEnd(end);
thread.setName("work-thread-" + i);
thread.start();
threads.add(thread);
}
new Thread(() -> {
while (true) {
// 定时检测状态
log.debug("----------------------------------------");
threads.forEach(v -> {
log.debug("线程名:" + v.getName());
log.debug("任务总数:" + (v.getNumEnd() - v.getNumBegin()));
log.debug("当前工作页码:" + (v.getCurrWork() - v.getNumBegin()));
log.debug("错误页码数:" + v.getErrorPage());
log.debug("错误索引数:" + v.getErrorIndex() + "\n");
});
log.debug("----------------------------------------");
long notComplete = threads.stream().filter(v -> !v.isComplete()).count();
if(notComplete == 0){
long t2 = System.currentTimeMillis();
log.debug("全部完成,耗时:"+(t2-t1)+"毫秒");
ppts.sort((o1, o2) -> o2.getUpdateTime().compareTo(o1.getUpdateTime()));
printStream.println(JSON.toJSONString(ppts,PrettyFormat));
break;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}