Made some twerks to tasks, fix task period for PeriodicSyncTask
Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
parent
24656fa74f
commit
be9f422f1f
@ -2,7 +2,6 @@ package fr.altarik.toolbox.task.async;
|
||||
|
||||
import fr.altarik.toolbox.task.AltarikRunnable;
|
||||
import fr.altarik.toolbox.task.PeriodicTaskI;
|
||||
import fr.altarik.toolbox.task.TaskI;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -48,11 +47,11 @@ public class AsyncPeriodicTasks implements PeriodicTaskI, AsyncTaskI {
|
||||
*
|
||||
* @return an instance of AsyncTasks
|
||||
*/
|
||||
public static TaskI initialize(int numberOfWorker) {
|
||||
public static PeriodicTaskI initialize(int numberOfWorker) {
|
||||
return new AsyncPeriodicTasks(numberOfWorker);
|
||||
}
|
||||
|
||||
public static TaskI initialize() {
|
||||
public static PeriodicTaskI initialize() {
|
||||
return initialize(Runtime.getRuntime().availableProcessors());
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,6 @@ public class TaskScheduler {
|
||||
*/
|
||||
public synchronized void asyncRunnerPeriodicTasks() throws InterruptedException {
|
||||
loop: while(true) {
|
||||
notify();
|
||||
while(asyncTasks.size() == 0) {
|
||||
if(isStop()) {
|
||||
break loop;
|
||||
|
@ -24,7 +24,7 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable {
|
||||
|
||||
@Override
|
||||
public void addTask(AltarikRunnable function) {
|
||||
addTask(function, 0, 0);
|
||||
addTask(function, 0, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -51,6 +51,6 @@ public class PeriodicSyncTask implements PeriodicTaskI, Runnable {
|
||||
|
||||
@Override
|
||||
public void addTask(AltarikRunnable function, long delay, long period) {
|
||||
tasks.add(new SchedulerTaskData(function, delay, period));
|
||||
tasks.add(new SchedulerTaskData(function, delay, period - 1));
|
||||
}
|
||||
}
|
||||
|
@ -7,18 +7,18 @@ import fr.altarik.toolbox.task.TaskI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OneTimeSyncTask implements TaskI, Runnable {
|
||||
public class SyncTask implements TaskI, Runnable {
|
||||
|
||||
private final ServerTickListener listener;
|
||||
private final List<SchedulerTaskData> tasks;
|
||||
|
||||
private OneTimeSyncTask() {
|
||||
private SyncTask() {
|
||||
this.listener = new ServerTickListener(this);
|
||||
this.tasks = new ArrayList<>(2);
|
||||
}
|
||||
|
||||
public static TaskI initialize() {
|
||||
return new OneTimeSyncTask();
|
||||
return new SyncTask();
|
||||
}
|
||||
|
||||
@Override
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class PeriodicSyncTaskTest {
|
||||
class PeriodicSyncTaskTest {
|
||||
|
||||
@Test
|
||||
void testPeriodicSyncTask() {
|
||||
@ -22,7 +22,7 @@ public class PeriodicSyncTaskTest {
|
||||
public void run() {
|
||||
results.add(value1);
|
||||
}
|
||||
}, 1, 2);
|
||||
}, 1, 3);
|
||||
worker.addTask(new AltarikRunnable() {
|
||||
private int i = 0;
|
||||
@Override
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user