简体   繁体   English

Java等待Timer线程完成

[英]Java wait for Timer thread to complete

I am new in Java multithreading, i just implemented Timer class to execute method on specific interval time. 我是Java多线程技术的新手,我只是实现了Timer类,以在特定的间隔时间执行方法。

here my code : 这是我的代码:

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

}

but the output like this : 但输出是这样的:

finish
inside timer

I want it like this : 我想要这样:

inside timer
finish

so how to wait timer thread to complete and then continue execute code in main thread? 那么如何等待计时器线程完成,然后继续在主线程中执行代码? any suggestion? 有什么建议吗?

Your question is somewhat vague and may be better answered via Java's Concurrency Tutorial , however... 您的问题有些含糊,可以通过Java的Concurrency Tutorial更好地回答,但是...

You could... 你可以...

Use a "monitor lock" 使用“显示器锁”

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

}

You could... 你可以...

Use a CountDownLatch ... 使用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();
    }

}

You could... 你可以...

Use a callback or simply call a method from the Timer class 使用回调或仅从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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM