提交 6e6a0286 authored 作者: 张航's avatar 张航

优化了线程池的功能(之前资源占用率太高)

上级 785077ca
...@@ -3,9 +3,8 @@ package com.comleader.ldmapdownload.bean; ...@@ -3,9 +3,8 @@ package com.comleader.ldmapdownload.bean;
import com.comleader.ldmapdownload.util.OperationTypeEnum; import com.comleader.ldmapdownload.util.OperationTypeEnum;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.*;
import java.util.Timer; import java.util.concurrent.Future;
import java.util.TimerTask;
/** /**
* @ClassName ResultData * @ClassName ResultData
...@@ -80,21 +79,4 @@ public class SocketResultData implements Serializable { ...@@ -80,21 +79,4 @@ public class SocketResultData implements Serializable {
this.body = body; this.body = body;
} }
//public static void main(String[] args) throws InterruptedException {
//
// testTask("Task 1");
// testTask("Task 2");
//}
private static void testTask(String name) throws InterruptedException {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(name);
}
}, 0, 500);
Thread.sleep(2000);
timer.cancel();
}
} }
...@@ -34,7 +34,7 @@ public class DownMapService { ...@@ -34,7 +34,7 @@ public class DownMapService {
// 执行任务的线程池 // 执行任务的线程池
private ExecutorService es; private ExecutorService es;
private CompletionService<String> completionService; private CompletionService<Integer> completionService;
//private volatile Map<String, String> errResults = new HashMap<>(); //private volatile Map<String, String> errResults = new HashMap<>();
private List<String> errResults = new CopyOnWriteArrayList<>(); private List<String> errResults = new CopyOnWriteArrayList<>();
...@@ -79,7 +79,7 @@ public class DownMapService { ...@@ -79,7 +79,7 @@ public class DownMapService {
//记录返回结果 //记录返回结果
Map<String, Object> resBody = new HashMap<>(); Map<String, Object> resBody = new HashMap<>();
//记录异步结果 //记录异步结果
List<Future<String>> futures = new ArrayList<>(); List<Future<Integer>> futures = new ArrayList<>();
// 记录总用时 // 记录总用时
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
...@@ -106,20 +106,18 @@ public class DownMapService { ...@@ -106,20 +106,18 @@ public class DownMapService {
int minX = CLStringUtil.getOSMTileXFromLongitude(minLng, z); int minX = CLStringUtil.getOSMTileXFromLongitude(minLng, z);
int maxX = CLStringUtil.getOSMTileXFromLongitude(maxLng, z); int maxX = CLStringUtil.getOSMTileXFromLongitude(maxLng, z);
for (int x = minX; x <= maxX; x++) { // Y轴 for (int x = minX; x <= maxX; x++) { // Y轴
for (int y = minY; y <= maxY; y++) { // X轴
// 多线程异步执行下载 // 多线程异步执行下载
Future<String> resultFulture = completionService.submit(new DownMapCallable(z, x, y)); Future<Integer> resultFulture = completionService.submit(new DownMapCallable(z, x, minY, maxY));
// 加入集合中 // 加入集合中
futures.add(resultFulture); futures.add(resultFulture);
} }
} }
}
// 启动一个定时器,每秒刷新下载速度 // 启动一个定时器,每秒刷新下载速度
speedTimer = new Timer(); speedTimer = new Timer();
speedTimer.schedule(new TimerTask() { speedTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
if (DownMapService.stoped || DownMapService.finished) { if (DownMapService.stoped || DownMapService.finished || !session.isOpen()) {
speedTimer.cancel(); speedTimer.cancel();
return; return;
} }
...@@ -128,31 +126,37 @@ public class DownMapService { ...@@ -128,31 +126,37 @@ public class DownMapService {
}, 1000, 1000); }, 1000, 1000);
// 刷新进度 // 刷新进度
scheduleTimer = new Timer(); scheduleTimer = new Timer();
StringBuffer stringBuffer = new StringBuffer();
scheduleTimer.schedule(new TimerTask() { scheduleTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
if (DownMapService.stoped || DownMapService.finished) { if (DownMapService.stoped || DownMapService.finished || !session.isOpen()) {
scheduleTimer.cancel(); scheduleTimer.cancel();
return; return;
} }
DownMapService.schedule = countSuccessFile * 100 / readyCountFile; // 下载的进度 DownMapService.schedule = countSuccessFile * 100 / readyCountFile; // 下载的进度
stringBuffer.setLength(0); // 重新拼接进度
stringBuffer.append("\r当前进度:" + DownMapService.schedule + "%\t");
for (int i = 0; i < DownMapService.schedule; i++) {
stringBuffer.append("]");
}
stringBuffer.append(countSuccessFile +"/"+readyCountFile);
System.out.print(stringBuffer);
} }
}, 200, 200); }, 200, 200);
// 主线程阻塞等待执行完成 // 主线程阻塞等待执行完成
for (Future<String> future : futures) { for (Future<Integer> future : futures) {
if (DownMapService.stoped || !session.isOpen()) { if (DownMapService.stoped || !session.isOpen()) {
DownMapService.finished = true; DownMapService.finished = true;
// 如果发布了取消任务,则取消任务 // 如果发布了取消任务,则取消任务
stopDownLoad(futures); stopDownLoad(futures);
break; break;
} }
Future<String> take = completionService.take(); Future<Integer> take = completionService.take();
String result = take.get(); Integer result = take.get();
speed++; // 累计到下载进度上
countSuccessFile++;
System.out.println(result);
} }
// 完成标志 // 完成标志
DownMapService.finished = true; DownMapService.finished = true;
// 结束定时器 // 结束定时器
...@@ -166,14 +170,18 @@ public class DownMapService { ...@@ -166,14 +170,18 @@ public class DownMapService {
resBody.put("totalTime", (end - start) / 1000 + " s"); resBody.put("totalTime", (end - start) / 1000 + " s");
// 总文件大小 // 总文件大小
resBody.put("totalSize", CLStringUtil.getDownTotalSize()); resBody.put("totalSize", CLStringUtil.getDownTotalSize());
log.info("falidNum > "+errResults.size()); log.info("FalidList > ");
log.info("totalTime > "+(end - start) / 1000 + " s"); for (String errResult : errResults) {
log.info("totalSize > "+CLStringUtil.getDownTotalSize()); System.out.println(errResult + " DownLoadFaild");
}
log.info("falidNum > " + errResults.size());
log.info("totalTime > " + (end - start) / 1000 + " s");
log.info("totalSize > " + CLStringUtil.getDownTotalSize());
return resBody; return resBody;
} }
private void stopDownLoad(List<Future<String>> futures) { private void stopDownLoad(List<Future<Integer>> futures) {
for (Future<String> future : futures) { for (Future<Integer> future : futures) {
if (!future.isDone()) { if (!future.isDone()) {
future.cancel(false); future.cancel(false);
} }
...@@ -221,26 +229,29 @@ public class DownMapService { ...@@ -221,26 +229,29 @@ public class DownMapService {
* @author: zhanghang * @author: zhanghang
* @date: 2020/4/7 * @date: 2020/4/7
**/ **/
class DownMapCallable implements Callable<String> { class DownMapCallable implements Callable<Integer>, Cloneable {
private int z; private int z;
private int x; private int x;
private int y; private int minY;
private int maxY;
public DownMapCallable(int z, int x, int y) { public DownMapCallable(int z, int x, int minY, int maxY) {
this.z = z; this.z = z;
this.x = x; this.x = x;
this.y = y; this.minY = minY;
this.maxY = maxY;
} }
// 1: 下载完成 0: 地图已经下载过 2: 异常
@Override @Override
public String call() throws Exception { public Integer call() throws Exception {
String imgUrl = null; String imgUrl = null;
File file = null; File file = null;
for (int y = minY; y <= maxY; y++) {
try { try {
if (DownMapService.stoped) { // 停止下载的命令 if (DownMapService.stoped) { // 停止下载的命令
DownMapService.finished = true; DownMapService.finished = true;
return imgUrl + " Loaded";
} }
//高德地图(6:影像,7:矢量,8:影像路网) //高德地图(6:影像,7:矢量,8:影像路网)
imgUrl = CLStringUtil.getImgUrl(z, x, y); imgUrl = CLStringUtil.getImgUrl(z, x, y);
...@@ -249,17 +260,19 @@ public class DownMapService { ...@@ -249,17 +260,19 @@ public class DownMapService {
// 开始下载地图 // 开始下载地图
if (file != null) { if (file != null) {
HttpUtil.downImageByGet(imgUrl, file); HttpUtil.downImageByGet(imgUrl, file);
return imgUrl + " Success";
} }
return imgUrl + " Loaded";
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
if (file != null && file.exists()) { if (file != null && file.exists()) {
file.delete(); file.delete();
} }
errResults.add("Failed: " + imgUrl + " ErrorMsg >> " + e.getMessage()); errResults.add(imgUrl);
return imgUrl + " Down Failed"; log.info(e.getMessage());
}
speed++; // 累计到下载进度上
countSuccessFile++;
} }
return 1;
} }
} }
......
...@@ -96,6 +96,8 @@ public class DownLoadMapWebSocket { ...@@ -96,6 +96,8 @@ public class DownLoadMapWebSocket {
@OnClose @OnClose
public void OnClose() { public void OnClose() {
DownMapService.stoped = true; DownMapService.stoped = true;
DownMapService.finished = true;
DownMapService.isBusy = false;
log.info("[WebSocket] 退出成功"); log.info("[WebSocket] 退出成功");
} }
...@@ -177,7 +179,7 @@ public class DownLoadMapWebSocket { ...@@ -177,7 +179,7 @@ public class DownLoadMapWebSocket {
@Override @Override
public void run() { public void run() {
// 发送了停止命令或者已经完成 // 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) { if (DownMapService.stoped || DownMapService.finished || !session.isOpen()) {
DownMapService.isBusy = false; DownMapService.isBusy = false;
SocketResultData schedule = new SocketResultData(OperationTypeEnum.DOWNLOAD_SCHEDULE, 100 + "", null); SocketResultData schedule = new SocketResultData(OperationTypeEnum.DOWNLOAD_SCHEDULE, 100 + "", null);
AppointSending(JSONUtil.toJsonStr(schedule)); AppointSending(JSONUtil.toJsonStr(schedule));
...@@ -195,8 +197,8 @@ public class DownLoadMapWebSocket { ...@@ -195,8 +197,8 @@ public class DownLoadMapWebSocket {
@Override @Override
public void run() { public void run() {
// 发送了停止命令或者已经完成 // 发送了停止命令或者已经完成
if (DownMapService.stoped || DownMapService.finished) { if (DownMapService.stoped || DownMapService.finished || !session.isOpen()) {
speedTimer.purge(); speedTimer.cancel();
}else { }else {
// 发送速度 // 发送速度
SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null); SocketResultData speed = new SocketResultData(OperationTypeEnum.SYS_SUCCESS, "下载速度" + DownMapService.speed + "张/s", null);
......
...@@ -147,16 +147,16 @@ public class CLStringUtil { ...@@ -147,16 +147,16 @@ public class CLStringUtil {
public static String getDownTotalSize() { public static String getDownTotalSize() {
double size = HttpUtil.totalSize / 1024.0; double size = HttpUtil.totalSize / 1024.0;
if (size < 1024) { if (size < 1024) {
return new DecimalFormat("#,##").format(size) + "K"; return new DecimalFormat("#.##").format(size) + "K";
} else if (size < 1024 * 1024) { } else if (size < 1024 * 1024) {
size = size / 1024.0; size = size / 1024.0;
return new DecimalFormat("#,##").format(size) + "M"; return new DecimalFormat("#.##").format(size) + "M";
} else if (size < 1024 * 1024 * 1024) { } else if (size < 1024 * 1024 * 1024) {
size = size / 1024.0 / 1024.0; size = size / 1024.0 / 1024.0;
return new DecimalFormat("#,##").format(size) + "G"; return new DecimalFormat("#.##").format(size) + "G";
} else { } else {
size = size / 1024.0 / 1024.0 / 1024.0; size = size / 1024.0 / 1024.0 / 1024.0;
return new DecimalFormat("#,##").format(size) + "T"; return new DecimalFormat("#.##").format(size) + "T";
} }
} }
...@@ -196,15 +196,18 @@ public class CLStringUtil { ...@@ -196,15 +196,18 @@ public class CLStringUtil {
**/ **/
public static String countFileSize(int countFileNum) { public static String countFileSize(int countFileNum) {
// 平均每个图片20K // 平均每个图片20K
long countSize = countFileNum * 20; double size = countFileNum * 20;
if (countSize < 1024) { if (size < 1024) {
return countSize + "K"; return new DecimalFormat("#.##").format(size) + "K";
} else if (countSize < 1024 * 1024) { } else if (size < 1024 * 1024) {
countSize = countSize / 1024; size = size / 1024.0;
return countSize + "M"; return new DecimalFormat("#.##").format(size) + "M";
} else if (size < 1024 * 1024 * 1024) {
size = size / 1024.0 / 1024.0;
return new DecimalFormat("#.##").format(size) + "G";
} else { } else {
countSize = countSize / 1024 / 1024; size = size / 1024.0 / 1024.0 / 1024.0;
return countSize + "G"; return new DecimalFormat("#.##").format(size) + "T";
} }
} }
......
# 下载地图的保存路径 # 下载地图的保存路径
file.basepath=G:/em-map file.basepath=H:/em-map
# 项目的发布地址 # 项目的发布地址
file.mapImgPath=G:/em-map file.mapImgPath=H:/em-map
# 下载地图的地址 # 下载地图的地址
#天地图服务器t0-t8间选一个 #天地图服务器t0-t8间选一个
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论