0% found this document useful (0 votes)
38 views

Word

This Java program uses multiple threads to count the number of words in multiple text files concurrently. It takes file names as command line arguments, creates a WordCount thread for each file, and starts the threads. Each WordCount thread opens the file, reads it character by character, uses whitespace checks to count words, prints the file name and count, and closes the file. This allows the word counts for all files to be calculated simultaneously rather than sequentially.

Uploaded by

prabhusct
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Word

This Java program uses multiple threads to count the number of words in multiple text files concurrently. It takes file names as command line arguments, creates a WordCount thread for each file, and starts the threads. Each WordCount thread opens the file, reads it character by character, uses whitespace checks to count words, prints the file name and count, and closes the file. This allows the word counts for all files to be calculated simultaneously rather than sequentially.

Uploaded by

prabhusct
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

MULTIPLE THREADS- WORD COUNT

import java.io.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.*;

class wordcount implements Runnable


{
String s;
int numword;

wordcount(String a)
{
s=a;
numword=0;
new Thread(this, a).start();
}

synchronized public void run()


{
int i,index=0;

try
{
FileReader fin=new FileReader(s);
boolean ps=true;
do
{

i = fin.read();
char c=(char) i;
boolean cs=Character.isWhitespace(c);
if(ps&&!cs)
numword++; //counting no.of words
ps=cs;
} while(i != -1);

fin.close();
System.out.println("");
System.out.println(s+":"+numword);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}

}
}

class word
{
public static void main(String args[])
{
System.out.println(" *** WordCount in Files *** ");
for(int i=0;i<args.length;i++)
new wordcount(args[i]);

OUTPUT:

C:\>javac word.java
C:\>java word word.java bdlog.txt word.java

*** WordCount in Files ***

bdlog.txt:810
word.java:91

word.java:91

You might also like