Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要将执行速度降低至少5秒_Java_Arrays_Performance_Execution - Fatal编程技术网

Java 需要将执行速度降低至少5秒

Java 需要将执行速度降低至少5秒,java,arrays,performance,execution,Java,Arrays,Performance,Execution,我编写了一个Java程序,用于计算不同整数循环中的最大循环。我的代码非常快,在我的四核A8AMD处理器上可以在毫秒内产生结果 我想将执行速度至少降低5秒。有什么我可以添加到代码中来降低速度的吗 import java.util.Scanner; class SNASA { public static void main(String args[]) { long i,j,n,max=0, pos=0, x, y; long count=0;

我编写了一个Java程序,用于计算不同整数循环中的最大循环。我的代码非常快,在我的四核A8AMD处理器上可以在毫秒内产生结果

我想将执行速度至少降低5秒。有什么我可以添加到代码中来降低速度的吗

import java.util.Scanner;

class SNASA {
    public static void main(String args[]) {
        long i,j,n,max=0, pos=0, x, y;
        long count=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the positive integer :");
        while(sc.hasNextLong()) {
            x=System.currentTimeMillis();
            i = sc.nextLong();
            long arr[] = new long [60000000];
            for(long p=i/2;p<i;p++) {
                arr[(int)p]=0;
            }
            for(j=1;j<=i;j++) {
                n=j;
                count=0;
                tag:while(n!=1) {
                    if(n<60000000) {
                        if(arr[(int)(n-1)]!=0) {
                            count = count + arr[(int)(n-1)];
                            break tag;
                        }
                    }
                    if(n%2==0) {
                        n = n/2;
                        count++;
                    } else {
                        n = (2*n)+n+1;
                        count++;
                    }
                }
                if(j<60000000)
                    arr[(int)(j-1)]=count;
                if(count>max) { 
                    max=count; pos=j; 
                }
                count=0;
            }
            y=System.currentTimeMillis();
            System.out.println("Maximum Cycle length occurs at "+pos+" and the number of steps in cycle "+max+"\n Total time taken is "+(y-x)+"ms");
        }
    }
}
import java.util.Scanner;
SNASA类{
公共静态void main(字符串参数[]){
长i,j,n,max=0,pos=0,x,y;
长计数=0;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入正整数:”);
while(sc.hasNextLong()){
x=系统.currentTimeMillis();
i=sc.nextLong();
长arr[]=新长[60000000];
对于(长p=i/2;p用于添加5秒的延迟

根据系统计时器和调度程序的精度和准确性,使当前执行的线程休眠(暂时停止执行)指定的毫秒数


您可以暂停执行5秒钟,具体如下所示:

Thread.sleep(5000);
这将恰好在该行停止程序,等待指定的持续时间,然后继续

还要注意的是

由于程序将在放置这一行的任何位置暂停,如果您希望总延迟量取决于您运行的迭代次数,则可以将其保留在循环中,并且每次循环运行时都会有延迟

for (int i = 0; i < 10; i++)
{
    Thread.sleep(1000); //Delays 1s 10 times; 10s overall delay
}
for(int i=0;i<10;i++)
{
Thread.sleep(1000);//延迟1s 10次;总延迟10s
}

答案显然包括降低CPU的时钟速度。谢谢你的回答。:@admdrew我可能会因为这么说而大发雷霆,但我认为时钟速度足够低的CPU无法在合理的时间内启动JVM。:)你希望该线程具有什么特定功能。sleep()不提供?请解释为什么您需要放慢流程。我们没有一个神奇的水晶球来解释您对工作的具体要求,以完全理解问题。