自定义线程增加相关处理接口,并增加与前端交互的业务逻辑接口

This commit is contained in:
wanglei 2025-09-15 17:40:24 +08:00
parent 5abb24c95a
commit cddf5e66cc
5 changed files with 213 additions and 17 deletions

View File

@ -4,13 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数">
<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$/src/main/java/com/simulationservice/service/DemandThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" 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/service/WasterThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WasterThread.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" />
@ -79,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": {
@ -127,7 +125,8 @@
<workItem from="1757594198944" duration="2502000" />
<workItem from="1757664586037" duration="3070000" />
<workItem from="1757820561153" duration="249000" />
<workItem from="1757829080725" duration="24265000" />
<workItem from="1757829080725" duration="24604000" />
<workItem from="1757922989495" duration="5398000" />
</task>
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
<option name="closed" value="true" />
@ -153,7 +152,15 @@
<option name="project" value="LOCAL" />
<updated>1757899861547</updated>
</task>
<option name="localTasksCounter" value="4" />
<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">
@ -174,7 +181,8 @@
<MESSAGE value="修改配置文件和去掉打包时test模块" />
<MESSAGE value="打包库文件到jar包" />
<MESSAGE value="增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数" />
<option name="LAST_COMMIT_MESSAGE" value="增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数" />
<MESSAGE value="修改线程继承自定义线程相关类错误问题" />
<option name="LAST_COMMIT_MESSAGE" value="修改线程继承自定义线程相关类错误问题" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />

View File

@ -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;
}
}

View File

@ -10,31 +10,52 @@ import org.springframework.stereotype.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() {
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));
}
}

View File

@ -1,5 +1,4 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -11,8 +10,21 @@ import com.simulationservice.util.CustomThread;
*/
public class TimeSyncThread extends CustomThread {
private String beginTime; //开始时间
private long continueTime; //持续时间
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() {
@ -26,15 +38,137 @@ public class TimeSyncThread extends CustomThread {
//String dateString = "2023-04-01 12:00:00";
try {
Date dateT = sdf.parse(beginTime);
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) {
this.beginTime = 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;
}
}

View File

@ -10,6 +10,7 @@ public abstract class CustomThread extends Thread {
private String scenarioId = ""; //想定Id
private String roomId = ""; //房间号
public int max = 1000;
public int time = 1000; //快进和快退时间单位毫秒
@Override
public void run() {
@ -19,11 +20,11 @@ public abstract class CustomThread extends Thread {
if (exit)
break;
if (running == false) {
Thread.sleep(100);
Thread.sleep(time);
continue;
}
processBuss();
Thread.sleep(100);
Thread.sleep(time);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (Throwable t) {
@ -61,4 +62,8 @@ public abstract class CustomThread extends Thread {
public void closeThread() {
exit = true;
}
public void setTime(int time) {
this.time = time;
}
}