Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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.lang.RuntimeException";。我怎样才能修好它? import java.util.concurrent.ExecutionException; 导入java.util.concurrent.ExecutorService; 导入java.util.concurrent.Executors; 导入java.util.concurrent.Future; 公共类Fibonacci{ 私有静态长[]值; 公共静态void main(字符串args[])引发InterruptedException{ int n; 试试{ n=整数.parseInt(args[0]); }捕获(例外e){ 抛出新的RuntimeException(“数字n”); } 值=新长[n+1]; 长启动=System.nanoTime(); 系统输出打印(“动态规划=“+fibon(n)); long end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); 系统输出打印(“序列=”+序列(n)); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); //int nThreads=Runtime.getRuntime().availableProcessors(); int=30; ExecutorService ExecutorService=执行者 .newFixedThreadPool(n个线程); int结果; 试试{ 结果=fibonacciSum(n,executorService);} 捕获(执行例外){ 抛出新的RuntimeException(“线程中断”);} System.out.print(“多线程=”+结果); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); } 公共静态长fibon(int n){ 值[0]=1; 值[1]=1; 如果(n_Java_Multithreading_Fibonacci - Fatal编程技术网

我在运行以下代码后出现此错误;“线程中的异常”;“主要”;java.lang.RuntimeException";。我怎样才能修好它? import java.util.concurrent.ExecutionException; 导入java.util.concurrent.ExecutorService; 导入java.util.concurrent.Executors; 导入java.util.concurrent.Future; 公共类Fibonacci{ 私有静态长[]值; 公共静态void main(字符串args[])引发InterruptedException{ int n; 试试{ n=整数.parseInt(args[0]); }捕获(例外e){ 抛出新的RuntimeException(“数字n”); } 值=新长[n+1]; 长启动=System.nanoTime(); 系统输出打印(“动态规划=“+fibon(n)); long end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); 系统输出打印(“序列=”+序列(n)); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); //int nThreads=Runtime.getRuntime().availableProcessors(); int=30; ExecutorService ExecutorService=执行者 .newFixedThreadPool(n个线程); int结果; 试试{ 结果=fibonacciSum(n,executorService);} 捕获(执行例外){ 抛出新的RuntimeException(“线程中断”);} System.out.print(“多线程=”+结果); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); } 公共静态长fibon(int n){ 值[0]=1; 值[1]=1; 如果(n

我在运行以下代码后出现此错误;“线程中的异常”;“主要”;java.lang.RuntimeException";。我怎样才能修好它? import java.util.concurrent.ExecutionException; 导入java.util.concurrent.ExecutorService; 导入java.util.concurrent.Executors; 导入java.util.concurrent.Future; 公共类Fibonacci{ 私有静态长[]值; 公共静态void main(字符串args[])引发InterruptedException{ int n; 试试{ n=整数.parseInt(args[0]); }捕获(例外e){ 抛出新的RuntimeException(“数字n”); } 值=新长[n+1]; 长启动=System.nanoTime(); 系统输出打印(“动态规划=“+fibon(n)); long end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); 系统输出打印(“序列=”+序列(n)); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); start=System.nanoTime(); //int nThreads=Runtime.getRuntime().availableProcessors(); int=30; ExecutorService ExecutorService=执行者 .newFixedThreadPool(n个线程); int结果; 试试{ 结果=fibonacciSum(n,executorService);} 捕获(执行例外){ 抛出新的RuntimeException(“线程中断”);} System.out.print(“多线程=”+结果); end=System.nanoTime(); System.out.println(“\t time=“+(结束-开始)+”ns”); } 公共静态长fibon(int n){ 值[0]=1; 值[1]=1; 如果(n,java,multithreading,fibonacci,Java,Multithreading,Fibonacci,要从标准输入流读取,请从System.in流读取。传递给main方法的字符串数组是命令行参数,而不是用户输入数据。运行程序时,应在命令中提供参数。使用以下命令,其中“n”应该是程序要生成的斐波那契数的整数 java Fibonacci n确保您使用java Fibonacci启动程序,否则您会遇到此异常,因为您的程序例外一个参数。您如何运行您的程序?顺便问一句,您认为会抛出新的运行时异常(“数字n”)do?请将示例代码减少到一个最小值。除非问题是关于这方面的,否则最小值的代码通常不应包含计时例

