X Tutup
Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,14 @@ private static Future<?> directExecute(Runnable runnable, long delay) {
* executor)
*/
public static synchronized void execute(Task task) {
Future<?> future = null;
if (task.serial == null || !hasSerialRunning(task.serial)) {
task.executionAsked = true;
future = directExecute(task, task.remainingDelay);
}
if ((task.id != null || task.serial != null) && !task.managed.get()) {
if (task.id != null || task.serial != null) {
/* keep task */
task.future = future;
TASKS.add(task);
}
if (task.serial == null || !hasSerialRunning(task.serial)) {
task.executionAsked = true;
task.future = directExecute(task, task.remainingDelay);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ public void parallelBackgroundTasks() {
}
}

@Test
public void serializedBackgroundTasksAsynchronous() throws InterruptedException {
/* set an executor with 4 threads */
BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4));

testSerializedBackgroundTasks();
}

@Test
public void serializedBackgroundTasksSynchronous() throws InterruptedException {
/* set a synchronous executor */
BackgroundExecutor.setExecutor(new Executor() {
@Override
public void execute(Runnable command) {
try {
command.run();
} catch (RuntimeException ignored) {
// ignored
}
}
});

testSerializedBackgroundTasks();
}

/**
* Verify that serialized background tasks are correctly serialized.
*
Expand All @@ -173,14 +198,10 @@ public void parallelBackgroundTasks() {
* Once all tasks have completed execution, verify that the items in the
* list are ordered.
*/
@Test
public void serializedBackgroundTasks() {
private void testSerializedBackgroundTasks() {
/* number of items to add to the list */
final int NB_ADD = 10;

/* set an executor with 4 threads */
BackgroundExecutor.setExecutor(Executors.newFixedThreadPool(4));

/*
* the calls are serialized, but not necessarily on the same thread, so
* we need to synchronize to avoid cache effects
Expand Down
X Tutup