How To Read A File Using Multithreading In Java
It says multiple tasks tin be done at a time but really its non exactly like that.
It will continue the cpu idle time less. Because cpu will take a responsibility to execute many threads if nosotros
Use multithreading.
If its single threaded application and then it might go along the cpu idle for some fourth dimension and hence lowers the performance.
Lets meet the same through example.
Requirement
Read multiple files from a location and write the content of each file to the same destination file by appending each time.
i accept kept total 3000 files in the source location to read and i will create one thread to read and write 1 file content in multithreading scenario whereas in single thread scenario for loop iterates 3000 times to read and write 3000 files.
Lets use single threaded program
-
packet com.kb ;
-
import coffee.io.BufferedReader ;
-
import coffee.io.BufferedWriter ;
-
import java.io.File ;
-
import java.io.FileNotFoundException ;
-
import coffee.io.FileReader ;
-
import java.io.FileWriter ;
-
import java.io.IOException ;
-
public class SingleThreadWriteToFile {
-
public static void main( Cord [ ] args) {
-
long startTime = System.nanoTime ( ) ;
-
File dir = new File ( "E:\\java\\sample files" ) ;
-
File destination = new File ( "East:\\coffee\\sample files\\Destination.txt" ) ;
-
File [ ] files = dir.listFiles ( ) ;
-
String content;
-
for ( File file : files) {
-
content = readFromFile(file.getAbsolutePath ( ) ) ;
-
writeToFile(destination,content) ;
-
}
-
long stopTime = System.nanoTime ( ) ;
-
System.out.println ( "Full execution time is " + (stopTime - startTime) ) ;
-
}
-
private static void writeToFile( File file,String content) {
-
try {
-
BufferedWriter writer = new BufferedWriter ( new FileWriter (file,true ) ) ;
-
writer.write (content) ;
-
writer.affluent ( ) ;
-
} catch ( IOException e) {
-
// TODO Auto-generated take hold of cake
-
east.printStackTrace ( ) ;
-
}
-
}
-
static String readFromFile( String filename) {
-
StringBuffer content = new StringBuffer ( ) ;
-
try {
-
String text;
-
BufferedReader reader = new BufferedReader ( new FileReader (filename) ) ;
-
while ( (text = reader.readLine ( ) ) != nada ) {
-
content.append (text) ;
-
content.append ( "\north" ) ;
-
}
-
} catch ( FileNotFoundException east) {
-
// TODO Auto-generated take hold of cake
-
e.printStackTrace ( ) ;
-
}
-
catch ( IOException e) {
-
// TODO Auto-generated catch block
-
due east.printStackTrace ( ) ;
-
}
-
render content.toString ( ) ;
-
}
-
}
package com.kb; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public grade SingleThreadWriteToFile { public static void main(String[] args) { long startTime = System.nanoTime(); File dir = new File("E:\\java\\sample files"); File destination = new File("East:\\java\\sample files\\Destination.txt"); File[] files = dir.listFiles(); String content; for (File file : files) { content = readFromFile(file.getAbsolutePath()); writeToFile(destination,content); } long stopTime = System.nanoTime(); System.out.println("Total execution time is "+(stopTime - startTime)); } private static void writeToFile(File file,String content) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(file,true)); writer.write(content); author.flush(); } catch (IOException due east) { // TODO Car-generated catch cake due east.printStackTrace(); } } static String readFromFile(String filename){ StringBuffer content = new StringBuffer(); endeavor { String text; BufferedReader reader = new BufferedReader(new FileReader(filename)); while((text = reader.readLine())!=null){ content.append(text); content.append("\north"); } } grab (FileNotFoundException e) { // TODO Auto-generated catch cake e.printStackTrace(); } catch (IOException eastward) { // TODO Auto-generated take hold of block e.printStackTrace(); } return content.toString(); } }
Output is
Now lets utilize multithreading to the above program.
Here how information technology goes
-
package com.kb ;
-
import java.io.BufferedReader ;
-
import java.io.BufferedWriter ;
-
import java.io.File ;
-
import java.io.FileNotFoundException ;
-
import java.io.FileReader ;
-
import coffee.io.FileWriter ;
-
import java.io.IOException ;
-
public class MultithreadingWriteToFile {
-
public static void main( String [ ] args) {
-
Thread.currentThread ( ).setPriority ( Thread.MIN_PRIORITY ) ;
-
long startTime = System.nanoTime ( ) ;
-
File dir = new File ( "E:\\java\\sample files" ) ;
-
File destination = new File ( "E:\\java\\sample files\\DestinationMultiThread.txt" ) ;
-
File [ ] files = dir.listFiles ( ) ;
-
for ( File file : files) {
-
Author w1 = new Writer (file, destination) ;
-
Thread t = new Thread (w1) ;
-
t.setPriority ( Thread.MAX_PRIORITY ) ;
-
t.commencement ( ) ;
-
}
-
long stopTime = Organization.nanoTime ( ) ;
-
System.out.println ( "Total execution time is " + (stopTime - startTime) ) ;
-
}
-
}
-
class Author implements Runnable {
-
File source;
-
File destination;
-
public Author ( File source,File destination) {
-
this.source = source;
-
this.destination = destination;
-
}
-
@Override
-
public void run( ) {
-
String content;
-
content = readFromFile(source.getAbsolutePath ( ) ) ;
-
writeToFile(destination,content) ;
-
}
-
private static void writeToFile( File file,String content) {
-
endeavour {
-
BufferedWriter author = new BufferedWriter ( new FileWriter (file,true ) ) ;
-
author.write (content) ;
-
writer.write ( "file content written" ) ;
-
writer.flush ( ) ;
-
} grab ( IOException eastward) {
-
// TODO Auto-generated take hold of block
-
e.printStackTrace ( ) ;
-
}
-
}
-
static Cord readFromFile( String filename) {
-
StringBuffer content = new StringBuffer ( ) ;
-
attempt {
-
String text;
-
BufferedReader reader = new BufferedReader ( new FileReader (filename) ) ;
-
while ( (text = reader.readLine ( ) ) != null ) {
-
content.suspend (text) ;
-
content.append ( "\due north" ) ;
-
}
-
} take hold of ( FileNotFoundException eastward) {
-
// TODO Car-generated grab block
-
eastward.printStackTrace ( ) ;
-
}
-
catch ( IOException e) {
-
// TODO Auto-generated catch block
-
eastward.printStackTrace ( ) ;
-
}
-
return content.toString ( ) ;
-
}
-
}
parcel com.kb; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import coffee.io.IOException; public class MultithreadingWriteToFile { public static void main(Cord[] args) { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); long startTime = System.nanoTime(); File dir = new File("Due east:\\coffee\\sample files"); File destination = new File("E:\\java\\sample files\\DestinationMultiThread.txt"); File[] files = dir.listFiles(); for (File file : files) { Author w1 = new Writer(file, destination); Thread t = new Thread(w1); t.setPriority(Thread.MAX_PRIORITY); t.start(); } long stopTime = Organisation.nanoTime(); System.out.println("Total execution time is "+(stopTime - startTime)); } } class Writer implements Runnable{ File source; File destination; public Writer(File source,File destination) { this.source = source; this.destination = destination; } @Override public void run() { Cord content; content = readFromFile(source.getAbsolutePath()); writeToFile(destination,content); } private static void writeToFile(File file,String content) { try { BufferedWriter author = new BufferedWriter(new FileWriter(file,true)); writer.write(content); writer.write("file content written"); author.affluent(); } catch (IOException e) { // TODO Automobile-generated catch block east.printStackTrace(); } } static String readFromFile(String filename){ StringBuffer content = new StringBuffer(); endeavor { String text; BufferedReader reader = new BufferedReader(new FileReader(filename)); while((text = reader.readLine())!=goose egg){ content.append(text); content.append("\n"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException eastward) { // TODO Auto-generated take hold of block e.printStackTrace(); } return content.toString(); } }
Output is
See the time difference between each output is
396687651 nano seconds.
This is how multithreading helps us to attain the functioning.
About the Author
How To Read A File Using Multithreading In Java,
Source: http://javainsimpleway.com/multithreading-in-java/
Posted by: walkerdeboyfaing.blogspot.com
0 Response to "How To Read A File Using Multithreading In Java"
Post a Comment