I have started learning multi-core programming and developing parallel algorithms. This can be done with ease by the use of multithreading in Java. So, I created two text files with 10 lines of content as follows:
This is the first line in file 1
This is the second line in file 1
This is the third line in file 1
This is the fourth line in file 1
This is the fifth line in file 1
This is the sixth line in file 1
This is the seventh line in file 1
This is the eighth line in file 1
This is the ninth line in file 1
This is the tenth line in file 1
Similarly, in the other text file, the file 1 is replaced by file 2. I wrote a program to read the contents of the file, with and without threads. They are as follows:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class SimpleThread {
static void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
BufferedReader br1 = new BufferedReader(new FileReader(new File("test1.txt")));
BufferedReader br2 = new BufferedReader(new FileReader(new File("test2.txt")));
SimpleThread.printFile(br1);
SimpleThread.printFile(br2);
System.out.println(System.nanoTime() - startTime + "ns");
}
}
The program using multithreading is as follows:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Threading extends Thread{
BufferedReader br;
public Threading(String fileName)
{
try{
br = new BufferedReader(new FileReader(new File(fileName)));
start();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public void run()
{
try{
printFile(br);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
Threading t1 = new Threading("test1.txt");
Threading t2 = new Threading("test2.txt");
System.out.println(System.nanoTime() - startTime + "ns");
}
}
Now, when I compare the execution time of both the programs, I see that the program which is single threaded takes 1544589.0ns and the multithreaded program takes 410522.0ns.
I was curious to know the factor by which the speed was increased. I found it to be 0.23 approximately.
After revising the code that uses multiple threads, I found that a single threaded program executes faster and this has increased my confusion to a greater extent.
Here is the revised code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Threading extends Thread{
BufferedReader br;
public Threading(String fileName)
{
try{
br = new BufferedReader(new FileReader(new File(fileName)));
start();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private void printFile(BufferedReader br) throws Exception
{
for(String line; (line = br.readLine())!=null; )
System.out.println(line);
}
public void run()
{
try{
printFile(br);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[]) throws Exception
{
double startTime = System.nanoTime();
Threading t1 = new Threading("test1.txt");
Threading t2 = new Threading("test2.txt");
t1.join(); //waiting for t1 to finish
t2.join(); //waiting for t2 to finish
System.out.println(System.nanoTime() - startTime + "ns");
}
}
And now the execution time are :
Single Threaded - 1459052.0ns
Multithreaded - 1768651.0ns
Why is the system behaving in an unnatural way?
Now, my questions are :
- Will increasing the number of threads, reduce the execution time?
- When should one use multithreading in writing programs
- Can the same concept of file be ported to databases, where every thread reads a portion of the database based on the category say information on news, sports, politics, etc. will be read by the corresponding threads and finally the results will be clubbed together. Is this feasible?
- Should mutlithreading be used only for CPU bound programs?