Popular Articles

The Anglo Sikh wars (1846-49) were the most ferocious battles faced by the British in India. These battles deeply affected the British psyche, with the
I have been wanting to write about the military heroes of the 20th century from India. We have so many great soldiers and airmen who are not remembered
Backwaters are unique tourist attractions of Kerala. Cruising along the marvelous Kerala backwaters on a traditional house boat is one of the best ways

Latest Articles

Backwaters are unique tourist attractions of Kerala. Cruising along the marvelous Kerala backwaters on a traditional house boat is one of the best ways
I have been wanting to write about the military heroes of the 20th century from India. We have so many great soldiers and airmen who are not remembered
The Anglo Sikh wars (1846-49) were the most ferocious battles faced by the British in India. These battles deeply affected the British psyche, with the

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.

  • No comments found