任务相关
This commit is contained in:
parent
84256158c3
commit
b03c0bcf23
|
|
@ -6,7 +6,6 @@ import com.hivekion.room.func.TaskAction;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [类的简要说明]
|
* [类的简要说明]
|
||||||
|
|
@ -62,10 +61,19 @@ public class RoomManager {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
public static void addFuture(ScheduledExecutorService future,String roomId){
|
|
||||||
|
public static void addFuture(ScheduledExecutorService future, String roomId) {
|
||||||
Room room = roomsMap.get(roomId);
|
Room room = roomsMap.get(roomId);
|
||||||
if (room != null) {
|
if (room != null) {
|
||||||
room.addTaskReference(future);
|
room.addTaskReference(future);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isRunning(String id) {
|
||||||
|
Room room = roomsMap.get(id);
|
||||||
|
if (room != null) {
|
||||||
|
return room.isRunning();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,11 +66,15 @@ public abstract class AbtParentTask implements TaskAction {
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return scenarioTask.getTaskType();
|
return scenarioTask.getTaskType();
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取房间的持续时间
|
//获取房间的持续时间
|
||||||
public long getDuringTime() {
|
public long getDuringTime() {
|
||||||
return RoomManager.getRoomDuringTime(this.roomId);
|
return RoomManager.getRoomDuringTime(this.roomId);
|
||||||
}
|
}
|
||||||
|
//获取房间状态
|
||||||
|
public boolean getRoomStatus() {
|
||||||
|
return RoomManager.isRunning(roomId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义线程工厂
|
// 自定义线程工厂
|
||||||
|
|
@ -90,4 +94,5 @@ class CustomThreadFactory implements ThreadFactory {
|
||||||
thread.setPriority(Thread.NORM_PRIORITY);
|
thread.setPriority(Thread.NORM_PRIORITY);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -104,8 +104,10 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
||||||
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
||||||
1);
|
1);
|
||||||
schedule.scheduleWithFixedDelay(() -> {
|
schedule.scheduleWithFixedDelay(() -> {
|
||||||
|
long duringTime = getDuringTime();
|
||||||
|
//
|
||||||
|
double distance = duringTime * SPEED;
|
||||||
|
|
||||||
|
|
||||||
}, 0, 1, TimeUnit.SECONDS);
|
}, 0, 1, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ThreadPoolExecutor;
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
@ -37,6 +38,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Room implements AutoCloseable {
|
public class Room implements AutoCloseable {
|
||||||
|
|
||||||
|
private AtomicBoolean status = new AtomicBoolean(false);
|
||||||
/**
|
/**
|
||||||
* 任务管理相关
|
* 任务管理相关
|
||||||
*/
|
*/
|
||||||
|
|
@ -57,7 +59,7 @@ public class Room implements AutoCloseable {
|
||||||
//日期格式化
|
//日期格式化
|
||||||
private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
private DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
//房间中关联的任务管理器
|
//房间中关联的任务管理器
|
||||||
private Map<String,ScheduledExecutorService> futures = new ConcurrentHashMap<>();
|
private Map<String, ScheduledExecutorService> futures = new ConcurrentHashMap<>();
|
||||||
//线程池
|
//线程池
|
||||||
private final ExecutorService actionExecutor =
|
private final ExecutorService actionExecutor =
|
||||||
new ThreadPoolExecutor(
|
new ThreadPoolExecutor(
|
||||||
|
|
@ -87,7 +89,7 @@ public class Room implements AutoCloseable {
|
||||||
* @param time 总时间
|
* @param time 总时间
|
||||||
*/
|
*/
|
||||||
public void start(long time) {
|
public void start(long time) {
|
||||||
|
status.set(true);
|
||||||
totalTime.set(time);
|
totalTime.set(time);
|
||||||
startTask();
|
startTask();
|
||||||
}
|
}
|
||||||
|
|
@ -96,6 +98,7 @@ public class Room implements AutoCloseable {
|
||||||
* 停止
|
* 停止
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
status.set(false);
|
||||||
cancelTask();
|
cancelTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,10 +106,12 @@ public class Room implements AutoCloseable {
|
||||||
* 暂停
|
* 暂停
|
||||||
*/
|
*/
|
||||||
public void pause() {
|
public void pause() {
|
||||||
|
status.set(false);
|
||||||
cancelTask();
|
cancelTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resume() {
|
public void resume() {
|
||||||
|
status.set(true);
|
||||||
startTask();
|
startTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -188,4 +193,8 @@ public class Room implements AutoCloseable {
|
||||||
public void addTaskReference(ScheduledExecutorService scheduledExecutorService) {
|
public void addTaskReference(ScheduledExecutorService scheduledExecutorService) {
|
||||||
futures.put(IdUtils.simpleUUID(), scheduledExecutorService);
|
futures.put(IdUtils.simpleUUID(), scheduledExecutorService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isRunning() {
|
||||||
|
return status.get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user