Switched DelayedExecutorService to use ScheduledThreadPoolExecutor
This commit is contained in:
parent
8be88eedda
commit
47e114ba57
3 changed files with 27 additions and 22 deletions
|
@ -12,7 +12,7 @@ plugins {
|
||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
|
|
||||||
version = "1.1.10"
|
version = "1.1.11"
|
||||||
group = "dev.zontreck"
|
group = "dev.zontreck"
|
||||||
archivesBaseName = "LibAC"
|
archivesBaseName = "LibAC"
|
||||||
java.toolchain.languageVersion = JavaLanguageVersion.of(17)
|
java.toolchain.languageVersion = JavaLanguageVersion.of(17)
|
||||||
|
|
|
@ -4,8 +4,9 @@ import dev.zontreck.ariaslib.util.DelayedExecutorService;
|
||||||
import dev.zontreck.ariaslib.util.Progress;
|
import dev.zontreck.ariaslib.util.Progress;
|
||||||
|
|
||||||
import java.io.Console;
|
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;
|
public final String TASK_NAME;
|
||||||
private TaskCompletionToken token = new TaskCompletionToken();
|
private TaskCompletionToken token = new TaskCompletionToken();
|
||||||
|
|
|
@ -2,7 +2,10 @@ package dev.zontreck.ariaslib.util;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import dev.zontreck.ariaslib.terminal.Task;
|
import dev.zontreck.ariaslib.terminal.Task;
|
||||||
import dev.zontreck.ariaslib.terminal.Terminal;
|
import dev.zontreck.ariaslib.terminal.Terminal;
|
||||||
|
@ -11,20 +14,19 @@ import dev.zontreck.ariaslib.terminal.Terminal;
|
||||||
public class DelayedExecutorService {
|
public class DelayedExecutorService {
|
||||||
|
|
||||||
private static final AtomicBoolean RUN = new AtomicBoolean(true);
|
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 DelayedExecutorService inst;
|
||||||
private static final Timer repeater;
|
private static final ScheduledThreadPoolExecutor repeater;
|
||||||
static{
|
static{
|
||||||
inst=new DelayedExecutorService();
|
inst=new DelayedExecutorService();
|
||||||
repeater=new Timer();
|
repeater = new ScheduledThreadPoolExecutor(20);
|
||||||
repeater.schedule(new TimerTask(){
|
|
||||||
|
repeater.schedule(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run() {
|
||||||
{
|
|
||||||
DelayedExecutorService.getInstance().onTick();
|
DelayedExecutorService.getInstance().onTick();
|
||||||
|
|
||||||
}
|
}
|
||||||
}, 1000L, 1000L);
|
}, 1L, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
private DelayedExecutorService(){}
|
private DelayedExecutorService(){}
|
||||||
|
|
||||||
|
@ -34,12 +36,13 @@ public class DelayedExecutorService {
|
||||||
public static void setup()
|
public static void setup()
|
||||||
{
|
{
|
||||||
stopRepeatingThread();
|
stopRepeatingThread();
|
||||||
repeater.schedule(new TimerTask() {
|
|
||||||
|
repeater.schedule(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
DelayedExecutorService.getInstance().onTick();
|
DelayedExecutorService.getInstance().onTick();
|
||||||
}
|
}
|
||||||
}, 1000L, 1000L);
|
}, 1L, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,8 +93,9 @@ public class DelayedExecutorService {
|
||||||
if(!isRunning()){
|
if(!isRunning()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
long unix = Instant.now().getEpochSecond()+ (seconds);
|
repeater.schedule(run, seconds, TimeUnit.SECONDS);
|
||||||
EXECUTORS.add(new DelayedExecution(run, unix));
|
//long unix = Instant.now().getEpochSecond()+ (seconds);
|
||||||
|
//EXECUTORS.add(new DelayedExecution(run, unix));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isRunning()
|
public static boolean isRunning()
|
||||||
|
@ -103,20 +107,21 @@ public class DelayedExecutorService {
|
||||||
{
|
{
|
||||||
if(!isRunning()) return;
|
if(!isRunning()) return;
|
||||||
|
|
||||||
long unix = Instant.now().getEpochSecond()+ (seconds);
|
//long unix = Instant.now().getEpochSecond()+ (seconds);
|
||||||
Task repeater = new Task("Repeating:"+run.TASK_NAME, true) {
|
Task repeat = new Task("Repeating:"+run.TASK_NAME, true) {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
run.run();
|
run.run();
|
||||||
scheduleRepeating(run, seconds);
|
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()
|
private static void stopRepeatingThread()
|
||||||
{
|
{
|
||||||
repeater.cancel();
|
repeater.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onTick()
|
public void onTick()
|
||||||
|
@ -125,7 +130,7 @@ public class DelayedExecutorService {
|
||||||
{
|
{
|
||||||
DelayedExecutorService.stopRepeatingThread();
|
DelayedExecutorService.stopRepeatingThread();
|
||||||
}
|
}
|
||||||
Iterator<DelayedExecution> it = EXECUTORS.iterator();
|
/*Iterator<DelayedExecution> it = EXECUTORS.iterator();
|
||||||
try{
|
try{
|
||||||
|
|
||||||
while(it.hasNext())
|
while(it.hasNext())
|
||||||
|
@ -139,12 +144,11 @@ public class DelayedExecutorService {
|
||||||
tx.start();
|
tx.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(Exception e){}
|
}catch(Exception e){}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getNext()
|
public static int getNext()
|
||||||
{
|
{
|
||||||
COUNT++;
|
return COUNT.getAndIncrement();
|
||||||
return COUNT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue