增加处理业务线程,简单编写部分业务逻辑,增加自定义类,用于实现线程通用函数

This commit is contained in:
wanglei 2025-09-15 09:30:56 +08:00
parent bc243e27b1
commit 5eb448aaa8
8 changed files with 231 additions and 7 deletions

View File

@ -4,9 +4,10 @@
<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="打包库文件到jar包">
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" afterDir="false" />
<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/service/InferenceTaskService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -122,7 +123,7 @@
<workItem from="1757594198944" duration="2502000" />
<workItem from="1757664586037" duration="3070000" />
<workItem from="1757820561153" duration="249000" />
<workItem from="1757829080725" duration="4245000" />
<workItem from="1757829080725" duration="23780000" />
</task>
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
<option name="closed" value="true" />
@ -132,7 +133,15 @@
<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>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -151,7 +160,8 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="修改配置文件和去掉打包时test模块" />
<option name="LAST_COMMIT_MESSAGE" value="修改配置文件和去掉打包时test模块" />
<MESSAGE value="打包库文件到jar包" />
<option name="LAST_COMMIT_MESSAGE" value="打包库文件到jar包" />
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />

View File

@ -0,0 +1,23 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import com.simulationservice.service.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);
}
}
}

View File

@ -9,15 +9,32 @@ import org.springframework.stereotype.Service;
@Service
public class InferenceTaskService {
private DemandThread demandThread;
@Async
public boolean loadScenario(String roomId, String scenarioId) {
//
demandThread = new DemandThread();
demandThread.setRoomId(roomId);
demandThread.setScenarioId(scenarioId);
demandThread.start();
return true;
}
@Async
public void executeTask() {
// 启动我们的任务
//new MyTask().run();
demandThread.startThread();
}
@Async
public void stopTask() {
// 停止我们的任务
demandThread.stopThread();
}
@Async
public void closeTask() {
// 停止我们的任务
demandThread.closeThread();
}
}

View File

@ -0,0 +1,24 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import com.simulationservice.service.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);
}
}
}

View File

@ -0,0 +1,23 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import com.simulationservice.service.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);
}
}
}

View File

@ -0,0 +1,40 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.simulationservice.service.CustomThread;
/**
* 时间同步线程用于处理想定的倒计时和想定时间结束
*/
public class TimeSyncThread extends CustomThread {
private String beginTime; //开始时间
private long continueTime; //持续时间
@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);
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
}

View File

@ -0,0 +1,23 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import com.simulationservice.service.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);
}
}
}

View File

@ -0,0 +1,64 @@
package com.simulationservice.service;
import java.lang.*;
/**
* 需求消耗线程用于各个作战单元的需求消耗
*/
public abstract class CustomThread extends Thread {
private boolean running = false;
private boolean exit = false;
private String scenarioId = ""; //想定Id
private String roomId = ""; //房间号
int max = 1000;
@Override
public void run() {
//此处让这个新线程每隔0.1s执行一次打印操作
while (true) {
try {
if (exit)
break;
if (running == false) {
Thread.sleep(100);
continue;
}
processBuss();
Thread.sleep(100);
} 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;
}
}