暂停/停止和启动/持续恢复的Java TimerTask的? [英] Pausing/stopping and starting/resuming Java TimerTask continuously?

查看:4033
本文介绍了暂停/停止和启动/持续恢复的Java TimerTask的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Java的TimerTask的一个简单的问题。如何暂停/基于一定的条件下恢复2 TimerTask的任务是什么?比如我有一个相互之间运行两个定时器。当某一条件满足第一定时器的任务内,第一定时器停止并启动第二定时器,并且当在一定的条件已经满足第二定时器的任务内同样的事情发生。下面的类正说明我的意思:

I have one simple question regarding Java TimerTask. How do I pause/resume two TimerTask tasks based on a certain condition? For example I have two timers that run between each other. When a certain condition has been met inside the task of first timer, the first timer stops and starts the second timer, and the same thing happens when a certain condition has been met inside the task of second timer. The class below shows exactly what I mean:

public class TimerTest {
    Timer timer1;
    Timer timer2;
    volatile boolean a = false;

    public TimerTest() {
        timer1 = new Timer();
        timer2 = new Timer();     
    }

    public void runStart() {
        timer1.scheduleAtFixedRate(new Task1(), 0, 1000);
    }

    class Task1 extends TimerTask {
        public void run() {
            System.out.println("Checking a");
            a = SomeClass.getSomeStaticValue();

            if (a) {
                // Pause/stop timer1, start/resume timer2 for 5 seconds
                timer2.schedule(new Task2(), 5000);
            }
        }
    }

    class Task2 extends TimerTask{
        public void run() {
            System.out.println("Checking a");
            a = SomeClass.getSomeStaticValue();

            if (!a) {
                // Pause/stop timer2, back to timer1
                timer1.scheduleAtFixedRate(new Task1(), 0, 1000);
            }

            // Do something...
        }
    }

    public static void main(String args[]) {
        TimerTest tt = new TimerTest();
        tt.runStart();      
    }
}

所以我的问题是,如何暂停定时器运行时定时器2 ,反之亦然,而定时器2 运行?性能和时序是我最关心的,因为这需要在另一个正在运行的线程来实现。顺便说一句,我想要实现在Android上,这些并发的计时器。

So my question is, how do I pause timer1 while running timer2 and vice versa while timer2 is running? Performance and timing is my main concern as this needs to be implemented inside another running thread. By the way I am trying to implement these concurrent timers on Android.

感谢您的帮助!

推荐答案

TimerTask.cancel()

请注意,调用此方法   的重复的run方法中   计时器任务绝对保证   计时器任务不会再运行。

Note that calling this method from within the run method of a repeating timer task absolutely guarantees that the timer task will not run again.

因此​​,一旦取消,就永远不会再运行。你会转而使用更现代化的<一个会更好href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html"><$c$c>ScheduledExecutorService (从Java 5 +)。

So once cancelled, it won't ever run again. You'd be better off instead using the more modern ScheduledExecutorService (from Java 5+).

编辑:的基本结构是:

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(runnable, 0, 1000, TimeUnit.MILLISECONDs);

但寻找到它没有办法取消这一次任务的开始,而无需关闭服务,这是一个有点奇怪的。

but looking into it there's no way of cancelling that task once its started without shutting down the service, which is a bit odd.

的TimerTask 可能是在这种情况下更容易,但你需要的时候你开始一起来创建一个新的实例。它不能被重复使用。

TimerTask might be easier in this case but you'll need to create a new instance when you start one up. It can't be reused.

另外,您可以封装每个任务作为一个单独的临时服务:

Alternatively you could encapsulate each task as a separate transient service:

final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
Runnable task1 = new Runnable() {
  public void run() {
    a++;
    if (a == 3) {
      exec.shutdown();
      exec = Executors.newSingleThreadScheduledExecutor();
      exec.scheduleAtFixedRate(task2, 0, 1000, TimeUnit.MILLISECONDS)
    }
  }
};
exec.scheduleAtFixedRate(task1, 0, 1000, TimeUnit.MILLISECONDS);

这篇关于暂停/停止和启动/持续恢复的Java TimerTask的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