Java 需要帮助勇敢地报告这个项目的结果吗

Java 需要帮助勇敢地报告这个项目的结果吗,java,arrays,multithreading,performance,runtime,Java,Arrays,Multithreading,Performance,Runtime,因此,我制作了一个小程序来测试java中的多线程,并比较使用while循环扩展阵列所需的时间,然后创建多个线程并运行这些线程。我不确定程序结束时我得到的数字,所以我想知道我是否在某个时候犯了一个愚蠢的错误,把一些事情搞砸了,得到了完全不同的数字 代码如下: import java.util.Scanner; public class arrayScaling { public static void main(String[] args) throws Inter

因此,我制作了一个小程序来测试java中的多线程,并比较使用while循环扩展阵列所需的时间,然后创建多个线程并运行这些线程。我不确定程序结束时我得到的数字,所以我想知道我是否在某个时候犯了一个愚蠢的错误,把一些事情搞砸了,得到了完全不同的数字

代码如下:

    import java.util.Scanner;

    public class arrayScaling {

      public static void main(String[] args) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount of number you want the program to generate:");
        int numOfNumbs = input.nextInt();
        int [] arrayForNumbers = new int [numOfNumbs];
        int [] newArrayForNumbers = new int [numOfNumbs];

        for (int i = 0; i < arrayForNumbers.length; i++) {
            arrayForNumbers[i] = (int) ((Math.random() * 25) + 1);
        }

        long startTime = System.nanoTime();
        for (int i = 0; i < arrayForNumbers.length; i++) {
            newArrayForNumbers[i] = newArrayForNumbers[i] * 3;

        }

        long endTime = System.nanoTime();
        System.out.println();

        long totalExecutionTime = endTime-startTime;

        System.out.println("Time it takes execute scaling is " + 
                totalExecutionTime + " nanoseconds");
        System.out.println();

        int numOfNumLeftOver = numOfNumbs % 5;
        int numOfNumDivided = numOfNumbs / 5;

        int [] temp = null;
        int [] temp2 = null;
        int [] temp3 = null;
        int [] temp4 = null;
        int [] temp5 = null;

        MyThread thread1 = new MyThread (numOfNumbs/5);
        MyThread thread2 = new MyThread (numOfNumbs/5);
        MyThread thread3 = new MyThread (numOfNumbs/5);
        MyThread thread4 = new MyThread (numOfNumbs/5);
        MyThread thread5;
        if (numOfNumLeftOver != 0) {
            numOfNumDivided = numOfNumDivided + numOfNumLeftOver;
            thread5 = new MyThread (numOfNumDivided);   
        }
        else {
            thread5 = new MyThread (numOfNumbs/5);
        }

        int tempNum = 0;
        for ( int i = 0; i < thread1.getArray().length; i ++) {
            temp = thread1.getArray();
            temp[tempNum] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread2.getArray().length; i ++) {
            temp2 = thread2.getArray();
            temp2[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread3.getArray().length; i ++) {
            temp3 = thread3.getArray();
            temp3[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread4.getArray().length; i ++) {
            temp4 = thread4.getArray();
            temp4[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread5.getArray().length; i ++) {
            temp5 = thread5.getArray();
            temp5[i] = arrayForNumbers[tempNum];
            tempNum++;
        }       
        thread1.setArray(temp);
        thread2.setArray(temp2);
        thread3.setArray(temp3);
        thread4.setArray(temp4);
        thread5.setArray(temp5);

        long startTime2 = System.nanoTime();
        thread1.start(); 
        thread2.start(); 
        thread3.start(); 
        thread4.start();
        thread5.start();
        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        thread5.join();
        long endTime2 = System.nanoTime();

        long newTotalExecutionTime = endTime2 - startTime2;
        System.out.println("Time it takes execute scaling w/ multiple threads is " + 
                newTotalExecutionTime + " nanoseconds");

        if (newTotalExecutionTime < totalExecutionTime) {
            System.out.println("Multithreading was more effective");
        }
        else if (totalExecutionTime < newTotalExecutionTime) {
            System.out.println("The original algorithm was more effective");
        }
        else if (totalExecutionTime == newTotalExecutionTime) {
            System.out.println("Both method worked at the same speed");
        }
        input.close();
    }

}



    public class MyThread extends Thread {
    private int [] array;
    private int [] scaleArray;

    public MyThread(int size) {
        array = new int [size];
        scaleArray = new int [size];
    }

    public int[] getArray() {
        return array;
    }

    public void setArray(int[] array) {
        this.array = array;
    }

    public int[] getScaleArray() {
        return scaleArray;
    }

    public void setScaleArray(int[] scaleArray) {
        this.scaleArray = scaleArray;
    }

    public void run () {
        for (int z = 0; z < array.length; z++){
            scaleArray[z] = 3 * array[z];
        } 
    }

}
import java.util.Scanner;
公共类数组缩放{
公共静态void main(字符串[]args)引发InterruptedException{
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入希望程序生成的数字量:”);
int numoffnumbs=input.nextInt();
int[]arrayForNumbers=新的int[numfNumbs];
int[]newArrayForNumbers=新的int[numfNumbs];
for(int i=0;i
这个程序的输出是:

输入您希望程序生成的数字量: 十六,

执行缩放所需的时间为893纳秒

使用多线程执行缩放所需的时间为590345纳秒
原来的算法更有效

你的结果一点也不让我吃惊。创建线程、启动线程、等待线程完成等等都有很多开销。别忘了,590345ns还不到一毫秒;但是,这大部分是与随机线程有关,而不是与数字相乘有关


如果您想看到程序的线程部分优于其他部分,请尝试生成远远超过16个数字。

我明白了。非常感谢。这只是一个测试程序,所以我不太担心数据池的大小。