Popular Articles

From the 8th century the Muslim invaders began to make forays into Hindustan and by the 12th century the entire North India and the throne at Delhi had
Riding a bike in the hills is a matter of great pleasure. It is also an adventure, to be relished long after it has past.  Riding a bike in the
Privacy is an important thing in every section. Same goes for mobile phones too. People always have something to hide from others on their mobile phone

Latest Articles

Privacy is an important thing in every section. Same goes for mobile phones too. People always have something to hide from others on their mobile phone
Subhas Chandra Bose is one of the towering figures of the freedom movement. The fact that he differed from Gandhi and Nehru is well known and documented,
From the 8th century the Muslim invaders began to make forays into Hindustan and by the 12th century the entire North India and the throne at Delhi had

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