繁体   English   中英

在Java中运行代码仅60秒

[英]run a code in java for only 60 seconds

我只需要运行以下代码60秒钟

该代码将完美运行2秒钟,然后每5秒钟重复一次。

但在这里我只需要60秒

        int delay = 3000; // delay for 3 sec. 
        int period = 5000; // repeat every 5 sec. 
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                System.out.println("Would it run?"+System.currentTimeMillis());
            }
        }, delay, period);

请让我知道该怎么做?

您可以使用ScheduledExecutorService轻松完成此操作:

  • 安排任务
  • 检索ScheduledFuture
  • 60秒后取消未来:
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

final Runnable task = new Runnable() {
    public void run() {
        System.out.println("Would it run?"+System.currentTimeMillis());
    }
};
final ScheduledFuture<?> handle =
        scheduler.scheduleAtFixedRate(task, 2, 5, TimeUnit.SECONDS);
scheduler.schedule(new Runnable() {
    public void run() { handle.cancel(true); }
}, 60, SECONDS);
public class test{
static int delay = 3000; // delay for 3 sec. 
       static int period = 5000; // repeat every 5 sec. 
        static int totaltime = 0;   
public static void main(String ar[]){


        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                test.totaltime+= ((test.delay+test.period)/1000);

                if(test.totaltime > 60)System.exit(0);
                System.out.println("Would it run?"+System.currentTimeMillis());

            }
        }, test.delay, test.period);
}
    }

您需要计算蜜蜂已进行几次,然后取消:

import java.util.Timer;
import java.util.TimerTask;


public class Waiting {
    public static int times;

    public static void main(String[] args)
    {
        int delay = 3000;  // delay for 3 sec. 
        int period = 5000; // repeat every 5 sec. 
        times = 60000 / ( delay + period );


        Timer timer = new Timer();
        timer.scheduleAtFixedRate( new TimerTask() {

            public void run() {
                System.out.println( "Times remaining: " + Waiting.times );

                --Waiting.times;
                if ( Waiting.times == 0 ) {
                    this.cancel();
                    System.exit( 0 );
                }
            }
        }, delay, period); 
    }
}

希望这可以帮助。

您可以设置一个计时器,将其设置为60,然后设置以下条件:

int time = 3;

 timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
     if(time<60){
                System.out.println("Would it run?"+System.currentTimeMillis());
                time = time + 5;
     }
     else{ timer.stop(); }
            }
        }, delay, period);

在所需的重复次数之后,只需从run方法调用cancel

老实说,我从未使用过计时器,但是如果您想在一段时间内运行任务,为什么不使用public void schedule(TimerTask task,long delay, long period)

如果我不能正确理解,您想立即运行任务,持续60秒,因此:

timer.schedule(myTask,0,60000);

应该为您效劳,但是正如我所说,我从不使用计时器,因此我不确定100%是否会正常工作

暂无
暂无

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

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