【问题标题】:Java wait for Timer thread to completeJava 等待 Timer 线程完成
【发布时间】:2018-07-13 11:59:31
【问题描述】:

我是 Java 多线程的新手,我刚刚实现了 Timer 类以在特定的间隔时间执行方法。

这里是我的代码:

public static void main(String[] args) {

    Timer timer = new Timer();
    timer.schedule(new MyTimerTask(), 3000);

    //execute this code after timer finished
    System.out.println("finish");
}

private static class MyTimerTask extends TimerTask {

    @Override
    public void run() {
        System.out.println("inside timer");
    }

}

但是输出是这样的:

finish
inside timer

我想要这样:

inside timer
finish

那么如何等待计时器线程完成然后在主线程中继续执行代码?有什么建议吗?

【问题讨论】:

  • 如果您确实需要后台线程,请不要使用计时器。相反,只需调用Thread.sleep(...)。否则,如果您仍想做后台线程,则在 Timer 或由 Timer 触发的回调中调用"finish" inside
  • 使用CountLatch 或其他类型的SemaphoreObject 监视器

标签: java multithreading


【解决方案1】:

您的问题有些模糊,最好通过Java's Concurrency Tutorial 回答,但是...

你可以...

使用“监控锁”

public static void main(String[] args) {

    Object lock = new Object();
    Timer timer = new Timer();
    timer.schedule(new MyTimerTask(lock), 3000);

    synchronized (lock) {
        try {
            lock.wait();
        } catch (InterruptedException ex) {
        }
    }

    //execute this code after timer finished
    System.out.println("finish");
}

private static class MyTimerTask extends TimerTask {

    private Object lock;

    public MyTimerTask(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        System.out.println("inside timer");
        synchronized (lock) {
            lock.notifyAll();
        }
    }

}

你可以...

使用CountDownLatch...

public static void main(String[] args) {

    CountDownLatch cdl = new CountDownLatch(1);
    Timer timer = new Timer();
    timer.schedule(new MyTimerTask(cdl), 3000);

    try {
        cdl.await();
    } catch (InterruptedException ex) {
    }

    //execute this code after timer finished
    System.out.println("finish");
}

private static class MyTimerTask extends TimerTask {

    private CountDownLatch latch;

    public MyTimerTask(CountDownLatch lock) {
        this.latch = lock;
    }

    @Override
    public void run() {
        System.out.println("inside timer");
        latch.countDown();
    }

}

你可以...

使用回调或简单地调用 Timer 类中的方法

public static void main(String[] args) {

    CountDownLatch cdl = new CountDownLatch(1);
    Timer timer = new Timer();
    timer.schedule(new MyTimerTask(new TimerDone() {
        @Override
        public void timerDone() {
            //execute this code after timer finished
            System.out.println("finish");
        }
    }), 3000);
}

public static interface TimerDone {
    public void timerDone();
}

private static class MyTimerTask extends TimerTask {

    private TimerDone done;

    public MyTimerTask(TimerDone done) {
        this.done = done;
    }

    @Override
    public void run() {
        System.out.println("inside timer");            
        done.timerDone();
    }

}

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2015-02-15
    • 2015-08-29
    相关资源
    最近更新 更多