Java: Wait for TimerTask to complete before contin

2019-02-28 18:58发布

I'm having a bit of an annoying problem. Right now, I have a snippet of code that starts a thread, sets a timer within that thread, and then exits that thread and continues with its life. My intent here was for the program to wait for the TimerTask to complete before continuing with code flow. However, obviously, setting up a new TimerTask doesn't pause execution to wait for the timer to run down.

How do I set this up so that my code reaches the TimerTask, waits for the TimerTask to expire, and then continues? Should I even be using a Timer at all? I've looked everywhere for a solution, but I Can't seem to find one.

timer = new Timer();
    Thread t = new Thread(new Runnable(){
        boolean isRunning = true;
        public void run() {
            int delay = 1000;
            int period = 1000;
            interval = 10;
            timerPanel.setText(interval.toString());

            //Scheduling the below TimerTask doesn't wait
            //for the TimerTask to finish before continuing
            timer.scheduleAtFixedRate(new TimerTask() { 

                public void run() {
                    timerPanel.setText(setInterval().toString());
                }
            }, delay, period);

            System.out.println("Thread done.");
        }
    });
    t.start();

    try {
        t.join(); //doesn't work as I wanted
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    endTask();

Thanks in advance.

EDIT: Sorry for the confusion about the repeated task. I need the task to repeat because it's a countdown timer that pulses every second from 10 to 0. The function setInterval() eventually cancels the timer. Here's the relevant code:

private final Integer setInterval() {
    if (interval == 1)
        timer.cancel();
    return --interval;
}

3条回答
手持菜刀,她持情操
2楼-- · 2019-02-28 19:11

I believe a CountDownLatch will do what you want.

final CountDownLatch latch = new CountDownLatch(10);
int delay = 1000;
int period = 1000;

timerPanel.setText(Long.toString(latch.getCount()));

timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    public void run() {
        latch.countDown();
        timerPanel.setText(Long.toString(latch.getCount()));
    }
}, delay, period);

try {
    latch.await();
}
catch (InterruptedException e) {
    e.printStackTrace();
}

timer.cancel();
查看更多
神经病院院长
3楼-- · 2019-02-28 19:31

Use a Semaphore. Initialize it before declaring the timer task, with 0 permits. In the timer task, use a try/finally block to release the semaphore. In the main thread, acquire the permit from the semaphore.

In your code, join works as specified since it waits for the thread to finish. And no, using a thread for this is not necessary. If you really want to block until a certain time, you don't need a Timer. Get the current time, compute the millis until the future time, and sleep().

查看更多
神经病院院长
4楼-- · 2019-02-28 19:34

You should use Thread.sleep function instead of TimeTask to halt execution.

TimerTask is not meant to halt execution, its like a clock running in background. So for your requirement you should go for Thread.sleep.

查看更多
登录 后发表回答