在Java中特定时间后如何中断方法?

帕维尔:

我需要编写一个简单的程序,该程序可以打印最多给定数字但不超过5秒的质数。经过一段时间后,是否可以使用某种计时器来中断方法?(但如果打印时间少于5秒,则不会中断)。提前致谢。

我的代码:

public class Primes {
    private static boolean checkIfPrime(int x) {
        if (x == 2) return true;
        if (x % 2 == 0) return false;
        int sqrt = (int) Math.sqrt(x) + 1;
        for (int i = 3; i < sqrt; i = i + 2) if (x % i == 0) return false;
        return true;
    }

    private static void printPrimesAndOperationTime(int n) {
        long start = System.nanoTime();
        for (int i = 2; i <= n; i++) if (checkIfPrime(i)) System.out.println(i);
        long end = System.nanoTime();

        long timeResult = end - start;
        System.out.println("Printing time = " + timeResult + " [ns] => "
                + Math.round(timeResult * 100.0 / 1000000) / 100.0 + " [ms]");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        printPrimesAndOperationTime(n);
    }
}
QuickSilver:

使用Java并发API可以解决上述问题。请找到内联注释以进行代码遍历。

import java.util.Scanner;
import java.util.concurrent.*;

public class TimeoutInterval {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newSingleThreadExecutor(); // Start Single thread executor
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Future future = executor.submit(new Primes(n)); // Find prime no.
        try {
            future.get(5, TimeUnit.SECONDS); // Set the time out of the prime no. search task
            executor.shutdown();
        } catch (TimeoutException e) {
            executor.shutdown();
            System.out.println("Terminated!");
        }

        executor.shutdownNow();
    }
}

class Primes  implements Runnable {
    private final int number;

    Primes(int number) {
        this.number = number;
    }
    @Override
    public void run() {
        System.out.println("Started..");
        printPrimesAndOperationTime(number);
        System.out.println("Finished!");
    }

    private static boolean checkIfPrime(int x) {
        if (x == 2) return true;
        if (x % 2 == 0) return false;
        int sqrt = (int) Math.sqrt(x) + 1;
        for (int i = 3; i < sqrt; i = i + 2) if (x % i == 0) return false;
        return true;
    }

    private static void printPrimesAndOperationTime(int n) {
        long start = System.nanoTime();
        for (int i = 2; i <= n && !Thread.interrupted(); i++) if (checkIfPrime(i)) {
            System.out.println(i);
        }
        long end = System.nanoTime();

        long timeResult = end - start;
        System.out.println("Printing time = " + timeResult + " [ns] => "
                + Math.round(timeResult * 100.0 / 1000000) / 100.0 + " [ms]");
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java:如何在特定时间后“中断”开关柜?

如何在Java中的特定时间调用方法?

如何为Java中的方法确定特定时间

在特定时间后如何删除SKSpriteNodes

如何在特定时间使用Java触发方法?

如何在UTC Java中获取今天的特定时间

如何在java中执行特定时间的代码?

如何在特定时间后运行功能,然后在特定时间后将其关闭?

在特定时间后仅更新阵列中特定项目的最佳方法是什么?

在Java中的特定时间安排任务

一定时间后中断功能

如何在Django中的特定时间后自动从数据库中删除记录

在ASP.NET中配置数据库的行以在特定时间后删除的最佳方法

Grep log4j用于Java中特定时间戳记后的行

从特定时间获取Java中的修改后的Notes文档

在一定时间间隔后,如何在jquery中重复调用特定的URL?

如何在特定时间后以每 30 分钟为增量在 Python 中安排我的代码

如何在特定时间后自动从 Firestore 中删除数据?

在特定时间间隔后如何显示react.js中的每个数组对象?

在定义了特定时间后如何在多线程中中止线程?

在特定时间段后中断未知的长期运行功能

在特定时间间隔后如何检查文件是否存在

特定时间后如何停止纹理滚动

在特定时间运行后,如何使脚本自行终止?

在特定时间后如何自动关闭插页式广告?

如何搜索特定时间后修改的文件?

特定时间段后如何显示活动

如何在特定时间后遍历每个对象?

在特定时间后如何自动关闭系统?