Loaders are observable. Modules refactoring
- Now it is possible to monitor loaders and tasks they perform - Now it is NOT possible to add dublicate tasks and modules
This commit is contained in:
parent
f520d4b2c6
commit
c7842935ba
@ -31,14 +31,13 @@ public class AudioSystem {
|
||||
Thread shutdownHook = new Thread(AudioManager::closeAL, "AL Shutdown Hook");
|
||||
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
||||
|
||||
Task t = new Task("AudioSystem:Initialize") {
|
||||
Task t = new Task("AudioSystem:Initialize", audioModule) {
|
||||
@Override
|
||||
protected void perform() {
|
||||
loadAudioData();
|
||||
LogManager.getLogger().info("Audio data is loaded");
|
||||
}
|
||||
};
|
||||
audioModule.addTask(t);
|
||||
TaskManager.getInstance().registerModule(audioModule);
|
||||
}
|
||||
|
||||
|
@ -2,16 +2,13 @@ package ru.windcorp.progressia.common.modules;
|
||||
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class Module extends Namespaced {
|
||||
|
||||
private final List<Task> tasks = new ArrayList<>();
|
||||
private final Map<String, String> meta = new HashMap<>();
|
||||
private final boolean done = false;
|
||||
private final Map<String, String> unmodifiableMeta = Collections.unmodifiableMap(meta);
|
||||
|
||||
|
||||
public Module(String id) {
|
||||
@ -19,7 +16,7 @@ public class Module extends Namespaced {
|
||||
}
|
||||
|
||||
public Map<String, String> getMeta() {
|
||||
return meta;
|
||||
return unmodifiableMeta;
|
||||
}
|
||||
|
||||
public List<Task> getTasks() {
|
||||
@ -27,19 +24,8 @@ public class Module extends Namespaced {
|
||||
}
|
||||
|
||||
public void addTask(Task task) {
|
||||
task.setOwner(this);
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false - not all tasks are done
|
||||
*/
|
||||
public boolean done() {
|
||||
for (Task t : tasks) {
|
||||
if (!t.isDone()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.windcorp.progressia.common.modules;
|
||||
|
||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -12,11 +13,17 @@ public abstract class Task
|
||||
List<Task> requiredTasks = new ArrayList<>();
|
||||
private boolean isDone = false;
|
||||
private boolean isActive = false;
|
||||
private Module owner;
|
||||
|
||||
protected Task(String id) {
|
||||
public Task(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public Task(String id, Module module) {
|
||||
this(id);
|
||||
module.addTask(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
isActive = true;
|
||||
@ -50,4 +57,19 @@ public abstract class Task
|
||||
public void addRequiredTask(Task task) {
|
||||
requiredTasks.add(task);
|
||||
}
|
||||
|
||||
public void setOwner(Module module) {
|
||||
if(owner != null) {
|
||||
CrashReports.crash(
|
||||
new Exception("Owner is not null")
|
||||
, "Could not set %s as owner of %s, because %s is already owner of it.",
|
||||
module.getId(), this.getId(), this.getOwner().getId());
|
||||
} else {
|
||||
owner = module;
|
||||
}
|
||||
}
|
||||
|
||||
public Module getOwner() {
|
||||
return owner;
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
package ru.windcorp.progressia.common.modules;
|
||||
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -14,17 +12,24 @@ import static java.util.concurrent.Executors.newFixedThreadPool;
|
||||
|
||||
public class TaskManager {
|
||||
private static final TaskManager instance = new TaskManager();
|
||||
private final List<Task> tasks = new ArrayList<>();
|
||||
private final List<Module> modules = new ArrayList<>();
|
||||
|
||||
private final Set<Task> tasks = new HashSet<>();
|
||||
private final Set<Module> modules = new HashSet<>();
|
||||
private final ExecutorService executorService;
|
||||
|
||||
private final AtomicBoolean loadingDone;
|
||||
private final AtomicInteger activeThreadsCount;
|
||||
|
||||
private final Map<Thread, Task> loadersMonitorMap;
|
||||
Map<Thread, Task> unmodifiableLoadersMonitorMap;
|
||||
|
||||
private TaskManager() {
|
||||
loadingDone = new AtomicBoolean(false);
|
||||
activeThreadsCount = new AtomicInteger(0);
|
||||
executorService = newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors(), Thread::new);
|
||||
loadersMonitorMap = new HashMap<>(Runtime.getRuntime().availableProcessors());
|
||||
unmodifiableLoadersMonitorMap = Collections.unmodifiableMap(loadersMonitorMap);
|
||||
}
|
||||
|
||||
public static TaskManager getInstance() {
|
||||
@ -48,7 +53,9 @@ public class TaskManager {
|
||||
Task t = getRunnableTask();
|
||||
if (t != null) {
|
||||
activeThreadsCount.incrementAndGet();
|
||||
loadersMonitorMap.put(Thread.currentThread(), t);
|
||||
t.run();
|
||||
loadersMonitorMap.put(Thread.currentThread(), null);
|
||||
activeThreadsCount.decrementAndGet();
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
@ -104,4 +111,8 @@ public class TaskManager {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Thread, Task> getLoadersMonitorMap() {
|
||||
return unmodifiableLoadersMonitorMap;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user