Compare commits
3 Commits
bc243e27b1
...
cddf5e66cc
Author | SHA1 | Date | |
---|---|---|---|
cddf5e66cc | |||
5abb24c95a | |||
5eb448aaa8 |
|
@ -4,9 +4,12 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="修改配置文件和去掉打包时test模块">
|
||||
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="修改线程继承自定义线程相关类错误问题">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
|
@ -74,7 +77,7 @@
|
|||
"project.structure.last.edited": "问题",
|
||||
"project.structure.proportion": "0.15",
|
||||
"project.structure.side.proportion": "0.37011495",
|
||||
"settings.editor.selected.configurable": "MavenSettings",
|
||||
"settings.editor.selected.configurable": "vcs.Git",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
},
|
||||
"keyToStringList": {
|
||||
|
@ -122,7 +125,8 @@
|
|||
<workItem from="1757594198944" duration="2502000" />
|
||||
<workItem from="1757664586037" duration="3070000" />
|
||||
<workItem from="1757820561153" duration="249000" />
|
||||
<workItem from="1757829080725" duration="4245000" />
|
||||
<workItem from="1757829080725" duration="24604000" />
|
||||
<workItem from="1757922989495" duration="5398000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
|
||||
<option name="closed" value="true" />
|
||||
|
@ -132,7 +136,31 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1757833150037</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="2" />
|
||||
<task id="LOCAL-00002" summary="打包库文件到jar包">
|
||||
<option name="closed" value="true" />
|
||||
<created>1757833985847</created>
|
||||
<option name="number" value="00002" />
|
||||
<option name="presentableId" value="LOCAL-00002" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1757833985847</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00003" summary="增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数">
|
||||
<option name="closed" value="true" />
|
||||
<created>1757899861547</created>
|
||||
<option name="number" value="00003" />
|
||||
<option name="presentableId" value="LOCAL-00003" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1757899861547</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00004" summary="修改线程继承自定义线程相关类错误问题">
|
||||
<option name="closed" value="true" />
|
||||
<created>1757900269877</created>
|
||||
<option name="number" value="00004" />
|
||||
<option name="presentableId" value="LOCAL-00004" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1757900269877</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="5" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
@ -151,7 +179,10 @@
|
|||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<MESSAGE value="修改配置文件和去掉打包时test模块" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="修改配置文件和去掉打包时test模块" />
|
||||
<MESSAGE value="打包库文件到jar包" />
|
||||
<MESSAGE value="增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数" />
|
||||
<MESSAGE value="修改线程继承自定义线程相关类错误问题" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="修改线程继承自定义线程相关类错误问题" />
|
||||
</component>
|
||||
<component name="XSLT-Support.FileAssociations.UIState">
|
||||
<expand />
|
||||
|
|
|
@ -34,4 +34,32 @@ public class InferenceController {
|
|||
inferenceTaskService.executeTask();
|
||||
return "任务已启动";
|
||||
}
|
||||
|
||||
@GetMapping("/api/stopScenario")
|
||||
public String stopTask(@RequestParam(value = "roomId") String roomId, @RequestParam(value = "scenarioId") String scenarioId) {
|
||||
// 停止后台任务
|
||||
inferenceTaskService.stopTask();
|
||||
return "任务已停止";
|
||||
}
|
||||
|
||||
@GetMapping("/api/closeScenario")
|
||||
public String closeTask(@RequestParam(value = "roomId") String roomId, @RequestParam(value = "scenarioId") String scenarioId) {
|
||||
// 终止或者结束后台任务
|
||||
inferenceTaskService.closeTask();
|
||||
return "任务已终止";
|
||||
}
|
||||
|
||||
@GetMapping("/api/speedPlay")
|
||||
public String speedPlay(@RequestParam(value = "roomId") String roomId, @RequestParam(value = "scenarioId") String scenarioId, @RequestParam(value = "value") String value) {
|
||||
// 加速
|
||||
inferenceTaskService.speedPlay(value);
|
||||
return "加速执行 " + value;
|
||||
}
|
||||
|
||||
@GetMapping("/api/slowPlay")
|
||||
public String slowPlay(@RequestParam(value = "roomId") String roomId, @RequestParam(value = "scenarioId") String scenarioId, @RequestParam(value = "value") String value) {
|
||||
// 减速
|
||||
inferenceTaskService.slowPlay(value);
|
||||
return "减速执行 " + value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.simulationservice.service;
|
||||
import java.io.IOException;
|
||||
import java.lang.*;
|
||||
import com.simulationservice.util.CustomThread;
|
||||
/**
|
||||
* 生成保障需求线程,用于对各个作战单元的资源消耗进行判断产生需求
|
||||
*/
|
||||
public class DemandThread extends CustomThread {
|
||||
|
||||
@Override
|
||||
public void processBuss() {
|
||||
//进行业务逻辑处理
|
||||
if (max < 0) max = 1000;
|
||||
max--;
|
||||
String info = String.valueOf(max) + " " + getRoomId();
|
||||
try {
|
||||
WebSocketServer.sendInfo(info, "1111");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -9,15 +9,53 @@ import org.springframework.stereotype.Service;
|
|||
@Service
|
||||
public class InferenceTaskService {
|
||||
|
||||
private DemandThread demandThread;
|
||||
private TimeSyncThread timeSyncThread;
|
||||
@Async
|
||||
public boolean loadScenario(String roomId, String scenarioId) {
|
||||
|
||||
//保障需求线程执行
|
||||
demandThread = new DemandThread();
|
||||
demandThread.setRoomId(roomId);
|
||||
demandThread.setScenarioId(scenarioId);
|
||||
demandThread.start();
|
||||
//时间同步线程执行
|
||||
timeSyncThread = new TimeSyncThread();
|
||||
timeSyncThread.setRoomId(roomId);
|
||||
timeSyncThread.setScenarioId(scenarioId);
|
||||
timeSyncThread.start();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Async
|
||||
public void executeTask() {
|
||||
// 启动我们的任务
|
||||
//new MyTask().run();
|
||||
demandThread.startThread();
|
||||
timeSyncThread.startThread();
|
||||
}
|
||||
|
||||
@Async
|
||||
public void stopTask() {
|
||||
// 停止我们的任务
|
||||
demandThread.stopThread();
|
||||
timeSyncThread.stopThread();
|
||||
}
|
||||
|
||||
@Async
|
||||
public void closeTask() {
|
||||
// 停止我们的任务
|
||||
demandThread.closeThread();
|
||||
timeSyncThread.closeThread();
|
||||
}
|
||||
|
||||
@Async
|
||||
public void speedPlay(String value) {
|
||||
demandThread.setTime(Integer.parseInt(value));
|
||||
timeSyncThread.setTime(Integer.parseInt(value));
|
||||
}
|
||||
|
||||
@Async
|
||||
public void slowPlay(String value) {
|
||||
demandThread.setTime(Integer.parseInt(value));
|
||||
timeSyncThread.setTime(Integer.parseInt(value));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package com.simulationservice.service;
|
||||
import java.io.IOException;
|
||||
import java.lang.*;
|
||||
import com.simulationservice.util.CustomThread;
|
||||
/**
|
||||
* 任务分配线程,用于处理保障任务使用
|
||||
*/
|
||||
public class TaskAssignThread extends CustomThread {
|
||||
|
||||
@Override
|
||||
public void processBuss() {
|
||||
|
||||
//进行业务逻辑处理
|
||||
if (max < 0) max = 1000;
|
||||
max--;
|
||||
String info = String.valueOf(max) + " " + getRoomId();
|
||||
try {
|
||||
WebSocketServer.sendInfo(info, "3333");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
23
src/main/java/com/simulationservice/service/TaskThread.java
Normal file
23
src/main/java/com/simulationservice/service/TaskThread.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package com.simulationservice.service;
|
||||
import java.io.IOException;
|
||||
import java.lang.*;
|
||||
import com.simulationservice.util.CustomThread;
|
||||
/**
|
||||
* 保障任务线程,用于处理需求消耗生成的保障需求
|
||||
*/
|
||||
public class TaskThread extends CustomThread {
|
||||
|
||||
@Override
|
||||
public void processBuss() {
|
||||
|
||||
if (max < 0) max = 1000;
|
||||
max--;
|
||||
String info = String.valueOf(max) + " " + getRoomId();
|
||||
try {
|
||||
WebSocketServer.sendInfo(info, "2222");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
174
src/main/java/com/simulationservice/service/TimeSyncThread.java
Normal file
174
src/main/java/com/simulationservice/service/TimeSyncThread.java
Normal file
|
@ -0,0 +1,174 @@
|
|||
package com.simulationservice.service;
|
||||
import java.lang.*;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import com.simulationservice.util.CustomThread;
|
||||
/**
|
||||
* 时间同步线程,用于处理想定的倒计时和想定时间结束
|
||||
*/
|
||||
public class TimeSyncThread extends CustomThread {
|
||||
|
||||
private String _beginTime; //想定开始时间
|
||||
private int _continueTime; //持续时间
|
||||
|
||||
private int[] _monArray = new int[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
private String _resvTime = ""; //剩余时间
|
||||
private int _year;
|
||||
private int _month;
|
||||
private int _day;
|
||||
private int _hour;
|
||||
private int _min;
|
||||
private int _sec;
|
||||
|
||||
private int _residualHour;
|
||||
private int _residualMin;
|
||||
private int _residualSec;
|
||||
|
||||
@Override
|
||||
public void processBuss() {
|
||||
|
||||
//系统当前时间倒计时
|
||||
Date date = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String formattedDate = sdf.format(date);
|
||||
System.out.println(formattedDate);
|
||||
|
||||
//String dateString = "2023-04-01 12:00:00";
|
||||
|
||||
try {
|
||||
Date dateT = sdf.parse(_beginTime);
|
||||
System.out.println(dateT);
|
||||
|
||||
if (_residualHour <= 0 && _residualMin <= 0 && _residualSec <= 0)
|
||||
{
|
||||
//倒计时时间结束,处理时间结束业务逻辑
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_year % 4 == 0 && (_year % 100 != 0 || _year % 400 == 0))
|
||||
_monArray[1]++;
|
||||
_sec++;
|
||||
if (_sec >= 60)
|
||||
{
|
||||
_sec -= 60;
|
||||
_min++;
|
||||
if (_min == 60)
|
||||
{
|
||||
_min = 0;
|
||||
_hour++;
|
||||
if (_hour == 24)
|
||||
{
|
||||
_hour = 0;
|
||||
_day++;
|
||||
if (_day == 32)
|
||||
{
|
||||
_day = 1;
|
||||
_month++;
|
||||
if (_month == 13)
|
||||
{
|
||||
_month = 1;
|
||||
_year++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String month = String.format("%02d", _month);
|
||||
String day = String.format("%02d", _day);
|
||||
String hour = String.format("%02d", _hour);
|
||||
String min = String.format("%02d", _min);
|
||||
String sec = String.format("%02d", _sec);
|
||||
|
||||
if (_residualSec > 0)
|
||||
{
|
||||
_residualSec--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_residualMin > 0)
|
||||
{
|
||||
_residualSec = 59;
|
||||
_residualMin--;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_residualHour > 0)
|
||||
{
|
||||
_residualMin = 59;
|
||||
_residualSec = 59;
|
||||
_residualHour--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String residualHour = String.format("%02d", _residualHour);
|
||||
String residualMin = String.format("%02d", _residualMin);
|
||||
String residualSec = String.format("%02d", _residualSec);
|
||||
|
||||
String strTime = String.valueOf(_year) + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
|
||||
_resvTime = residualHour + ":" + residualMin + ":" + residualSec;
|
||||
System.out.println(getRoomId() + " " + strTime + " " + _resvTime);
|
||||
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void setBeginTime(String beginTime) {
|
||||
|
||||
String[] strArr = beginTime.split(" ");
|
||||
if (strArr.length == 2)
|
||||
{
|
||||
String strDate = strArr[0];
|
||||
String strTime = strArr[1];
|
||||
String[] strDateArr = strDate.split("-");
|
||||
String[] strTimeArr = strTime.split(":");
|
||||
if (strDateArr.length == 3)
|
||||
{
|
||||
_year = Integer.parseInt(strDateArr[0]);
|
||||
_month = Integer.parseInt(strDateArr[1]);
|
||||
_day = Integer.parseInt(strDateArr[2]);
|
||||
}
|
||||
if (strTimeArr.length == 3)
|
||||
{
|
||||
_hour = Integer.parseInt(strTimeArr[0]);
|
||||
_min = Integer.parseInt(strTimeArr[1]);
|
||||
_sec = Integer.parseInt(strTimeArr[2]);
|
||||
}
|
||||
}
|
||||
|
||||
this._beginTime = beginTime;
|
||||
}
|
||||
|
||||
public void setContinueTime(int continueTime) {
|
||||
int min = continueTime;
|
||||
if (min < 60)
|
||||
{
|
||||
_residualHour = 0;
|
||||
_residualMin = min;
|
||||
_residualSec = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
int hour = min / 60;
|
||||
if (hour >= 10)
|
||||
{
|
||||
_residualHour = hour;
|
||||
_residualMin = min - hour * 60;
|
||||
_residualSec = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_residualHour = hour;
|
||||
_residualMin = min - hour * 60;
|
||||
_residualSec = 0;
|
||||
}
|
||||
}
|
||||
this._continueTime = continueTime;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.simulationservice.service;
|
||||
import java.io.IOException;
|
||||
import java.lang.*;
|
||||
import com.simulationservice.util.CustomThread;
|
||||
/**
|
||||
* 作战分队物资消耗线程,用于各个作战单元的资源消耗
|
||||
*/
|
||||
public class WasterThread extends CustomThread {
|
||||
|
||||
@Override
|
||||
public void processBuss() {
|
||||
//进行业务逻辑处理
|
||||
if (max < 0) max = 1000;
|
||||
max--;
|
||||
String info = String.valueOf(max) + " " + getRoomId();
|
||||
try {
|
||||
WebSocketServer.sendInfo(info, "1111");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
69
src/main/java/com/simulationservice/util/CustomThread.java
Normal file
69
src/main/java/com/simulationservice/util/CustomThread.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package com.simulationservice.util;
|
||||
import java.lang.*;
|
||||
/**
|
||||
* 需求消耗线程,用于各个作战单元的需求消耗
|
||||
*/
|
||||
public abstract class CustomThread extends Thread {
|
||||
|
||||
private boolean running = false;
|
||||
private boolean exit = false;
|
||||
private String scenarioId = ""; //想定Id
|
||||
private String roomId = ""; //房间号
|
||||
public int max = 1000;
|
||||
public int time = 1000; //快进和快退时间,单位毫秒
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
//此处让这个新线程每隔0.1s执行一次打印操作
|
||||
while (true) {
|
||||
try {
|
||||
if (exit)
|
||||
break;
|
||||
if (running == false) {
|
||||
Thread.sleep(time);
|
||||
continue;
|
||||
}
|
||||
processBuss();
|
||||
Thread.sleep(time);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void processBuss();
|
||||
|
||||
public String getScenarioId() {
|
||||
return this.scenarioId;
|
||||
}
|
||||
|
||||
public void setScenarioId(String scenarioId) {
|
||||
this.scenarioId = scenarioId;
|
||||
}
|
||||
|
||||
public String getRoomId() {
|
||||
return this.roomId;
|
||||
}
|
||||
|
||||
public void setRoomId(String roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
|
||||
public void startThread() {
|
||||
running = true;
|
||||
}
|
||||
|
||||
public void stopThread() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
public void closeThread() {
|
||||
exit = true;
|
||||
}
|
||||
|
||||
public void setTime(int time) {
|
||||
this.time = time;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user