要从标准输入流读取,请从System.in流读取。传递给main方法的字符串数组是命令行参数,而不是用户输入数据。

运行程序时,应在命令中提供参数。使用以下命令,其中“n”应该是程序要生成的斐波那契数的整数


java Fibonacci n

确保您使用
java Fibonacci
启动程序,否则您会遇到此异常,因为您的程序例外一个参数。您如何运行您的程序?顺便问一句,您认为
会抛出新的运行时异常(“数字n”)
do?请将示例代码减少到一个最小值。除非问题是关于这方面的,否则最小值的代码通常不应包含计时例程。
import java.util.concurrent.ExecutionException; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 

public class Fibonacci { 


    private static long[] value; 
    public static void main(String args[]) throws InterruptedException { 
        int n;
        try { 
            n = Integer.parseInt(args[0]); 
        } catch (Exception e) { 
            throw new RuntimeException(" number n"); 
        }

        value = new long[n + 1]; 
        long start = System.nanoTime(); 
        System.out.print("Dynamic Programming = " + fibon(n)); 
        long end = System.nanoTime(); 

        System.out.println("\t time = " + (end - start) + "ns"); 
        start = System.nanoTime(); 

        System.out.print("Sequence = " + Sequence(n));
        end = System.nanoTime(); 

        System.out.println("\t time = " + (end - start) + "ns"); 
        start = System.nanoTime(); 

        //int nThreads = Runtime.getRuntime().availableProcessors(); 
        int nThreads = 30; 
         ExecutorService executorService = Executors 
                .newFixedThreadPool(nThreads); 
        int result; 
        try { 
            result = fibonacciSum(n, executorService); }
        catch (ExecutionException e) { 
            throw new RuntimeException("Thread Interuppted "); } 

        System.out.print(" MultiThreading = " + result); 
        end = System.nanoTime(); 
        System.out.println("\t time = " + (end - start) + "ns"); 
    } 

    public static long fibon(int n) { 
        value[0] = 1; 
        value[1] = 1; 
            if (n <= 2) 
            return 1;

          else if (value[n - 1] != 0) 
          return value[n]; 

        for (int j = 2; j <= n; j++) { 
           value[j] = fibon(j - 2) + fibon(j - 1); } 
            return value[n]; 
    } 

    public static long Sequence(int n) { 
        if (n <= 2) 
            return 1; 
        else 
            return (Sequence(n - 1) + Sequence(n - 2)); 
    } 

    private static class FibonacciThread implements Runnable { 
        int index; 
        int result; 
        ExecutorService executorService; 
        public FibonacciThread(int index) { 
            this.index = index; 
        } 
         public void run() { 

            try { 
                this.result = fibonacciSum(index, executorService); 
            } catch (Exception e) { 
                throw new RuntimeException("Thread interupted"); 
            } 
        } 
    } 

    private static int fibonacciSum(int index, ExecutorService executorService) 
      throws InterruptedException, ExecutionException { 
        if (index == 1 || index == 2) { 
            return 1; 
        } else {   

            FibonacciThread fibonacciThread1 = new FibonacciThread(index - 2); 
            fibonacciThread1.executorService=executorService; 
            Future future = executorService.submit(fibonacciThread1); 
            Object object = future.get(); 
            int resultPart2 = fibonacciSum(index - 1, executorService); 
            int result = fibonacciThread1.result + resultPart2; 
           //executorService.shutdown(); 
           return result; 
        } 
    } 
}