Popular Articles

During the terrific silence of that horrible night, they could not hear the footsteps of death. On the eighteenth night of a great war, they were sleeping
“Bottle, bottle everywhere; not a single one to provide water” Bottled water is the present era acquisition of safe drinking water to homes and offices
Beauty of Kashmir is breathtaking and thus this valley is rightly described as paradise on earth. Carpets of saffron resembling lotus petals are spread

Latest Articles

Beauty of Kashmir is breathtaking and thus this valley is rightly described as paradise on earth. Carpets of saffron resembling lotus petals are spread
Located in culturally important Bhavnagar district of Gujarat, Alang today is known worldwide for ship dismantling. It is world's largest ship dismantling
Our ancestors discovered the technique of cooking food. Earlier they used to consume food in its original state i,e., in its raw form, regardless of whether

Hi,

 

One of the most important concepts in java is Threads. If we want to master in java we need to have a sound knowledge in the concept of threads and programming of java using threads. I am going to explain this with an illustrative program; the following is the program about a text file containing large number of words. Our task is to divide the large text file into five smaller text files and we should be able to run 5 threads parallel on each of the 5 smaller files and finally to sum up the total number of words.

 

import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;

class newThread1 implements Runnable
{
Thread t;
public static int vcount=0;
char ch[];
int i;
String process="";

newThread1(String pro)
{
t = new Thread(this); 
process=pro;
t.start(); 
}

public void run() 
{
try
{
for(int i=0;i<=10000;i++)
{
ch=process.toCharArray( );
if( (ch[i]==' ') )
{
vcount++;
}
}
}
catch (Exception e) 
{}
}

}
class multithreading
{
static Scanner in ;
public static void main(String args[]) throws Exception
{
int vcount=0;
FileInputStream fis=new FileInputStream("sample.txt");
int c;
int n=1;
int b;
while((c=fis.read())!=-1)
if(((char)c==' '))
{
n++;
}
System.out.println(n);
fis.close();
in=new Scanner(new FileReader("sample.txt"));
String pro1="";
String pro2="";
String pro3="";
String pro4="";
String pro5="";
String pro="";
b=n/5;
System.out.println(b);
for(int i=0;i
{
if(i<=b)
{
FileOutputStream fos=new FileOutputStream("first.txt");
pro=in.next();
pro1=pro1+ ' ' +pro;
fos.write(pro1.getBytes());
pro="";
}

if((i>(b)) && (i<=(2*b)))
{
FileOutputStream fos=new FileOutputStream("second.txt");
pro=in.next();
pro2=pro2+ ' ' +pro;
fos.write(pro2.getBytes());
pro="";
}

if((i>(2*b)) && (i<=(3*b)))
{
FileOutputStream fos=new FileOutputStream("third.txt");
pro=in.next();
pro3=pro3+ ' ' +pro;
fos.write(pro3.getBytes());
pro="";
}

if((i>(3*b)) && (i<=(4*b)))
{
FileOutputStream fos=new FileOutputStream("fourth.txt");
pro=in.next();
pro4=pro4+ ' ' +pro;
fos.write(pro4.getBytes());
pro="";
}
if((i>(4*b)) && (i<=n))
{
FileOutputStream fos=new FileOutputStream("five.txt");
pro=in.next();
pro5=pro5+ ' ' +pro;
fos.write(pro5.getBytes());
pro="";
}
}

newThread1 o1=new newThread1(pro1);

newThread1 o2=new newThread1(pro2);

newThread1 o3=new newThread1(pro3);

newThread1 o4=new newThread1(pro4);

newThread1 o5=new newThread1(pro5);


try 
{
o1.t.join();
o2.t.join();
o3.t.join();
o4.t.join();
o5.t.join();

catch (InterruptedException e) 
{}
String s="count";
newThread1 o=new newThread1(s);
System.out.println("The total no. of words in the file : ");
System.out.print((o.vcount));
}
}

In the above program using the concept of threads the large text is broken into five small text files. These are compared using the string options which are mentioned in the program and finally they are added up using the join function.

Regards,

Karthik.