任务相关
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.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* [类的简要说明]
|
||||
|
@ -62,10 +61,19 @@ public class RoomManager {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
public static void addFuture(ScheduledExecutorService future,String roomId){
|
||||
|
||||
public static void addFuture(ScheduledExecutorService future, String roomId) {
|
||||
Room room = roomsMap.get(roomId);
|
||||
if (room != null) {
|
||||
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() {
|
||||
return scenarioTask.getTaskType();
|
||||
}
|
||||
|
||||
//获取房间的持续时间
|
||||
public long getDuringTime() {
|
||||
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);
|
||||
return thread;
|
||||
}
|
||||
|
||||
}
|
|
@ -104,8 +104,10 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
||||
1);
|
||||
schedule.scheduleWithFixedDelay(() -> {
|
||||
|
||||
|
||||
long duringTime = getDuringTime();
|
||||
//
|
||||
double distance = duringTime * SPEED;
|
||||
|
||||
|
||||
}, 0, 1, TimeUnit.SECONDS);
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
|||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -37,6 +38,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Slf4j
|
||||
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 Map<String,ScheduledExecutorService> futures = new ConcurrentHashMap<>();
|
||||
private Map<String, ScheduledExecutorService> futures = new ConcurrentHashMap<>();
|
||||
//线程池
|
||||
private final ExecutorService actionExecutor =
|
||||
new ThreadPoolExecutor(
|
||||
|
@ -87,7 +89,7 @@ public class Room implements AutoCloseable {
|
|||
* @param time 总时间
|
||||
*/
|
||||
public void start(long time) {
|
||||
|
||||
status.set(true);
|
||||
totalTime.set(time);
|
||||
startTask();
|
||||
}
|
||||
|
@ -96,6 +98,7 @@ public class Room implements AutoCloseable {
|
|||
* 停止
|
||||
*/
|
||||
public void stop() {
|
||||
status.set(false);
|
||||
cancelTask();
|
||||
}
|
||||
|
||||
|
@ -103,10 +106,12 @@ public class Room implements AutoCloseable {
|
|||
* 暂停
|
||||
*/
|
||||
public void pause() {
|
||||
status.set(false);
|
||||
cancelTask();
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
status.set(true);
|
||||
startTask();
|
||||
}
|
||||
|
||||
|
@ -188,4 +193,8 @@ public class Room implements AutoCloseable {
|
|||
public void addTaskReference(ScheduledExecutorService scheduledExecutorService) {
|
||||
futures.put(IdUtils.simpleUUID(), scheduledExecutorService);
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return status.get();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user