Java 当循环Akka调度程序';s Runnable花费的时间比循环时间长?

Java 当循环Akka调度程序';s Runnable花费的时间比循环时间长?,java,akka,Java,Akka,这是给爪哇阿卡的 假设我执行调度程序调用: ... FiniteDuration interval = new FiniteDuration(30L, TimeUnit.SECONDS); actorSystem.scheduler().schedule(delay, interval, action, actorSystem.dispatcher()); 其中action属于类somerunable: public class SomeRunnable implements Runnable

这是给爪哇阿卡的

假设我执行调度程序调用:

...
FiniteDuration interval = new FiniteDuration(30L, TimeUnit.SECONDS);
actorSystem.scheduler().schedule(delay, interval, action, actorSystem.dispatcher());
其中
action
属于类
somerunable

public class SomeRunnable implements Runnable {
    @Override
    public void run() {
        Thread.sleep(60000);
    }
}
因此,每30秒,调度程序将运行该操作,该操作将休眠60秒。如果我运行代码,第一次睡眠结束时,第二次睡眠立即开始。这里到底发生了什么?为什么会发生这种情况


对不起,如果这太模糊了。谢谢

您可以这样做,启动另一个线程并停止上一个线程

    final Thread previousthread[]=new Thread[1];
    FiniteDuration interval = new FiniteDuration(30L, TimeUnit.SECONDS);

    actorSystem.scheduler().schedule(Duration.Zero(), interval, new Runnable() {

        @Override
        public void run() {
            System.out.println("next cycle");
            if(previousthread[0]!=null){
                try{
                previousthread[0].interrupt();
                System.out.println("previous thread stopped");
                }catch(InterruptedException e){
                    // e.printStackTrace();
                }
            }
            Thread th = new Thread(new Runnable() {

                @Override
                public void run() {
                    try{
                        System.out.println("before sleep");
                        Thread.sleep(60000);

                    // put your required code here

                        System.out.println("thread complete");
                    }catch(Exception e){
                        e.printStackTrace();
                    }

                }
            });
            th.start();
            previousthread[0]=th;

        }
    }, actorSystem.dispatcher()); 

您可以这样做,启动另一个线程并停止前一个线程

    final Thread previousthread[]=new Thread[1];
    FiniteDuration interval = new FiniteDuration(30L, TimeUnit.SECONDS);

    actorSystem.scheduler().schedule(Duration.Zero(), interval, new Runnable() {

        @Override
        public void run() {
            System.out.println("next cycle");
            if(previousthread[0]!=null){
                try{
                previousthread[0].interrupt();
                System.out.println("previous thread stopped");
                }catch(InterruptedException e){
                    // e.printStackTrace();
                }
            }
            Thread th = new Thread(new Runnable() {

                @Override
                public void run() {
                    try{
                        System.out.println("before sleep");
                        Thread.sleep(60000);

                    // put your required code here

                        System.out.println("thread complete");
                    }catch(Exception e){
                        e.printStackTrace();
                    }

                }
            });
            th.start();
            previousthread[0]=th;

        }
    }, actorSystem.dispatcher()); 

调度程序在有限的周期内完成,但它会等待前一个线程完成,因此在睡眠完成后,下一个线程立即启动。但请解释一下你们想要实现什么,你们想要每30秒运行一次调度程序,即使之前的线程并没有完成?或者你想在每个线程完成后等待30秒?那么,为了澄清一下,你是说调度程序将等到前一个线程完成执行后再开始下一个线程?我希望每30秒运行一次调度程序,即使上一个线程没有完成。但是,如果前一个线程没有完成,我想杀死它。调度程序在有限的周期内完成,但它会等待前一个线程完成,因此在睡眠完成后立即启动下一个线程。但请解释一下你们想要实现什么,你们想要每30秒运行一次调度程序,即使之前的线程并没有完成?或者你想在每个线程完成后等待30秒?那么,为了澄清一下,你是说调度程序将等到前一个线程完成执行后再开始下一个线程?我希望每30秒运行一次调度程序,即使上一个线程没有完成。但是,如果前一个线程没有完成,我想杀死它。