Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 我的多线程分析_Java_Multithreading - Fatal编程技术网

Java 我的多线程分析

Java 我的多线程分析,java,multithreading,Java,Multithreading,我创建了一个下面的程序来比较有多线程和没有多线程的度量 在这里,我使用常规IO操作创建10000个文件。 使用多线程和不使用多线程都需要差不多的时间来完成 我错过了什么?为什么我没有从多线程中获得任何好处 package com.sripy; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date;

我创建了一个下面的程序来比较有多线程和没有多线程的度量

在这里,我使用常规IO操作创建10000个文件。 使用多线程和不使用多线程都需要差不多的时间来完成

我错过了什么?为什么我没有从多线程中获得任何好处

 package com.sripy;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;

    class CreateFile {
        String fileName = "";

    CreateFile(String fileName){
        this.fileName = fileName; 
    }

    public void createFile() throws IOException{
        //System.out.println("Creating file..."+fileName);
        FileOutputStream fos = new FileOutputStream(new File("C:\\sample\\5\\"+fileName));

        String str = "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java.";

        String str1 = "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java.";

        String str2 = "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java.";

        String str3 = "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java." +
                "Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from other system in database and only wanted to store value before the decimal point e.g. he was getting and he want to convert it to to store into database. Good thing was that API was returning a float primitive value and you don't need to convert an String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to int by replace long method to their int counterpart. First way is to auto-box float primitive into Float object and call the longVale() method and other way is simply cast float to long or int to get rid of decimal points. Alternative you can use Math.round() and then cast back to long. No doubt, second approach is the easiest if requirement is simply getting rid of anything after decimal point but if you need to rounding then third approach is the right way to go. In this tutorial, we will learn all three ways to perform float to long conversion in Java.";

        String finalStr = str +str1+str2+str3;
        fos.write(finalStr.getBytes());
        fos.flush();
        fos.close();
    }
}

class FeedProcessor {
    int StartNum = 0;
    int totalFile = 0;

    FeedProcessor(int StartNum, int totalFile){
        this.StartNum = StartNum;
        this.totalFile = totalFile;
    }

    public void process() throws IOException {
        for(int i=1;i<=totalFile;i++){
            CreateFile cf = new CreateFile((StartNum++)+".txt");
            cf.createFile();
        }
    }
}

class MyRunnable implements Runnable {
    FeedProcessor fp = null;

    MyRunnable(FeedProcessor fp){
        this.fp = fp;
    }
    @Override
    public void run() {
        try {
            fp.process();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

public class MyMultiThreads {
    public static void main(String ar[]) throws IOException, InterruptedException{
        long lStartTime = new Date().getTime();
        System.out.println("****Start Main Thread****");

        int totalFile = 10000;
        int files2Process = 100; 
        int noThread = totalFile/files2Process;

        //without multithreading
        for(int i = 0;i<noThread;i++){
            int startNum = (i*files2Process)+1; 
            FeedProcessor fp = new FeedProcessor(startNum,files2Process);
            fp.process();
        }

        //with multithreading       
        /*int threadCount = 0;
        for(int i = 0;i<noThread;i++){
            int startNum = (i*files2Process)+1;
            MyRunnable myRun = new MyRunnable(new FeedProcessor(startNum, files2Process) );
            Thread t = new Thread(myRun,"ChildThread"+(++threadCount));
            t.start();
            t.join();
        }*/


        long lEndTime = new Date().getTime();
        long difference = lEndTime - lStartTime;
        System.out.println("****End Main Thread****"+difference);
    }
}
package com.sripy;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Date;
类CreateFile{
字符串fileName=“”;
CreateFile(字符串文件名){
this.fileName=文件名;
}
public void createFile()引发IOException{
//System.out.println(“创建文件…”+文件名);
FileOutputStream fos=新的FileOutputStream(新文件(“C:\\sample\\5\\\”+文件名));
字符串str="昨天,我团队的一位小朋友来问我如何将浮点变量转换为long或int?他将来自其他系统的一些值存储在数据库中,只想存储小数点之前的值,例如,他得到了,他想将其转换为存储到数据库中。好的是API返回了在Java中,有3种方法可以将浮点值转换为long或int,但我们只关注long部分。您可以按照以下提示编写Java程序,通过替换long方法将float转换为int,并将long方法转换为int方法。第一种方法是自动将float原语装箱转换为Float对象并调用longVale()方法,另一种方法是将Float转换为long或int以去除小数点然后再回到long。毫无疑问,如果需要简单地去掉小数点后的任何内容,那么第二种方法是最简单的,但是如果需要舍入,那么第三种方法是正确的。在本教程中,我们将学习在Java中执行浮点到long转换的所有三种方法。”+
"昨天,我团队的一位小朋友来问我如何将浮点变量转换为long或int?他将来自其他系统的一些值存储在数据库中,只想存储小数点之前的值,例如,他得到了,他想将其转换为存储到数据库中。好的是API返回了在Java中,有3种方法可以将浮点值转换为long或int,但我们只关注long部分。您可以按照以下提示编写Java程序,通过替换long方法将float转换为int,并将long方法转换为int方法。第一种方法是自动将float原语装箱转换为Float对象并调用longVale()方法,另一种方法是将Float转换为long或int以去除小数点然后再回到long。毫无疑问,如果需要简单地去掉小数点后的任何内容,那么第二种方法是最简单的,但是如果需要舍入,那么第三种方法是正确的。在本教程中,我们将学习在Java中执行浮点到long转换的所有三种方法。”+
"昨天,我团队的一位小朋友来问我如何将浮点变量转换为long或int?他将来自其他系统的一些值存储在数据库中,只想存储小数点之前的值,例如,他得到了,他想将其转换为存储到数据库中。好的是API返回了在Java中,有3种方法可以将浮点值转换为long或int,但我们只关注long部分。您可以按照以下提示编写Java程序,通过替换long方法将float转换为int,并将long方法转换为int方法。第一种方法是自动将float原语装箱转换为Float对象并调用longVale()方法,另一种方法是将Float转换为long或int以去除小数点然后再回到long。毫无疑问,如果需要简单地去掉小数点后的任何内容,那么第二种方法是最简单的,但如果需要舍入,那么第三种方法是正确的。在本教程中,我们将学习在Java中执行浮点到long转换的所有三种方法。“;
字符串str1=”昨天,我团队的一位小朋友来问我如何将浮点变量转换为long或int?他将来自其他系统的一些值存储在数据库中,只想存储小数点之前的值,例如,他得到了,他想将其转换为存储到数据库中。好的是API返回了在Java中,有3种方法可以将浮点值转换为long或int,但我们只关注long部分。您可以按照以下提示编写Java程序,通过替换long方法将float转换为int,并将long方法转换为int方法。第一种方法是自动将float原语装箱转换为Float对象并调用longVale()方法,另一种方法是将Float转换为long或int以去除小数点然后再回到long。毫无疑问,如果需要简单地去掉小数点后的任何内容,那么第二种方法是最简单的,但是如果需要舍入,那么第三种方法是正确的。在本教程中,我们将学习在Java中执行浮点到long转换的所有三种方法。”+
"昨天,我团队的一位小朋友来问我如何将浮点变量转换为long或int?他将来自其他系统的一些值存储在数据库中,只想存储小数点之前的值,例如,他得到了,他想将其转换为存储到数据库中。好的是API返回了在浮子上打水漂