Java中的多线程基准测试

Java中的多线程基准测试,java,exception-handling,Java,Exception Handling,我正在做一个科学展项目,在这个项目中,我正在测试有多少额外的处理核心提高了计算速度。这是到目前为止我编写的多核基准测试,但也存在一些问题。“thread#.join();”行都会抛出错误。有人能帮我修一下吗?请记住,我是一个初学者,所以请您用非常简单的术语解释如何修复它,或者通过将修复后的代码放在您的回复中向我展示。以下是我所拥有的: import java.util.Calendar; import java.text.SimpleDateFormat; import java.util.Sc

我正在做一个科学展项目,在这个项目中,我正在测试有多少额外的处理核心提高了计算速度。这是到目前为止我编写的多核基准测试,但也存在一些问题。“thread#.join();”行都会抛出错误。有人能帮我修一下吗?请记住,我是一个初学者,所以请您用非常简单的术语解释如何修复它,或者通过将修复后的代码放在您的回复中向我展示。以下是我所拥有的:

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.text.*;
import java.util.concurrent.*;

public class SciFair 
{

    /**
     * Numa Robertson
     * 1/13/11
     * Science Fair 2011
     */

    public static String now(String dateFormat) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(cal.getTime());
    }

    public static void main(String[] args) {
        Scanner dataIn = new Scanner(System.in);
        DecimalFormat timeInSeconds = new DecimalFormat("##.###");

        System.out.println("Are you ready to begin the benchmark? ");
        String response = dataIn.nextLine();
        double endNum = 0;

        if(response.equals("Yes") || response.equals("yes") || response.equals("Ok") || response.equals("ok")) {

            System.out.println("Benchmark starting at " + SciFair.now("H:mm:ss:SSS") + ".");

            String startTimeHours = SciFair.now("H");
            String startTimeMinutes = SciFair.now("mm");
            String startTimeSeconds = SciFair.now("ss");
            String startTimeMilliseconds = SciFair.now("SSS");
            double startTimeHoursNumFormat = Double.valueOf(startTimeHours.trim()).doubleValue();
            double startTimeMinutesNumFormat = Double.valueOf(startTimeMinutes.trim()).doubleValue();
            double startTimeSecondsNumFormat = Double.valueOf(startTimeSeconds.trim()).doubleValue();
            double startTimeMillisecondsNumFormat = Double.valueOf(startTimeMilliseconds.trim()).doubleValue();
            double startHoursInSeconds = (startTimeHoursNumFormat * 3600);
            double startMinutesInSeconds = (startTimeMinutesNumFormat * 60);
            double startMillisecondsInSeconds = (startTimeMillisecondsNumFormat / 1000);
            double startTotalSeconds = (startHoursInSeconds + startMinutesInSeconds + startTimeSecondsNumFormat + startMillisecondsInSeconds);

            Thread thread1 = new Thread() {
                public void run() {
                    double endNum = 0;
                    double num = 5832544225416546445465465465465465448412168546725.2655236355335649499923164684654345649874181221173246189579162421579584572121334216957951462175918894199993642446445548521652158975;

                    for(double numberRun = 0; numberRun <= 1666666660; numberRun++) {
                        endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);    
                    }
                }
            };


            Thread thread2 = new Thread() {
                public void run() {
                    double endNum = 0;
                    double num = 5832544225416546445465465465465465448412168546725.2655236355335649499923164684654345649874181221173246189579162421579584572121334216957951462175918894199993642446445548521652158975;

                    for(double numberRun = 0; numberRun <= 1666666660; numberRun++) {
                        endNum = endNum+((num * 58445246411658545558.458561435435385465146445641894984984651321616548946132131654984 + 26564.6351648941654984156 / 321654984616321654654984365.3216548974165416484 + 216541648165141654.23164584961321316546844651 * 65616484316541486541.1654484646546576746862468546576248645814668585145181 / 111.3216516543464545795479884754424759845 * 3467247954975472171276545789595.4721756147541715715157145715145714514748717521 / 31654688498445132154.3215449848489461531654 * 61654984132165465484464131321145644684.3164846434654648446546546465456465465414321165118547521569845258547566352544 / 32136546465434654654484843131.3164484151515121211554464547951126758178571454164175415417584175664) / 2431276251975915431326725157958456157321624621765419.4321672175951247316726451751487465462645754579517232172757497516373127651975167516715);    
                    }
                }
            };

            //Snip out a bunch of copy/paste thread implementations



            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
            thread5.start();
            thread6.start();

            thread1.join();
            thread2.join();
            thread3.join();
            thread4.join();
            thread5.join();
            thread6.join();


            String endTimeHours = SciFair.now("H");
            String endTimeMinutes = SciFair.now("mm");
            String endTimeSeconds = SciFair.now("ss");
            String endTimeMilliseconds = SciFair.now("SSS");
            double endTimeHoursNumFormat = Double.valueOf(endTimeHours.trim()).doubleValue();
            double endTimeMinutesNumFormat = Double.valueOf(endTimeMinutes.trim()).doubleValue();
            double endTimeSecondsNumFormat = Double.valueOf(endTimeSeconds.trim()).doubleValue();
            double endTimeMillisecondsNumFormat = Double.valueOf(endTimeMilliseconds.trim()).doubleValue();
            double endHoursInSeconds = (endTimeHoursNumFormat * 3600);
            double endMinutesInSeconds = (endTimeMinutesNumFormat * 60);
            double endMillisecondsInSeconds = (endTimeMillisecondsNumFormat / 1000);
            double endTotalSeconds = (endHoursInSeconds + endMinutesInSeconds + endTimeSecondsNumFormat + endMillisecondsInSeconds);

            double elapsedTime = (endTotalSeconds - startTotalSeconds);

            System.out.println("\nThe benchmark is complete at " + SciFair.now("H:mm:ss:SSS") + ", and the end result is " + endNum + ".");
            System.out.println("\nThe benchmark was run in " + timeInSeconds.format(elapsedTime) + " seconds.");
        } else {
            System.out.println("\nPlease restart the program when you are ready to benchmark.");
        }
    }
}
import java.util.Calendar;
导入java.text.simpleDataFormat;
导入java.util.Scanner;
导入java.text.*;
导入java.util.concurrent.*;
公开课
{
/**
*努玛·罗伯逊
* 1/13/11
*2011年科学博览会
*/
现在使用公共静态字符串(字符串日期格式){
Calendar cal=Calendar.getInstance();
SimpleDataFormat sdf=新的SimpleDataFormat(日期格式);
返回sdf.format(cal.getTime());
}
公共静态void main(字符串[]args){
扫描仪数据输入=新扫描仪(System.in);
DecimalFormat timeInSeconds=新的DecimalFormat(“##.####”);
System.out.println(“您准备好开始基准测试了吗?”);
字符串响应=dataIn.nextLine();
双端数=0;
if(response.equals(“Yes”)| response.equals(“Yes”)| response.equals(“Ok”)| response.equals(“Ok”)){
System.out.println(“从“+scifear.now”(“H:mm:ss:SSS”)+”开始的基准测试);
字符串starttimehurs=scifear.now(“H”);
字符串startTimeMinutes=scifear.now(“mm”);
字符串startTimeSeconds=scifear.now(“ss”);
字符串starttimemillesons=scifear.now(“SSS”);
double startTimeHoursNumFormat=double.valueOf(startTimeHours.trim()).doubleValue();
double startTimeMinutesNumFormat=double.valueOf(startTimeMinutes.trim()).doubleValue();
double StartTimeSecondsUMFormat=double.valueOf(startTimeSeconds.trim()).doubleValue();
double StartTimeMillSecondSnumFormat=double.valueOf(startTimeMillSeconds.trim()).doubleValue();
双startHoursInSeconds=(startTimeHoursNumFormat*3600);
双StartMinutesUnseconds=(startTimeMinutesNumFormat*60);
双启动毫秒秒=(启动毫秒秒格式/1000);
双startTotalSeconds=(StartToUrsInsectonds+StartMinutesInsectonds+StartTimeSecondsSnumFormat+StartMills秒);
线程thread1=新线程(){
公开募捐{
双端数=0;
double num=58325442254165165464454654464654654654484121685446725.2655236355356499923164465434564987418122117324461895791624215795845721213342169579551462159188941999936426445548521652158975;
对于(double numberRun=0;numberRun,问题在于线程#join()正在引发一个已检查的异常,编译器希望您处理该异常。已检查的异常通常通过声明您的方法/函数可以引发异常或捕获异常来处理

public static void main(String[] args) throws InterruptedException {
在您的特定情况下,因为您只是在编写一个简单的基准测试应用程序,所以最简单的解决方案就是指出您的代码可能会引发异常

public static void main(String[] args) throws InterruptedException {
处理异常的示例如下:

try {
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    thread5.join();
    thread6.join();
} catch (InterruptedException e) {
    Thread.currentThread().interrupt(); //This is a best practice for handling the InterruptedException only
    logger.log("The thread has been interrupted and should now exit quickly and cleanly. Processing may be incomplete.");
}
问题是线程#join()正在抛出一个已检查的异常,编译器希望您处理该异常。已检查的异常通常通过声明您的方法/函数可以抛出该异常或捕获该异常来处理

public static void main(String[] args) throws InterruptedException {
在您的特定情况下,因为您只是在编写一个简单的基准测试应用程序,所以最简单的解决方案就是指出您的代码可能会引发异常

public static void main(String[] args) throws InterruptedException {
处理异常的示例如下:

try {
    thread1.join();
    thread2.join();
    thread3.join();
    thread4.join();
    thread5.join();
    thread6.join();
} catch (InterruptedException e) {
    Thread.currentThread().interrupt(); //This is a best practice for handling the InterruptedException only
    logger.log("The thread has been interrupted and should now exit quickly and cleanly. Processing may be incomplete.");
}

我建议您使用ExecutorService。我希望您会看到它使管理线程变得更容易

public static void main(String[] args) throws InterruptedException {
int threads = 6;

System.out.println("Benchmark starting at " + new Date());
long start = System.nanoTime();
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < threads; i++)
    es.submit(new Runnable() {
        public void run() {
            // place your benchmark code here
        }
    });
es.shutdown();
es.awaitTermination(60, TimeUnit.SECONDS);
long time = System.nanoTime() - start;

System.out.printf("%nThe benchmark is complete at " + new Date() + "%nThe benchmark was run in %.3f seconds.%n", time / 1e9);
}
publicstaticvoidmain(String[]args)抛出InterruptedException{
int线程=6;
System.out.println(“从“+新日期()开始的基准”);
长启动=System.nanoTime();
ExecutorService es=Executors.newCachedThreadPool();
对于(int i=0;i

我假设您的编译错误是您没有处理InterruptedException。没有必要捕获InterruptedException,因为a)它不应该发生b)您只能记录它。我建议您使用ExecutorService。我希望您会看到它使管理线程变得更容易

public static void main(String[] args) throws InterruptedException {
int threads = 6;

System.out.println("Benchmark starting at " + new Date());
long start = System.nanoTime();
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 0; i < threads; i++)
    es.submit(new Runnable() {
        public void run() {
            // place your benchmark code here
        }
    });
es.shutdown();
es.awaitTermination(60, TimeUnit.SECONDS);
long time = System.nanoTime() - start;

System.out.printf("%nThe benchmark is complete at " + new Date() + "%nThe benchmark was run in %.3f seconds.%n", time / 1e9);
}
publicstaticvoidmain(String[]args)抛出InterruptedException{
int线程=6;
System.out.println(“从“+新日期()开始的基准”);
长启动=System.nanoTime();
ExecutorService es=Executors.newCachedThreadPool();
对于(int i=0;i

我假设您的编译错误是您没有处理InterruptedException。没有必要捕获InterruptedException,因为a)它不应该发生b)您只能记录它。

告诉我们错误是什么对找到答案有很大帮助!这不是一个解决方案,而是一个使结果更准确的建议。您应该uld考虑使用循环屏障。它本质上就像是所有线程的起始门