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

cn.nukkit.scheduler.AsyncWorker Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.scheduler;

import cn.nukkit.InterruptibleThread;

import java.util.LinkedList;

/**
 * @author MagicDroidX (Nukkit Project)
 */
public class AsyncWorker extends Thread implements InterruptibleThread {
    private final LinkedList stack = new LinkedList<>();

    public AsyncWorker() {
        this.setName("Asynchronous Worker");
    }

    public void stack(AsyncTask task) {
        synchronized (stack) {
            stack.addFirst(task);
        }
    }

    public void unstack() {
        synchronized (stack) {
            stack.clear();
        }
    }

    public void unstack(AsyncTask task) {
        synchronized (stack) {
            stack.remove(task);
        }
    }

    @Override
    public void run() {
        while (true) {
            synchronized (stack) {
                for (AsyncTask task : stack) {
                    if (!task.isFinished()) {
                        task.run();
                    }
                }
            }
            try {
                sleep(5);
            } catch (InterruptedException e) {
                //igonre
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy