Code cleanup
This commit is contained in:
parent
3b24ee0531
commit
8c7c37c9c0
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
package ru.windcorp.progressia;
|
package ru.windcorp.progressia;
|
||||||
|
|
||||||
import ru.windcorp.progressia.common.modules.TaskManager;
|
|
||||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||||
import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer;
|
import ru.windcorp.progressia.common.util.crash.analyzers.OutOfMemoryAnalyzer;
|
||||||
import ru.windcorp.progressia.common.util.crash.providers.*;
|
import ru.windcorp.progressia.common.util.crash.providers.*;
|
||||||
|
@ -31,13 +31,14 @@ public class AudioSystem {
|
|||||||
Thread shutdownHook = new Thread(AudioManager::closeAL, "AL Shutdown Hook");
|
Thread shutdownHook = new Thread(AudioManager::closeAL, "AL Shutdown Hook");
|
||||||
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
||||||
|
|
||||||
Task t = new Task("AudioSystem:Initialize", audioModule) {
|
Task t = new Task("AudioSystem:Initialize") {
|
||||||
@Override
|
@Override
|
||||||
protected void perform() {
|
protected void perform() {
|
||||||
loadAudioData();
|
loadAudioData();
|
||||||
LogManager.getLogger().info("Audio data is loaded");
|
LogManager.getLogger().info("Audio data is loaded");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
audioModule.addTask(t);
|
||||||
TaskManager.getInstance().registerModule(audioModule);
|
TaskManager.getInstance().registerModule(audioModule);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,8 +25,6 @@ public class Module extends Namespaced {
|
|||||||
|
|
||||||
private final Set<Task> tasks = new HashSet<>();
|
private final Set<Task> tasks = new HashSet<>();
|
||||||
private final Map<String, String> meta = new HashMap<>();
|
private final Map<String, String> meta = new HashMap<>();
|
||||||
private final Map<String, String> unmodifiableMeta = Collections.unmodifiableMap(meta);
|
|
||||||
private final Set<Task> unmodifiableTasks = Collections.unmodifiableSet(tasks);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param id the identifier of a task object.
|
* @param id the identifier of a task object.
|
||||||
@ -42,11 +40,11 @@ public class Module extends Namespaced {
|
|||||||
* @return meta information of the module as {@link Map}.
|
* @return meta information of the module as {@link Map}.
|
||||||
*/
|
*/
|
||||||
public Map<String, String> getMeta() {
|
public Map<String, String> getMeta() {
|
||||||
return unmodifiableMeta;
|
return Collections.unmodifiableMap(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Task> getTasks() {
|
public Set<Task> getTasks() {
|
||||||
return unmodifiableTasks;
|
return Collections.unmodifiableSet(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,9 +21,7 @@ import ru.windcorp.jputil.chars.StringUtil;
|
|||||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public abstract class Task
|
public abstract class Task
|
||||||
@ -40,28 +38,18 @@ public abstract class Task
|
|||||||
* Its format is restricted by {@link Namespaced}.
|
* Its format is restricted by {@link Namespaced}.
|
||||||
* @see Namespaced#Namespaced
|
* @see Namespaced#Namespaced
|
||||||
*/
|
*/
|
||||||
public Task(String id) {
|
protected Task(String id) {
|
||||||
super(id);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param id the identifier of a task object.
|
|
||||||
* Its format is restricted by {@link Namespaced}.
|
|
||||||
* @param module to which the task will be attached.
|
|
||||||
* @see Namespaced#Namespaced
|
|
||||||
*/
|
|
||||||
public Task(String id, Module module) {
|
|
||||||
this(id);
|
|
||||||
module.addTask(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (!canRun()) {
|
if (!canRun()) {
|
||||||
ArrayList<Task> undoneTasks = new ArrayList<>();
|
List<Task> undoneTasks = new ArrayList<>();
|
||||||
for (Task j : requiredTasks) {
|
for (Iterator<Task> iterator = requiredTasks.iterator(); iterator.hasNext(); ) {
|
||||||
if (!j.isDone()) {
|
Task t = iterator.next();
|
||||||
undoneTasks.add(j);
|
if (!t.isDone()) {
|
||||||
|
undoneTasks.add(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,14 +91,15 @@ public abstract class Task
|
|||||||
*/
|
*/
|
||||||
public boolean canRun() {
|
public boolean canRun() {
|
||||||
if (this.isActive.get() || isDone.get()) return false;
|
if (this.isActive.get() || isDone.get()) return false;
|
||||||
for (Task reqT : requiredTasks) {
|
for (Iterator<Task> iterator = requiredTasks.iterator(); iterator.hasNext(); ) {
|
||||||
if (!reqT.isDone()) return false;
|
Task reqTask = iterator.next();
|
||||||
|
if (!reqTask.isDone()) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Task> getRequiredTasks() {
|
public Set<Task> getRequiredTasks() {
|
||||||
return requiredTasks;
|
return Collections.unmodifiableSet(requiredTasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRequiredTask(Task task) {
|
public void addRequiredTask(Task task) {
|
||||||
@ -130,10 +119,10 @@ public abstract class Task
|
|||||||
*/
|
*/
|
||||||
public void setOwner(Module module) {
|
public void setOwner(Module module) {
|
||||||
if (owner != null) {
|
if (owner != null) {
|
||||||
CrashReports.crash(
|
throw CrashReports.crash(
|
||||||
new Throwable()
|
null
|
||||||
, "Could not set %s as owner of %s, because %s is already owner of it.",
|
, "Could not set %s as owner of %s, because %s is already owner of it.",
|
||||||
module.getId(), this.getId(), this.getOwner().getId());
|
module.getId(), this.getId(), this.owner.getId());
|
||||||
} else {
|
} else {
|
||||||
owner = module;
|
owner = module;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ package ru.windcorp.progressia.common.modules;
|
|||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
import ru.windcorp.progressia.common.util.crash.CrashReports;
|
||||||
|
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@ -27,18 +28,17 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||||||
|
|
||||||
import static java.util.concurrent.Executors.newFixedThreadPool;
|
import static java.util.concurrent.Executors.newFixedThreadPool;
|
||||||
|
|
||||||
public class TaskManager {
|
public final class TaskManager {
|
||||||
private static final TaskManager instance = new TaskManager();
|
private static final TaskManager INSTANCE = new TaskManager();
|
||||||
|
|
||||||
private final Set<Task> tasks = new HashSet<>();
|
private final Set<Task> tasks = new HashSet<>();
|
||||||
private final Set<Module> modules = new HashSet<>();
|
private final Set<Module> modules = new HashSet<>();
|
||||||
private final ExecutorService executorService;
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
private final AtomicBoolean loadingDone;
|
final AtomicBoolean loadingDone;
|
||||||
private final AtomicInteger activeThreadsCount;
|
final AtomicInteger activeThreadsCount;
|
||||||
|
|
||||||
private final Map<Thread, Task> loadersMonitorMap;
|
private final Map<Thread, Namespaced> loadersMonitorMap;
|
||||||
Map<Thread, Task> unmodifiableLoadersMonitorMap;
|
|
||||||
|
|
||||||
private TaskManager() {
|
private TaskManager() {
|
||||||
loadingDone = new AtomicBoolean(false);
|
loadingDone = new AtomicBoolean(false);
|
||||||
@ -46,11 +46,10 @@ public class TaskManager {
|
|||||||
executorService = newFixedThreadPool(
|
executorService = newFixedThreadPool(
|
||||||
Runtime.getRuntime().availableProcessors(), Thread::new);
|
Runtime.getRuntime().availableProcessors(), Thread::new);
|
||||||
loadersMonitorMap = new HashMap<>(Runtime.getRuntime().availableProcessors());
|
loadersMonitorMap = new HashMap<>(Runtime.getRuntime().availableProcessors());
|
||||||
unmodifiableLoadersMonitorMap = Collections.unmodifiableMap(loadersMonitorMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TaskManager getInstance() {
|
public static TaskManager getInstance() {
|
||||||
return instance;
|
return TaskManager.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,40 +82,14 @@ public class TaskManager {
|
|||||||
*/
|
*/
|
||||||
public void startLoading() {
|
public void startLoading() {
|
||||||
LogManager.getLogger().info("Loading is started");
|
LogManager.getLogger().info("Loading is started");
|
||||||
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
|
int procAmount = Runtime.getRuntime().availableProcessors();
|
||||||
executorService.submit(() -> {
|
for (int i = 0; i < procAmount; i++) {
|
||||||
while (!loadingDone.get()) {
|
executorService.submit(loaderTask);
|
||||||
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();
|
|
||||||
}
|
|
||||||
} else if (activeThreadsCount.get() > 0) {
|
|
||||||
try {
|
|
||||||
synchronized (this) {
|
|
||||||
this.wait();
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
loadingDone.set(true);
|
|
||||||
synchronized (this) {
|
|
||||||
notifyAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
waitForLoadingEnd();
|
waitForLoadingEnd();
|
||||||
if (!tasks.isEmpty()) {
|
if (!tasks.isEmpty()) {
|
||||||
throw CrashReports.crash(new Exception("Loading is failed"), "");
|
throw CrashReports.crash(null, "Loading is failed");
|
||||||
}
|
}
|
||||||
LogManager.getLogger().info("Loading is finished");
|
LogManager.getLogger().info("Loading is finished");
|
||||||
executorService.shutdownNow();
|
executorService.shutdownNow();
|
||||||
@ -125,12 +98,12 @@ public class TaskManager {
|
|||||||
/**
|
/**
|
||||||
* @return Task - founded registered task with {@link Task#canRun()} = true;
|
* @return Task - founded registered task with {@link Task#canRun()} = true;
|
||||||
* null - there is no available task found.
|
* null - there is no available task found.
|
||||||
* @see Task#canRun()
|
* @see Task#canRun()
|
||||||
*/
|
*/
|
||||||
private synchronized Task getRunnableTask() {
|
synchronized Task getRunnableTask() {
|
||||||
if (!tasks.isEmpty()) {
|
if (!tasks.isEmpty()) {
|
||||||
for (Task t :
|
for (Iterator<Task> iterator = tasks.iterator(); iterator.hasNext(); ) {
|
||||||
tasks) {
|
Task t = iterator.next();
|
||||||
if (t.canRun()) {
|
if (t.canRun()) {
|
||||||
tasks.remove(t);
|
tasks.remove(t);
|
||||||
return t;
|
return t;
|
||||||
@ -145,24 +118,56 @@ public class TaskManager {
|
|||||||
* to wait until the loading is not done.
|
* to wait until the loading is not done.
|
||||||
*/
|
*/
|
||||||
private void waitForLoadingEnd() {
|
private void waitForLoadingEnd() {
|
||||||
synchronized (this) {
|
synchronized (tasks) {
|
||||||
while (!loadingDone.get()) {
|
while (!loadingDone.get()) {
|
||||||
try {
|
try {
|
||||||
this.wait();
|
tasks.wait();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException ignored) {}
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void stopLoading() {
|
||||||
|
loadingDone.set(true);
|
||||||
|
synchronized (tasks) {
|
||||||
|
tasks.notifyAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a map where key is a thread making loading
|
* @return a map where key is a thread making loading
|
||||||
* and where value is a task that is being performed by it
|
* and where value is a {@link Namespaced} of {@link Task} that is being performed by it
|
||||||
* at the moment.
|
* at the moment.
|
||||||
|
* @see Namespaced
|
||||||
*/
|
*/
|
||||||
public Map<Thread, Task> getLoadersMonitorMap() {
|
public Map<Thread, Namespaced> getLoadersMonitorMap() {
|
||||||
return unmodifiableLoadersMonitorMap;
|
return Collections.unmodifiableMap(loadersMonitorMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final Runnable loaderTask = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (!loadingDone.get()) {
|
||||||
|
Task t = getRunnableTask();
|
||||||
|
if (t != null) {
|
||||||
|
activeThreadsCount.incrementAndGet();
|
||||||
|
loadersMonitorMap.put(Thread.currentThread(), t);
|
||||||
|
t.run();
|
||||||
|
loadersMonitorMap.put(Thread.currentThread(), null);
|
||||||
|
activeThreadsCount.decrementAndGet();
|
||||||
|
synchronized (tasks) {
|
||||||
|
tasks.notifyAll();
|
||||||
|
}
|
||||||
|
} else if (activeThreadsCount.get() > 0) {
|
||||||
|
try {
|
||||||
|
synchronized (tasks) {
|
||||||
|
tasks.wait();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException ignored) {}
|
||||||
|
} else {
|
||||||
|
stopLoading();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user