Fix delayed executor.

This commit is contained in:
Aria 2023-05-08 02:43:28 -07:00
parent abed7cd895
commit c30a980ea0
4 changed files with 60 additions and 33 deletions

View file

@ -12,7 +12,7 @@ plugins {
id 'maven-publish'
}
version = "1.1.8"
version = "1.1.9"
group = "dev.zontreck"
archivesBaseName = "LibAC"
java.toolchain.languageVersion = JavaLanguageVersion.of(17)

View file

@ -13,12 +13,24 @@ public abstract class Task implements Runnable
public static final String CHECK = "";
public static final String FAIL = "X";
// Else use the progress spinner from the Progress class
private boolean isSilent=false;
public Task(String name)
{
TASK_NAME=name;
}
/**
* This constructor is meant to be used to create silent tasks that do not output to the console. (Example usage: DelayedExecutionService)
* @param name Task name
* @param silent Whether to print to the terminal
*/
public Task(String name, boolean silent)
{
this(name);
isSilent=silent;
}
public boolean isComplete(){
return token.get();
@ -26,13 +38,14 @@ public abstract class Task implements Runnable
public void startTask()
{
DelayedExecutorService.getInstance().schedule(this, 1);
DelayedExecutorService.getInstance().schedule(new SpinnerTask(token), 1);
DelayedExecutorService.scheduleTask(this, 1);
if(!isSilent)
DelayedExecutorService.scheduleTask(new SpinnerTask(token), 1);
}
public void stopTask()
{
if(token.get())
if(token.get() && !isSilent)
{
System.out.printf("\r"+TASK_NAME+"\t\t["+token.status+"]\n");
}
@ -62,7 +75,8 @@ public abstract class Task implements Runnable
try {
Thread.sleep(1000L);
System.out.printf("\r"+TASK_NAME+"\t\t"+spinner.getSpinnerTick());
if(!isSilent)
System.out.printf("\r"+TASK_NAME+"\t\t"+spinner.getSpinnerTick());
}catch(Exception e)
{
e.printStackTrace();

View file

@ -1,13 +1,13 @@
package dev.zontreck.ariaslib.terminal;
import com.google.common.collect.Lists;
import dev.zontreck.ariaslib.util.DelayedExecutorService;
import java.util.List;
import java.util.Stack;
import java.util.*;
public class TaskBus implements Runnable
{
public static Stack<Task> tasks = new Stack<>();
public static List<Task> tasks = new ArrayList<>();
public static Task current = null;
@Override
@ -17,7 +17,7 @@ public class TaskBus implements Runnable
if(TaskBus.current == null)
{
current = tasks.pop();
current = tasks.get(0);
current.startTask();
}else {

View file

@ -1,11 +1,10 @@
package dev.zontreck.ariaslib.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import dev.zontreck.ariaslib.terminal.Task;
import dev.zontreck.ariaslib.terminal.Terminal;
@ -29,6 +28,20 @@ public class DelayedExecutorService {
}
private DelayedExecutorService(){}
/**
* This function is designed to set back up the executor if it was previously stopped and restarted.
*/
public static void setup()
{
stopRepeatingThread();
repeater.schedule(new TimerTask() {
@Override
public void run() {
DelayedExecutorService.getInstance().onTick();
}
}, 1000L, 1000L);
}
/**
* Stops accepting new tasks, and current ones will abort
*/
@ -53,32 +66,32 @@ public class DelayedExecutorService {
}
public class DelayedExecution
{
public DelayedExecution(Runnable run, long unix) {
public DelayedExecution(Task run, long unix) {
scheduled=run;
unix_time=unix;
}
public Runnable scheduled;
public Task scheduled;
public long unix_time;
}
public List<DelayedExecution> EXECUTORS = new ArrayList<>();
public void schedule(final Runnable run, int seconds)
public static void scheduleTask(final Task run, int seconds)
{
DelayedExecutorService.getInstance().schedule(run,seconds);
}
public static void scheduleRepeatingTask(final Task run, int seconds)
{
DelayedExecutorService.getInstance().scheduleRepeating(run,seconds);
}
public void schedule(final Task run, int seconds)
{
if(!isRunning()){
return;
}
//long unix = Instant.now().getEpochSecond()+ (seconds);
TimerTask task = new TimerTask() {
@Override
public void run()
{
run.run();
}
};
repeater.schedule(task, seconds*1000L);
//DelayedExecution exe = new DelayedExecution(run,unix);
//EXECUTORS.add(exe);
long unix = Instant.now().getEpochSecond()+ (seconds);
EXECUTORS.add(new DelayedExecution(run, unix));
}
public static boolean isRunning()
@ -86,19 +99,19 @@ public class DelayedExecutorService {
return RUN.get();
}
public void scheduleRepeating(final Runnable run, int seconds)
public void scheduleRepeating(final Task run, int seconds)
{
if(!isRunning()) return;
TimerTask task = new TimerTask() {
long unix = Instant.now().getEpochSecond()+ (seconds);
Task repeater = new Task("Repeating:"+run.TASK_NAME, true) {
@Override
public void run() {
run.run();
scheduleRepeating(run, seconds);
}
};
repeater.schedule(task, seconds*1000L);
EXECUTORS.add(new DelayedExecution(repeater, unix));
}
private static void stopRepeatingThread()
@ -112,7 +125,7 @@ public class DelayedExecutorService {
{
DelayedExecutorService.stopRepeatingThread();
}
/*Iterator<DelayedExecution> it = EXECUTORS.iterator();
Iterator<DelayedExecution> it = EXECUTORS.iterator();
while(it.hasNext())
{
DelayedExecution e = it.next();
@ -123,7 +136,7 @@ public class DelayedExecutorService {
tx.setName("DelayedExecutorTask-"+String.valueOf(DelayedExecutorService.getNext()));
tx.start();
}
}*/
}
}
public static int getNext()