1

I have created a below program to compare the metrics with multithreading and without multi threading.

Here i am creating 10000 files using regular IO operations. With Multithreading and without multithreading also it is taking more or less a similar time to complete.

What am i missing here? Why i am not getting any benefit from Multithreading?

 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);
    }
}

Thanks.

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • 1
    This is not a good way to microbenchmark in Java. You can read some guidelines in [How do I write a correct microbenchmark in Java](http://stackoverflow.com/q/504103/4125191) - though indeed as the answer said, I/O is less dependent on CPU and more on slower I/O device speed. – RealSkeptic Sep 11 '15 at 20:41

2 Answers2

4

This is because your performance is not limited by the CPU, but by the harddisk. A single CPU can create files faster than the harddisk can store them, so splitting the job onto multiple CPU cores will not help any.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Sorry, i am not getting this. you mentioned "so splitting the job onto multiple CPU cores will not help any" - am i splitting the job here to multiple CPU cores?? how? – user3453814 Sep 11 '15 at 21:01
  • What do you think multi-threading does? If you create 8 threads and system has 8 cores, it may run each thread on a different core. If you have more threads than cores, each core will multi-task and run multiple threads. Multi-threading gains performance by either allowing multiple cores do the job and/or by allowing a single core to do other work while a thread is waiting on an external resource. – Andreas Sep 11 '15 at 21:06
  • I am running in a laptop, the processor is i7 vPro in windows environment. In device manager i am seeing 4 core. So it means my laptop has 4 cpu cores. In my example i am creating 100 threads. does it means single CPU core is processing 25 threads? If IO jobs are hardware dependent, then for all IO related operations multhithreading will not be a beneficial one? – user3453814 Sep 12 '15 at 12:43
  • Correct. All 100 threads are spending most (pretty much *all*) of their time doing nothing, waiting for the harddisk to complete the operation. If you monitor the system, you'll notice that the disk light that blinks when the disk is in use, is actually solidly "on", and the CPU is idle. – Andreas Sep 12 '15 at 12:54
  • Thanks. So then when should i need to take the benefit of Multithreading? Should i use Multithreading whenever i need to do anything only inside the JVM, not while doing any hardisk related operation especially with IO or any writing or reading file related operations? – user3453814 Sep 13 '15 at 01:08
  • That's a very broad question. Google "[when to use multithreading](https://www.google.com/search?q=when+to+use+multithreading&ie=utf-8&oe=utf-8)" – Andreas Sep 13 '15 at 03:20
0

I'm afraid your mistake was to include a call to t.join() into the loop: That makes the JVM wait until the thread t ends. Obviously, that makes the same effect as a single thread.

If you want to wait until all the threads have ended, better store all the threads in a collection, and, after the loop which calls to start, put another loop that calls to join on every thread.

Little Santi
  • 8,563
  • 2
  • 18
  • 46