Java 定时任务,删除取消 Task - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
vance
V2EX    Java

Java 定时任务,删除取消 Task

  •  
  •   vance 2018-10-23 10:19:53 +08:00 5448 次点击
    这是一个创建于 2552 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有个定时任务 scheduler.scheduleAtFixedRate,每隔 5s 去执行检查 redis 某个 key,存在则继续逻辑,不存在则不往下走,但这个定时任务还是一直运行的。有什么方法在 run 的时候如果 redis 的 key 不存在把这个 Task 删除掉或取消掉

    10 条回复    2018-10-24 08:54:20 +08:00
    codingKingKong
        1
    codingKingKong  
       2018-10-23 11:11:21 +08:00
    没有试验, 试试这个?
    ```java
    /**
    * Terminates this timer, discarding any currently scheduled tasks.
    * Does not interfere with a currently executing task (if it exists).
    * Once a timer has been terminated, its execution thread terminates
    * gracefully, and no more tasks may be scheduled on it.
    *
    * <p>Note that calling this method from within the run method of a
    * timer task that was invoked by this timer absolutely guarantees that
    * the ongoing task execution is the last task execution that will ever
    * be performed by this timer.
    *
    * <p>This method may be called repeatedly; the second and subsequent
    * calls have no effect.
    */
    public void cancel()
    ```
    gaius
        2
    gaius  
       2018-10-23 11:13:53 +08:00
    用一个线程做?
    kanepan19
        3
    kanepan19  
       2018-10-23 11:17:18 +08:00
    当然可以 任务提交的时候会返回一个期望, 然后 future.cancel()
    D3EP
        4
    D3EP  
       2018-10-23 12:09:49 +08:00
    每次执行完 task 再去注册 task 就行了。
    D3EP
        5
    D3EP  
       2018-10-23 12:10:04 +08:00
    直接用 schedule。
    vance
        6
    vance  
    OP
       2018-10-23 14:45:56 +08:00
    @D3EP 能否详细点,感谢
    honeycomb
        7
    honeycomb  
      &nbp;2018-10-23 18:43:08 +08:00 via Android
    @vance 请仔细看 scheduler 的文档,里面提供的执行一次( one shot )的方法可以轻松的达到楼主的目的。

    比如楼上 @kanepan19 提到的,你让它跑一个 callable 就能有返回值,然后下一个 trigger 根据这个返回值决定下一次怎么 schedule。

    如果只用 runnable,找一个线程安全的变量来充当 flag 的作用。

    还有更复杂的办法,比如你直接在项目里引用 quartz 库来达成目的也是没有问题的。

    如果是 Java9,它的 forkjoin 也提供了一个定时触发异步人物的方法。
    D3EP
        8
    D3EP  
       2018-10-23 22:42:17 +08:00
    ```java
    public class Main {
    private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
    public static void main(String[] args) {
    EXECUTOR_SERVICE.schedule(Main::task, 1, TimeUnit.SECONDS);
    }
    public static void task(){
    if (true) {
    System.out.println("Continue");
    EXECUTOR_SERVICE.schedule(Main::task, 1, TimeUnit.SECONDS);
    }else {
    System.out.println("Exit");
    }
    }
    }
    ```
    mayowwwww
        9
    mayowwwww  
       2018-10-23 23:59:39 +08:00   1
    抛出一个运行时异常。
    lihongjie0209
        10
    lihongjie0209  
       2018-10-24 08:54:20 +08:00
    if(key not exist){

    logger.info("直接退出执行就好了, 搞那么复杂")

    return
    }
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2638 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 04:21 PVG 12:21 LAX 21:21 JFK 00:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86