Improved performance a bit, changed Stack (LIFO) to BlockingQueue(FIFO) with same synchronization principale than Stack and improved test

This commit is contained in:
Quentin Legot 2022-09-20 11:31:48 +02:00
parent 115538952e
commit bc1b0c1fed
5 changed files with 53 additions and 61 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -5,22 +5,11 @@
</component>
<component name="ChangeListManager">
<list default="true" id="8cc571ff-831a-43c2-a081-ccf36a30cea0" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/discord.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradle.properties" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.jar" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradle/wrapper/gradle-wrapper.properties" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradlew" afterDir="false" />
<change afterPath="$PROJECT_DIR$/gradlew.bat" afterDir="false" />
<change afterPath="$PROJECT_DIR$/settings.gradle" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/fr/altarik/toolbox/asynctasks/TasksThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -60,6 +49,8 @@
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "C:/Users/quent/IdeaProjects",
"nodejs_package_manager_path": "npm",
@ -81,6 +72,7 @@
<option name="presentableId" value="Default" />
<updated>1663623916232</updated>
<workItem from="1663623918010" duration="394000" />
<workItem from="1663662046975" duration="3831000" />
</task>
<servers />
</component>

View File

@ -8,19 +8,20 @@ public class AsyncTasks {
worker.run();
}
public static void addTask(Runnable function) {
worker.lock.lock();
public static void addTask(Runnable function) throws InterruptedException {
if(worker.workerThread.isInterrupted())
throw new RuntimeException("Async task thread has been interrupted while waiting for another task, which is anormal, please report this issue to developers");
worker.tasks.push(function);
throw new InterruptedException("Async task thread has been interrupted while waiting for another task, which is anormal, please report this issue to developers");
worker.tasks.put(function);
// this condition is non-atomic, but we want to avoid unwanted and useless interruption in the main thread(s) while waiting for the worker thread to be released
if(worker.isWaiting) {
worker.lock.lock();
worker.lockSignal.signalAll();
worker.lock.unlock();
}
}
public static int numberOfWaitingTask() {
synchronized (worker.lock) {
return worker.tasks.size();
}
}
}

View File

@ -1,33 +1,38 @@
package fr.altarik.toolbox.asynctasks;
import java.util.Stack;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class TasksThread {
boolean isWaiting = false;
final ReentrantLock lock = new ReentrantLock();
final Condition lockSignal = lock.newCondition();
Thread workerThread;
final Stack<Runnable> tasks = new Stack<>();
final BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<>();
public void run() {
workerThread = new Thread(() -> {
lock.lock();
try {
for(;;) {
while(tasks.empty()) {
while(tasks.isEmpty()) {
lock.lock();
isWaiting = true;
lockSignal.await();
isWaiting = false;
lock.unlock();
}
while(!tasks.empty()) {
tasks.pop().run();
while(!tasks.isEmpty()) {
tasks.take().run();
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
lock.unlock();
});
workerThread.start();

View File

@ -5,41 +5,30 @@ import org.junit.jupiter.api.Test;
import java.util.Date;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class AsyncTaskTest {
private String log(String message) {
return "[" + new Date() + "]" + message;
}
@Test
public void testAsyncOp() {
public void testAsyncOp() throws InterruptedException {
int numberOfTasks = 10000;
System.out.println("Initializing async tasks worker");
AsyncTasks.initialize();
Stack<Integer> results = new Stack<>();
System.out.println("[" + new Date() + "] sending task 1");
for(int i = 0; i < numberOfTasks; i++) {
System.out.println("[" + new Date() + "] sending task " + i);
AtomicInteger atomicInteger = new AtomicInteger(i);
AsyncTasks.addTask(() -> {
System.out.println("[" + new Date() + "] task 1");
results.push(1);
});
System.out.println("[" + new Date() + "] sending task 2");
AsyncTasks.addTask(() -> {
System.out.println("[" + new Date() + "] task 2");
results.push(2);
});
System.out.println("[" + new Date() + "] sending task 3");
AsyncTasks.addTask(() -> {
System.out.println("[" + new Date() + "] task 3");
results.push(3);
});
System.out.println("[" + new Date() + "] sending task 4");
AsyncTasks.addTask(() -> {
System.out.println("[" + new Date() + "] task 4");
results.push(4);
});
System.out.println("[" + new Date() + "] sending task 5");
AsyncTasks.addTask(() -> {
System.out.println("[" + new Date() + "] task 5");
results.push(5);
System.out.println(log(" task " + atomicInteger.get()));
results.push(atomicInteger.get());
});
}
while(AsyncTasks.numberOfWaitingTask() != 0) {
try {
synchronized (this) {
@ -49,7 +38,11 @@ public class AsyncTaskTest {
throw new RuntimeException(e);
}
}
assertArrayEquals(new Integer[]{1, 2, 3, 4, 5}, results.toArray());
Integer[] expected = new Integer[numberOfTasks];
for(int i = 0; i < numberOfTasks; i++) {
expected[i] = i;
}
assertArrayEquals(expected, results.toArray());
}
}