All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jtrim2.executor.SyncNonRecursiveExecutor Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package org.jtrim2.executor;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;
import java.util.concurrent.Executor;

final class SyncNonRecursiveExecutor implements Executor {
    private final ThreadLocal> taskQueueRef;

    public SyncNonRecursiveExecutor() {
        this.taskQueueRef = new ThreadLocal<>();
    }

    @Override
    public void execute(Runnable command) {
        Objects.requireNonNull(command, "command");

        Deque taskQueue = taskQueueRef.get();
        if (taskQueue == null) {
            taskQueue = new ArrayDeque<>();
            taskQueueRef.set(taskQueue);
            try {
                for (Runnable nextTask = command; nextTask != null; nextTask = taskQueue.pollFirst()) {
                    nextTask.run();
                }
            } finally {
                taskQueueRef.remove();
            }
        } else {
            taskQueue.addLast(command);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy