Switched DelayedExecutorService to use ScheduledThreadPoolExecutor

This commit is contained in:
Aria 2023-05-08 15:46:39 -07:00
parent 8be88eedda
commit 47e114ba57
3 changed files with 27 additions and 22 deletions

View file

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

View file

@ -4,8 +4,9 @@ import dev.zontreck.ariaslib.util.DelayedExecutorService;
import dev.zontreck.ariaslib.util.Progress;
import java.io.Console;
import java.util.TimerTask;
public abstract class Task implements Runnable
public abstract class Task extends TimerTask implements Runnable
{
public final String TASK_NAME;
private TaskCompletionToken token = new TaskCompletionToken();

View file

@ -2,7 +2,10 @@ package dev.zontreck.ariaslib.util;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import dev.zontreck.ariaslib.terminal.Task;
import dev.zontreck.ariaslib.terminal.Terminal;
@ -11,20 +14,19 @@ import dev.zontreck.ariaslib.terminal.Terminal;
public class DelayedExecutorService {
private static final AtomicBoolean RUN = new AtomicBoolean(true);
private static int COUNT = 0;
private static AtomicInteger COUNT = new AtomicInteger(0);
private static final DelayedExecutorService inst;
private static final Timer repeater;
private static final ScheduledThreadPoolExecutor repeater;
static{
inst=new DelayedExecutorService();
repeater=new Timer();
repeater.schedule(new TimerTask(){
repeater = new ScheduledThreadPoolExecutor(20);
repeater.schedule(new Runnable() {
@Override
public void run()
{
public void run() {
DelayedExecutorService.getInstance().onTick();
}
}, 1000L, 1000L);
}, 1L, TimeUnit.SECONDS);
}
private DelayedExecutorService(){}
@ -34,12 +36,13 @@ public class DelayedExecutorService {
public static void setup()
{
stopRepeatingThread();
repeater.schedule(new TimerTask() {
repeater.schedule(new Runnable() {
@Override
public void run() {
DelayedExecutorService.getInstance().onTick();
}
}, 1000L, 1000L);
}, 1L, TimeUnit.SECONDS);
}
/**
@ -90,8 +93,9 @@ public class DelayedExecutorService {
if(!isRunning()){
return;
}
long unix = Instant.now().getEpochSecond()+ (seconds);
EXECUTORS.add(new DelayedExecution(run, unix));
repeater.schedule(run, seconds, TimeUnit.SECONDS);
//long unix = Instant.now().getEpochSecond()+ (seconds);
//EXECUTORS.add(new DelayedExecution(run, unix));
}
public static boolean isRunning()
@ -103,20 +107,21 @@ public class DelayedExecutorService {
{
if(!isRunning()) return;
long unix = Instant.now().getEpochSecond()+ (seconds);
Task repeater = new Task("Repeating:"+run.TASK_NAME, true) {
//long unix = Instant.now().getEpochSecond()+ (seconds);
Task repeat = new Task("Repeating:"+run.TASK_NAME, true) {
@Override
public void run() {
run.run();
scheduleRepeating(run, seconds);
}
};
EXECUTORS.add(new DelayedExecution(repeater, unix));
repeater.schedule(repeat, seconds, TimeUnit.SECONDS);
//EXECUTORS.add(new DelayedExecution(repeater, unix));
}
private static void stopRepeatingThread()
{
repeater.cancel();
repeater.shutdown();
}
public void onTick()
@ -125,7 +130,7 @@ public class DelayedExecutorService {
{
DelayedExecutorService.stopRepeatingThread();
}
Iterator<DelayedExecution> it = EXECUTORS.iterator();
/*Iterator<DelayedExecution> it = EXECUTORS.iterator();
try{
while(it.hasNext())
@ -139,12 +144,11 @@ public class DelayedExecutorService {
tx.start();
}
}
}catch(Exception e){}
}catch(Exception e){}*/
}
public static int getNext()
{
COUNT++;
return COUNT;
return COUNT.getAndIncrement();
}
}