Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java等待计时器线程完成_Java_Multithreading - Fatal编程技术网

Java等待计时器线程完成

Java等待计时器线程完成,java,multithreading,Java,Multithreading,我是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"); } pr

我是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
我想要这样:

finish
inside timer
inside timer
finish

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

你的问题有点含糊不清,最好通过……来回答

你可以。。。 使用“监视器锁”

你可以。。。 使用
倒计时闩锁

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

}

你的问题有点含糊,也许最好通过……来回答

你可以。。。 使用“监视器锁”

你可以。。。 使用
倒计时闩锁

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

}

如果您确实需要后台线程,请不要使用计时器。相反,只需调用
Thread.sleep(…)
。否则,如果仍要执行后台线程,请在计时器内或计时器触发的回调中调用
完成
。使用
计数锁存器
或其他类型的
信号灯
对象
监视器如果确实要执行后台线程,请不要使用计时器。相反,只需调用
Thread.sleep(…)
。否则,如果仍要执行后台线程,则在计时器内或计时器触发的回调中调用
“finish”
。使用
CountLatch
或其他类型的
信号灯
对象
监视器