Reading Multiple Files Using Threads in Java
Information technology says multiple tasks can be done at a fourth dimension but really its non exactly like that.
It volition go on the cpu idle time less. Considering cpu will accept a responsibility to execute many threads if we
Use multithreading.
If its single threaded awarding then it might go on the cpu idle for some time and hence lowers the performance.
Lets run into the same through instance.
Requirement
Read multiple files from a location and write the content of each file to the same destination file past appending each time.
i accept kept full 3000 files in the source location to read and i will create one thread to read and write ane file content in multithreading scenario whereas in single thread scenario for loop iterates 3000 times to read and write 3000 files.
Lets utilize single threaded programme
-
package com.kb ;
-
import java.io.BufferedReader ;
-
import java.io.BufferedWriter ;
-
import java.io.File ;
-
import java.io.FileNotFoundException ;
-
import coffee.io.FileReader ;
-
import java.io.FileWriter ;
-
import java.io.IOException ;
-
public form SingleThreadWriteToFile {
-
public static void primary( String [ ] args) {
-
long startTime = System.nanoTime ( ) ;
-
File dir = new File ( "E:\\coffee\\sample files" ) ;
-
File destination = new File ( "E:\\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 ( "Total execution time is " + (stopTime - startTime) ) ;
-
}
-
individual static void writeToFile( File file,String content) {
-
attempt {
-
BufferedWriter author = new BufferedWriter ( new FileWriter (file,true ) ) ;
-
writer.write (content) ;
-
writer.flush ( ) ;
-
} grab ( IOException due east) {
-
// TODO Machine-generated catch cake
-
e.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.suspend ( "\n" ) ;
-
}
-
} grab ( FileNotFoundException eastward) {
-
// TODO Auto-generated take hold of block
-
e.printStackTrace ( ) ;
-
}
-
catch ( IOException due east) {
-
// TODO Automobile-generated catch block
-
e.printStackTrace ( ) ;
-
}
-
render content.toString ( ) ;
-
}
-
}
package com.kb; import java.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:\\coffee\\sample files"); File destination = new File("E:\\java\\sample files\\Destination.txt"); File[] files = dir.listFiles(); String content; for (File file : files) { content = readFromFile(file.getAbsolutePath()); writeToFile(destination,content); } long stopTime = Organisation.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)); author.write(content); writer.affluent(); } catch (IOException east) { // TODO Automobile-generated take hold of block e.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.suspend("\north"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return content.toString(); } }
Output is
Now lets use multithreading to the above programme.
Here how it 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 java.io.FileWriter ;
-
import java.io.IOException ;
-
public grade MultithreadingWriteToFile {
-
public static void primary( Cord [ ] args) {
-
Thread.currentThread ( ).setPriority ( Thread.MIN_PRIORITY ) ;
-
long startTime = Organization.nanoTime ( ) ;
-
File dir = new File ( "E:\\coffee\\sample files" ) ;
-
File destination = new File ( "Eastward:\\coffee\\sample files\\DestinationMultiThread.txt" ) ;
-
File [ ] files = dir.listFiles ( ) ;
-
for ( File file : files) {
-
Author w1 = new Author (file, destination) ;
-
Thread t = new Thread (w1) ;
-
t.setPriority ( Thread.MAX_PRIORITY ) ;
-
t.start ( ) ;
-
}
-
long stopTime = Organisation.nanoTime ( ) ;
-
Arrangement.out.println ( "Total execution time is " + (stopTime - startTime) ) ;
-
}
-
}
-
grade Writer 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) {
-
try {
-
BufferedWriter writer = new BufferedWriter ( new FileWriter (file,truthful ) ) ;
-
writer.write (content) ;
-
author.write ( "file content written" ) ;
-
writer.flush ( ) ;
-
} catch ( IOException e) {
-
// TODO Motorcar-generated catch block
-
e.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" ) ;
-
}
-
} catch ( FileNotFoundException e) {
-
// TODO Auto-generated catch block
-
due east.printStackTrace ( ) ;
-
}
-
catch ( IOException due east) {
-
// TODO Machine-generated grab block
-
e.printStackTrace ( ) ;
-
}
-
return content.toString ( ) ;
-
}
-
}
bundle com.kb; import java.io.BufferedReader; import java.io.BufferedWriter; import coffee.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class MultithreadingWriteToFile { public static void chief(Cord[] 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) { Writer w1 = new Author(file, destination); Thread t = new Thread(w1); t.setPriority(Thread.MAX_PRIORITY); t.start(); } long stopTime = System.nanoTime(); System.out.println("Total execution time is "+(stopTime - startTime)); } } grade 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) { endeavor { BufferedWriter writer = new BufferedWriter(new FileWriter(file,true)); author.write(content); writer.write("file content written"); writer.affluent(); } grab (IOException e) { // TODO Auto-generated catch block east.printStackTrace(); } } static String readFromFile(Cord filename){ StringBuffer content = new StringBuffer(); try { Cord text; BufferedReader reader = new BufferedReader(new FileReader(filename)); while((text = reader.readLine())!=null){ content.append(text); content.append("\northward"); } } catch (FileNotFoundException e) { // TODO Machine-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated grab block e.printStackTrace(); } return content.toString(); } }
Output is
Come across the fourth dimension difference between each output is
396687651 nano seconds.
This is how multithreading helps united states of america to achieve the functioning.
About the Author
Source: http://javainsimpleway.com/multithreading-in-java/
0 Response to "Reading Multiple Files Using Threads in Java"
Postar um comentário