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.