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

io.tlf.jme.jfx.injfx.ApplicationThreadExecutor Maven / Gradle / Ivy

The newest version!
package io.tlf.jme.jfx.injfx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * The executor for executing tasks in application thread.
 *
 * @author JavaSaBr
 */
public class ApplicationThreadExecutor {

    private static final ApplicationThreadExecutor INSTANCE = new ApplicationThreadExecutor();

    public static ApplicationThreadExecutor getInstance() {
        return INSTANCE;
    }

    /**
     * The list of waiting tasks.
     */
    // private final ConcurrentArray waitTasks;
    private final CopyOnWriteArrayList waitTasks;

    /**
     * The list of tasks to execute.
     */
    // private final Array execute;
    private final List execute;

    private ApplicationThreadExecutor() {
        // this.waitTasks = ArrayFactory.newConcurrentAtomicARSWLockArray(Runnable.class);
        // this.execute = ArrayFactory.newArray(Runnable.class);
        this.waitTasks = new CopyOnWriteArrayList<>();
        this.execute = new ArrayList<>();
    }

    /**
     * Add the task to execute.
     *
     * @param task the new task.
     */
    public void addToExecute(Runnable task) {
        // ArrayUtils.runInWriteLock(waitTasks, task, Array::add);
        waitTasks.add(task);
    }

    /**
     * Execute the waiting tasks.
     */
    public void execute() {

        if (waitTasks.isEmpty()) {
            return;
        }

        // ArrayUtils.runInWriteLock(waitTasks, execute, ArrayUtils::move);

        //long stamp

        try {
            execute.forEach(Runnable::run);
        } finally {
            execute.clear();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy