如何只运行java函数30分钟

如何只运行java函数30分钟,java,function,time,timer,Java,Function,Time,Timer,我需要创建一个只运行30分钟的java函数,并在30分钟结束时执行一些操作。但如果满足适当的条件,它也应该能够在给定的时间之前自行终止。我不希望函数处于休眠状态,因为它应该收集数据,所以没有休眠线程 谢谢使用System.currentTimeMillis()获取开始时间,计算停止时间,并在收集要收集的数据时不时检查当前时间。另一种方法是将计时器和数据收集分离,这样它们中的每一个都可以在自己的线程中运行 对于更具体的答案,如果您能说出您正在收集的数据以及如何收集这些数据,这将非常有用。使用: 类

我需要创建一个只运行30分钟的java函数,并在30分钟结束时执行一些操作。但如果满足适当的条件,它也应该能够在给定的时间之前自行终止。我不希望函数处于休眠状态,因为它应该收集数据,所以没有休眠线程


谢谢

使用
System.currentTimeMillis()
获取开始时间,计算停止时间,并在收集要收集的数据时不时检查当前时间。另一种方法是将计时器和数据收集分离,这样它们中的每一个都可以在自己的线程中运行

对于更具体的答案,如果您能说出您正在收集的数据以及如何收集这些数据,这将非常有用。

使用:


类似这样的方法会奏效:

long startTime = System.currentTimeMillis();
long maxDurationInMilliseconds = 30 * 60 * 1000;

while (System.currentTimeMillis() < startTime + maxDurationInMilliseconds) {
    // carry on running - 30 minutes hasn't elapsed yet

    if (someOtherConditionIsMet) {
        // stop running early
        break;
    }
}
long startTime=System.currentTimeMillis();
长最大持续时间毫秒=30*60*1000;
而(System.currentTimeMillis()
现代的
java.util.concurrent
方式将使用。有几个
invoke
方法需要超时

以下是一个启动示例:

public static void main(String args[]) throws Exception {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.invokeAll(Arrays.asList(new Task()), 30, TimeUnit.MINUTES);
    executor.shutdown();
}
其中
Task
如下所示:

public class Task implements Callable<String> {

    @Override
    public String call() {
        // Just a dummy long running task.
        BigInteger i = new BigInteger("0");
        for (long l = 0; l < Long.MAX_VALUE; l++) {
            i.multiply(new BigInteger(String.valueOf(l)));

            // You need to check this regularly..
            if (Thread.interrupted()) {
                System.out.println("Task interrupted!");
                break; // ..and stop the task whenever Thread is interrupted.
            }
        }
        return null; // Or whatever you'd like to use as return value.
    }

}
公共类任务实现可调用{
@凌驾
公共字符串调用(){
//只是一个虚拟的长时间运行的任务。
BigInteger i=新的BigInteger(“0”);
对于(长l=0;l
另见:

回答得好。在每次迭代中加入一个短线程.sleep()也是个好主意,除非您想100%地接受CPU建议:将
startTime+maxDurationInMilliseconds
分配给一个变量,并在循环条件中使用该变量。这样加法就不会执行1800000次:)@Anita:我可以想象虚拟机会将该计算从条件中提升出来,然后只计算一次。毕竟,
startTime
maxDurationInMilliseconds
对于正在运行的线程来说是本地的。@mikera:取决于该方法所做的工作。它可能涉及大量I/O,或者可能正在发出请求并等待响应。这个问题有点模糊。当然,如果方法只是等待(最多)30分钟,那么是的,它应该在每次迭代中休眠一段时间。这样更好,它不会运行上千次:D不能从Timer类型中静态引用非静态方法计划(TimerTask,long),这不就是在30分钟后运行任务吗。。。但是,
系统#exit()
将杀死整个JVM,而不是“运行”30分钟。不确定这是否是OP的想法。@BaluC的确,需要一个更复杂的机制,但我希望这会给他一个好的开始。
public class Task implements Callable<String> {

    @Override
    public String call() {
        // Just a dummy long running task.
        BigInteger i = new BigInteger("0");
        for (long l = 0; l < Long.MAX_VALUE; l++) {
            i.multiply(new BigInteger(String.valueOf(l)));

            // You need to check this regularly..
            if (Thread.interrupted()) {
                System.out.println("Task interrupted!");
                break; // ..and stop the task whenever Thread is interrupted.
            }
        }
        return null; // Or whatever you'd like to use as return value.
    }

}