Skip to content

Instantly share code, notes, and snippets.

@akkida746
Last active June 20, 2017 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkida746/abf085a6434a0244967d11b3d3873a8b to your computer and use it in GitHub Desktop.
Save akkida746/abf085a6434a0244967d11b3d3873a8b to your computer and use it in GitHub Desktop.
Multithreaded File Reading in Java
import java.io.*;
import java.lang.*;
class MultiThreadedFileRead extends Thread
{
InputStream in;
MultiThreadedFileRead(String fname) throws Exception
{
in=new FileInputStream(fname);
this.start();
}
public void run()
{
int i=0;
while(i!=-1)
{
try
{
i=in.read();
System.out.print((char)i);
}catch(Exception e){}
}
try
{
in.close();
}catch(Exception e){}
}
public static void main(String a[]) throws Exception
{
int n=2;
System.out.print("Enter the number of files : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
n=Integer.parseInt(br.readLine());
}catch(Exception e){}
MultiThreadedFileRead fr[]=new MultiThreadedFileRead[n];
long tim;
tim=System.currentTimeMillis();
for(int i=0;i<n;i++)
fr[i]=new MultiThreadedFileRead(a[i]);
for(int i=0;i<n;i++)
{
try
{
fr[i].join();
}catch(Exception e){}
}
System.out.println("Time Required : "+(System.currentTimeMillis()-tim)+" miliseconds.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment