Java等待计时器线程完成
我是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
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
或其他类型的信号灯
或对象
监视器