有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程Java等待计时器线程完成

我是Java多线程方面的新手,我只是实现了定时器类来在特定的时间间隔执行方法

这是我的代码:

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

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


共 (1) 个答案

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