Creating Schedulers.

Discussion in 'Programming' started by Nickframe76, Jun 12, 2016.

  1. Hello,
    so I'll get straight to the point. I need to make schedulers for an API of mine that does not have anything in relation with bukkit/spigot, so I'm asking how would I make schedulers like the bukkit API does.
    Here's what I've got so far.

    Code (Text):
    public abstract class SyncScheduler extends Scheduler {

        private Thread thread = null;
        private boolean repeating = false;

        @Override
        public synchronized void runTask() {
            thread = new Thread(this);
            thread.start();
        }

        @Override
        public synchronized void runTaskLater(long delay) {
            thread = new Thread(this);
            try {
                Thread.sleep(delay * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thread.run();
        }

        @Override
        public synchronized void runRepeatingTask(long period) {
            thread = new Thread(this);
            repeating = true;
            while (!thread.isInterrupted()) {
                thread.run();
                try {
                    Thread.sleep(period * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public synchronized void cancel() {
            if (thread != null || !repeating) {
                throw new SchedulerException("Scheduler is not started or is not a repeating task!");
            } else {
                thread.interrupt();
                repeating = false;
            }
        }

    }
    Code (Text):
    public abstract class Scheduler implements Runnable {
       
        public abstract void runTask();
       
        public abstract void runTaskLater(long delay);
       
        public abstract void runRepeatingTask(long period);
       
        public abstract void cancel();

    }
    Thanks in advance.
     
  2. What kind of API is it?