延遲隊列2

1兆龙、隊列工具類

import java.lang.reflect.Method;
import java.util.concurrent.DelayQueue;


public class DelayExecUtil {
/**
 * 延期執(zhí)行隊列
 */
private final static DelayQueue<DelayObject> queue=new DelayQueue<>();

/**
 * 控制只允許啟動一次
 */
private static boolean execFlag=false;

/**
 * 添加延期執(zhí)行對象
 * @param delayObject
 */
public static void add(DelayObject delayObject){
    queue.add(delayObject);

    //添加元素后就開始執(zhí)行
    execute();
}

public static void remove(DelayObject delayObject){
    queue.remove(delayObject);
}
private static void execute(){
    if(execFlag){
        return;
    }
    synchronized (DelayExecUtil.class){
        if(execFlag){
            return;
        }
        execFlag=true;
        new Thread(()->{
                while(true){
                    try {
                        DelayObject delay = queue.take();
                        Class<?> aClass = delay.getCallBackInstance().getClass();
                        Object params = delay.getParams();
                        Method method;
                        if (null != params) {
                            method = aClass.getMethod(delay.getMethodName(), params.getClass());
                            method.invoke(delay.getCallBackInstance(), params);
                        } else {
                            method = aClass.getMethod(delay.getMethodName());
                            method.invoke(delay.getCallBackInstance());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
        }).start();
    }
}
}

2杖爽、延遲執(zhí)行對象

import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 延期執(zhí)行對象
 */
public class DelayObject implements Delayed {
/**
 * 唯一id
 */
private String uuid;
/**
 * 延期執(zhí)行時間  到達該時間后,當(dāng)前任務(wù)執(zhí)行
 */
private Date executeTime;
/**
 * 方法參數(shù)
 */
private Object params;
/**
 * 回調(diào)實例
 */
private Object callBackInstance;
/**
 * 回調(diào)方法名
 */
private String methodName;

public DelayObject(String uuid) {
    if(StringUtils.isEmpty(uuid)){
        throw new IllegalArgumentException("extendObject or extendObject.id is null");
    }

    this.uuid = uuid;
}

public Date getExecuteTime() {
    return executeTime;
}

public void setExecuteTime(Date executeTime) {
    this.executeTime = executeTime;
}

public Object getCallBackInstance() {
    return callBackInstance;
}

public void setCallBackInstance(Object callBackInstance) {
    this.callBackInstance = callBackInstance;
}

public String getMethodName() {
    return methodName;
}

public void setMethodName(String methodName) {
    this.methodName = methodName;
}

public Object getParams() {
    return params;
}

public void setParams(Object params) {
    this.params = params;
}

/**
 * Returns the remaining delay associated with this object, in the
 * given time unit.
 *
 * @param unit the time unit
 * @return the remaining delay; zero or negative values indicate
 * that the delay has already elapsed
 */
@Override
public long getDelay(TimeUnit unit) {
    return this.executeTime.getTime() - System.currentTimeMillis();
}
@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    DelayObject that = (DelayObject) o;
    return Objects.equals(uuid, that.uuid);
}
/**
 * Compares this object with the specified object for order.  Returns a
 * negative integer, zero, or a positive integer as this object is less
 * than, equal to, or greater than the specified object.
 * <p>
 * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
 * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
 * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
 * <tt>y.compareTo(x)</tt> throws an exception.)
 * <p>
 * <p>The implementor must also ensure that the relation is transitive:
 * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
 * <tt>x.compareTo(z)&gt;0</tt>.
 * <p>
 * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
 * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
 * all <tt>z</tt>.
 * <p>
 * <p>It is strongly recommended, but <i>not</i> strictly required that
 * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
 * class that implements the <tt>Comparable</tt> interface and violates
 * this condition should clearly indicate this fact.  The recommended
 * language is "Note: this class has a natural ordering that is
 * inconsistent with equals."
 * <p>
 * <p>In the foregoing description, the notation
 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
 * <i>expression</i> is negative, zero or positive.
 *
 * @param o the object to be compared.
 * @return a negative integer, zero, or a positive integer as this object
 * is less than, equal to, or greater than the specified object.
 * @throws NullPointerException if the specified object is null
 * @throws ClassCastException   if the specified object's type prevents it
 *                              from being compared to this object.
 */
@Override
public int compareTo(Delayed o) {
    int result = this.getExecuteTime().compareTo(((DelayObject) o).getExecuteTime());
    return result;
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末紫皇,一起剝皮案震驚了整個濱河市掂林,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌坝橡,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件精置,死亡現(xiàn)場離奇詭異计寇,居然都是意外死亡,警方通過查閱死者的電腦和手機脂倦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門番宁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人赖阻,你說我怎么就攤上這事蝶押。” “怎么了火欧?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵棋电,是天一觀的道長茎截。 經(jīng)常有香客問我,道長赶盔,這世上最難降的妖魔是什么企锌? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮于未,結(jié)果婚禮上撕攒,老公的妹妹穿的比我還像新娘。我一直安慰自己烘浦,他們只是感情好抖坪,可當(dāng)我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著闷叉,像睡著了一般擦俐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上片习,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天捌肴,我揣著相機與錄音,去河邊找鬼藕咏。 笑死状知,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的孽查。 我是一名探鬼主播饥悴,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼盲再!你這毒婦竟也來了西设?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤答朋,失蹤者是張志新(化名)和其女友劉穎贷揽,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體梦碗,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡禽绪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了洪规。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片印屁。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖斩例,靈堂內(nèi)的尸體忽然破棺而出雄人,到底是詐尸還是另有隱情,我是刑警寧澤念赶,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布础钠,位于F島的核電站恰力,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏珍坊。R本人自食惡果不足惜牺勾,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望阵漏。 院中可真熱鬧驻民,春花似錦、人聲如沸履怯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叹洲。三九已至柠硕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間运提,已是汗流浹背蝗柔。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留民泵,地道東北人癣丧。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像栈妆,于是被迫代替她去往敵國和親胁编。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容