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

net.dongliu.prettypb.rpc.common.Task Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.rpc.common;

/**
 * @author dongliu
 */
public abstract class Task implements Comparable {

    private final int id;

    private final long timeout;

    private final long start;

    private volatile boolean cancel;

    protected Task(int id, long timeout, long start) {
        this.id = id;
        this.timeout = timeout;
        this.start = start;
    }

    /**
     * called when task is timeout
     */
    public abstract void onTimeout();

    /**
     * called when channel/taskSet is closed
     */
    public abstract void onClosed();

    public int id() {
        return this.id;
    }

    /**
     * has been time out
     *
     * @return
     */
    public boolean timeout() {
        return timeout >= 0 && System.currentTimeMillis() - start > timeout;
    }

    /**
     * timeout been set for this task
     *
     * @return
     */
    public boolean timeoutSet() {
        return timeout >= 0;
    }

    /**
     * till when task is timeout
     *
     * @return
     */
    public long life() {
        return timeout + start;
    }

    /**
     * cancel the task
     */
    public void cancel() {
        this.cancel = true;
    }

    public boolean canceled() {
        return this.cancel;
    }

    @Override
    public int compareTo(T task) {
        return (int) (this.life() - task.life());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy