java timer暂停,暂停/停止和启动/持续恢复的Java 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();

}

}

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.

Thanks for your help!

解决方案

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.

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

Edit: The basic construct is:

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 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值