Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.runescape.task;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- /**
- * Represents a {@link Task} for the application.
- *
- * @author Braeden Dempsey
- */
- public abstract class Task {
- /**
- * Constructs a default {@link Task}.
- * @param delay the interval between a cycle.
- * @param unit the {@link java.util.concurrent.TimeUnit} of the {@link com.runescape.task.Task}.
- */
- public Task(final int delay, final TimeUnit unit) {
- this.delay = delay;
- this.unit = unit;
- }
- /**
- * Interval between cycles.
- */
- private final int delay;
- /**
- * {@link TimeUnit} of the {@link Task}.
- */
- private final TimeUnit unit;
- /**
- * {@link TaskState} of the {@link Task}.
- */
- private TaskState state;
- /**
- * Scheduler of the {@link Task}.
- */
- private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
- /**
- * Executes the {@link Task}.
- */
- protected abstract void execute();
- /**
- * Pauses the {@link Task}.
- */
- public void pause() {
- state = TaskState.PAUSED;
- }
- /**
- * Registers a {@link Task}.
- */
- public void initialize() {
- if (!state.equals(TaskState.ACTIVE) || !service.isShutdown())
- service.scheduleAtFixedRate(new TaskManager(), 0, delay, unit);
- state = TaskState.ACTIVE;
- }
- /**
- * Remove a {@link Task}.
- */
- public void kill() {
- state = TaskState.DEAD;
- service.shutdown();
- }
- /**
- * Checks if the {@link TaskState} equals <code>PAUSED</code>.
- * @return the <code>state</code>.
- */
- public boolean isPaused() {
- return state.equals(TaskState.PAUSED);
- }
- /**
- * Checks if the {@link TaskState} equals <code>ACTIVE</code>.
- * @return the <code>state</code>.
- */
- public boolean isActive() {
- return state.equals(TaskState.ACTIVE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement