Made some twerks to tasks, fix task period for PeriodicSyncTask

Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
Quentin Legot 2023-02-12 19:10:24 +01:00
parent 24656fa74f
commit be9f422f1f
8 changed files with 90 additions and 36 deletions

View File

@ -2,7 +2,6 @@ package fr.altarik.toolbox.task.async;
import fr.altarik.toolbox.task.AltarikRunnable; import fr.altarik.toolbox.task.AltarikRunnable;
import fr.altarik.toolbox.task.PeriodicTaskI; import fr.altarik.toolbox.task.PeriodicTaskI;
import fr.altarik.toolbox.task.TaskI;
import it.unimi.dsi.fastutil.ints.IntComparators; import it.unimi.dsi.fastutil.ints.IntComparators;
import java.util.ArrayList; import java.util.ArrayList;
@ -48,11 +47,11 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI {
* *
* @return an instance of AsyncTasks * @return an instance of AsyncTasks
*/ */
public static TaskI initialize(int numberOfWorker) { public static PeriodicTaskI initialize(int numberOfWorker) {
return new AsyncPeriodicTasks(numberOfWorker); return new AsyncPeriodicTasks(numberOfWorker);
} }
public static TaskI initialize() { public static PeriodicTaskI initialize() {
return initialize(Runtime.getRuntime().availableProcessors()); return initialize(Runtime.getRuntime().availableProcessors());
} }

View File

@ -30,7 +30,6 @@ public class TaskScheduler {
*/ */
public synchronized void asyncRunnerPeriodicTasks() throws InterruptedException { public synchronized void asyncRunnerPeriodicTasks() throws InterruptedException {
loop: while(true) { loop: while(true) {
notify();
while(asyncTasks.size() == 0) { while(asyncTasks.size() == 0) {
if(isStop()) { if(isStop()) {
break loop; break loop;

View File

@ -24,7 +24,7 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable {
@Override @Override
public void addTask(AltarikRunnable function) { public void addTask(AltarikRunnable function) {
addTask(function, 0, 0); addTask(function, 0, 1);
} }
@Override @Override
@ -51,6 +51,6 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable {
@Override @Override
public void addTask(AltarikRunnable function, long delay, long period) { public void addTask(AltarikRunnable function, long delay, long period) {
tasks.add(new SchedulerTaskData(function, delay, period)); tasks.add(new SchedulerTaskData(function, delay, period - 1));
} }
} }

View File

@ -7,18 +7,18 @@ import fr.altarik.toolbox.task.TaskI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class OneTimeSyncTask implements TaskI, Runnable { public class SyncTask implements TaskI, Runnable {
private final ServerTickListener listener; private final ServerTickListener listener;
private final List<SchedulerTaskData> tasks; private final List<SchedulerTaskData> tasks;
private OneTimeSyncTask() { private SyncTask() {
this.listener = new ServerTickListener(this); this.listener = new ServerTickListener(this);
this.tasks = new ArrayList<>(2); this.tasks = new ArrayList<>(2);
} }
public static TaskI initialize() { public static TaskI initialize() {
return new OneTimeSyncTask(); return new SyncTask();
} }
@Override @Override

View File

@ -0,0 +1,32 @@
package fr.altarik.toolbox.task.async;
import fr.altarik.toolbox.task.AltarikRunnable;
import org.junit.jupiter.api.Test;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;
public class PeriodicAsyncTaskTest {
@Test
void testPeriodicASyncTask() {
AsyncPeriodicTasks worker = (AsyncPeriodicTasks) AsyncPeriodicTasks.initialize();
Stack<AtomicInteger> results = new Stack<>();
AtomicInteger value1 = new AtomicInteger(1);
AtomicInteger value2 = new AtomicInteger(2);
AltarikRunnable runnable1 = new AltarikRunnable() {
@Override
public void run() {
results.add(value1);
}
};
AltarikRunnable runnable2 = new AltarikRunnable() {
@Override
public void run() {
results.add(value2);
}
};
}
}

View File

@ -1,25 +0,0 @@
package fr.altarik.toolbox.task.sync;
import fr.altarik.toolbox.task.AltarikRunnable;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class OneTimeSyncTaskTest {
@Test
void testOneTimeTask() {
OneTimeSyncTask worker = (OneTimeSyncTask) OneTimeSyncTask.initialize();
List<AtomicInteger> results = new ArrayList<>();
AtomicInteger value1 = new AtomicInteger(1);
AtomicInteger value2 = new AtomicInteger(2);
worker.addTask(new AltarikRunnable() {
@Override
public void run() {
results.add(value1);
}
});
}
}

View File

@ -8,7 +8,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class PeriodicSyncTaskTest { class PeriodicSyncTaskTest {
@Test @Test
void testPeriodicSyncTask() { void testPeriodicSyncTask() {
@ -22,7 +22,7 @@ public class PeriodicSyncTaskTest {
public void run() { public void run() {
results.add(value1); results.add(value1);
} }
}, 1, 2); }, 1, 3);
worker.addTask(new AltarikRunnable() { worker.addTask(new AltarikRunnable() {
private int i = 0; private int i = 0;
@Override @Override

View File

@ -0,0 +1,49 @@
package fr.altarik.toolbox.task.sync;
import fr.altarik.toolbox.task.AltarikRunnable;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
class SyncTaskTest {
@Test
void testOneTimeTask() {
SyncTask worker = (SyncTask) SyncTask.initialize();
List<AtomicInteger> results = new ArrayList<>();
AtomicInteger value1 = new AtomicInteger(1);
AtomicInteger value2 = new AtomicInteger(2);
AltarikRunnable task1 = new AltarikRunnable() {
@Override
public void run() {
results.add(value1);
}
};
AltarikRunnable task2 = new AltarikRunnable() {
@Override
public void run() {
results.add(value2);
}
};
worker.addTask(task1);
worker.run();
worker.run();
worker.addTask(task2);
worker.addTask(task1);
worker.addTask(task2);
worker.run();
worker.addTask(task1);
worker.run();
AtomicInteger[] expected = {
value1,
value2,
value1,
value2,
value1
};
Assertions.assertArrayEquals(expected, results.toArray());
}
}