TaskManager bug fixes and code cleanup
This commit is contained in:
parent
5a06788652
commit
f520d4b2c6
@ -18,6 +18,7 @@
|
||||
|
||||
package ru.windcorp.progressia.client.audio;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import ru.windcorp.progressia.common.modules.Module;
|
||||
import ru.windcorp.progressia.common.modules.Task;
|
||||
import ru.windcorp.progressia.common.modules.TaskManager;
|
||||
@ -29,10 +30,12 @@ public class AudioSystem {
|
||||
AudioManager.initAL();
|
||||
Thread shutdownHook = new Thread(AudioManager::closeAL, "AL Shutdown Hook");
|
||||
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
||||
Task t = new Task("Task:InitializeAudio") {
|
||||
|
||||
Task t = new Task("AudioSystem:Initialize") {
|
||||
@Override
|
||||
protected void perform() {
|
||||
loadAudioData();
|
||||
LogManager.getLogger().info("Audio data is loaded");
|
||||
}
|
||||
};
|
||||
audioModule.addTask(t);
|
||||
@ -41,9 +44,9 @@ public class AudioSystem {
|
||||
|
||||
static void loadAudioData() {
|
||||
AudioManager.loadSound(
|
||||
ResourceManager.getResource("assets/sounds/block_destroy_clap.ogg"),
|
||||
"Progressia:BlockDestroy",
|
||||
AudioFormat.MONO
|
||||
ResourceManager.getResource("assets/sounds/block_destroy_clap.ogg"),
|
||||
"Progressia:BlockDestroy",
|
||||
AudioFormat.MONO
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,12 @@
|
||||
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 ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
|
||||
public class Module extends Namespaced {
|
||||
|
||||
private final List<Task> tasks = new ArrayList<>();
|
||||
@ -25,6 +25,7 @@ public class Module extends Namespaced {
|
||||
public List<Task> getTasks() {
|
||||
return tasks;
|
||||
}
|
||||
|
||||
public void addTask(Task task) {
|
||||
tasks.add(task);
|
||||
}
|
||||
|
@ -6,14 +6,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Task
|
||||
extends Namespaced
|
||||
implements Runnable
|
||||
{
|
||||
|
||||
private boolean isDone = false;
|
||||
private boolean isActive = false;
|
||||
extends Namespaced
|
||||
implements Runnable {
|
||||
|
||||
List<Task> requiredTasks = new ArrayList<>();
|
||||
private boolean isDone = false;
|
||||
private boolean isActive = false;
|
||||
|
||||
protected Task(String id) {
|
||||
super(id);
|
||||
@ -33,7 +31,9 @@ public abstract class Task
|
||||
return isDone;
|
||||
}
|
||||
|
||||
public boolean isActive() { return isActive; }
|
||||
public boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public boolean canRun() {
|
||||
if (this.isActive) return false;
|
||||
@ -46,4 +46,8 @@ public abstract class Task
|
||||
public List<Task> getRequiredTasks() {
|
||||
return requiredTasks;
|
||||
}
|
||||
|
||||
public void addRequiredTask(Task task) {
|
||||
requiredTasks.add(task);
|
||||
}
|
||||
}
|
||||
|
@ -1,101 +1,107 @@
|
||||
package ru.windcorp.progressia.common.modules;
|
||||
|
||||
import ru.windcorp.progressia.common.state.StateFieldBuilder;
|
||||
|
||||
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.concurrent.*;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
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 boolean loadingDone;
|
||||
private int activeThreadsCount;
|
||||
private static final TaskManager instance = new TaskManager();
|
||||
private final List<Task> tasks = new ArrayList<>();
|
||||
private final List<Module> modules = new ArrayList<>();
|
||||
private final ExecutorService executorService;
|
||||
private final AtomicBoolean loadingDone;
|
||||
private final AtomicInteger activeThreadsCount;
|
||||
|
||||
private final ExecutorService executorService;
|
||||
private TaskManager() {
|
||||
loadingDone = new AtomicBoolean(false);
|
||||
activeThreadsCount = new AtomicInteger(0);
|
||||
executorService = newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors(), Thread::new);
|
||||
}
|
||||
|
||||
private TaskManager() {
|
||||
loadingDone = false;
|
||||
activeThreadsCount = 0;
|
||||
executorService = newFixedThreadPool(
|
||||
Runtime.getRuntime().availableProcessors(), Thread::new);
|
||||
}
|
||||
public static TaskManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static TaskManager getInstance() {
|
||||
return instance;
|
||||
}
|
||||
public void registerModule(Module module) {
|
||||
tasks.addAll(module.getTasks());
|
||||
modules.add(module);
|
||||
}
|
||||
|
||||
public void registerModule(Module module) {
|
||||
tasks.addAll(module.getTasks());
|
||||
modules.add(module);
|
||||
}
|
||||
public boolean isLoadingDone() {
|
||||
return loadingDone.get();
|
||||
}
|
||||
|
||||
public boolean isLoadingDone() {
|
||||
return loadingDone;
|
||||
}
|
||||
public void startLoading() {
|
||||
LogManager.getLogger().info("Loading is started");
|
||||
for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
|
||||
executorService.submit(() -> {
|
||||
while (!loadingDone.get()) {
|
||||
Task t = getRunnableTask();
|
||||
if (t != null) {
|
||||
activeThreadsCount.incrementAndGet();
|
||||
t.run();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void startLoading() {
|
||||
waitForLoadingEnd();
|
||||
if (!tasks.isEmpty()) {
|
||||
throw CrashReports.crash(new Exception("Loading is failed"), "");
|
||||
}
|
||||
LogManager.getLogger().info("Loading is finished");
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
|
||||
for(int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
|
||||
executorService.submit(() -> {
|
||||
while(!loadingDone) {
|
||||
Task t = getRunnableTask();
|
||||
if (t != null) {
|
||||
synchronized (this) {
|
||||
activeThreadsCount++;
|
||||
}
|
||||
t.run();
|
||||
synchronized (this) {
|
||||
activeThreadsCount--;
|
||||
notifyAll();
|
||||
}
|
||||
} else if (activeThreadsCount > 0) {
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
synchronized (this) {
|
||||
loadingDone = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
public synchronized Task getRunnableTask() {
|
||||
if (!tasks.isEmpty()) {
|
||||
for (Task t :
|
||||
tasks) {
|
||||
if (t.canRun()) {
|
||||
tasks.remove(t);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
waitForLoadingEnd();
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
|
||||
public synchronized Task getRunnableTask() {
|
||||
if(!tasks.isEmpty()) {
|
||||
for (Task t :
|
||||
tasks) {
|
||||
if(t.canRun()) {
|
||||
tasks.remove(t);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void waitForLoadingEnd() {
|
||||
synchronized (this) {
|
||||
while(!loadingDone) {
|
||||
try {
|
||||
this.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void waitForLoadingEnd() {
|
||||
synchronized (this) {
|
||||
while (!loadingDone.get()) {
|
||||
try {
|
||||
this.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user