wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Please help me get started with this assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Please help me get started with this assignment" Watch "Please help me get started with this assignment" New topic
Author

Please help me get started with this assignment

farrukh zaheen
Greenhorn

Joined: Jul 01, 2004
Posts: 8
SO FAR I HAVE MANAGED THE COUNTER TO COUNT NUMBER OF LINES. BUT CANT USE IT IN MULTHITHREADING PLEASE HELP!!!


This assignment asks you to write a program that will read all the files in a specified directory, count the number of lines in each file and output the results to another file named �file_count_report�.

Your program should first get the list of files in the directory and for each file in the list, start a producer thread to read the file, count the lines, and set the information on the file name and line number in a shared object. Your program will also start only one consumer thread to retrieve the information from the shared object and output the information to the report file �file_count_report� in the following format:

Thread name: <thread name> File name: <file name> line no: 123

suppose the file contains 123 lines and it is handled by a thread whose name is <thread name>.

You are required to submit the following classes:

1.FileCountInfo � This class will be the type of the shared object. It contains three fields (threadName, fileName and lineCount), where the field threadName stores the name of the thread that handles a file, fileName stores the name of the file and the field lineCount stores the number of lines in that file.
2.Producer � This class extends java.lang.Thread. It must have the following constructor:

Public Producer(String name, String file, � ) � This constructor accepts two arguments, the name of the thread and a full pathed file name such as �C:\itec1630a\a2\file1� and other arguments that you need to pass to the constructor.

The Producer class�s run method should implement the following steps:

1)Read in the file �file� and count its lines
2)Use the method(s) of the FileCountInfo class to set the information on the thread name, file name and line count in the shared object

3.Consumer � This class extends java.lang.Thread. This class will implement the following pseudo code:

loop
retrieve thread name, file name, and line count from the shared object
write the information to the report file �file_count_report�
end loop

The loop will end when all the Producer threads that are started to handle the files in the specified directory have ended.

4.FileCounter � The main class that will have the following method:

Public void countFiles(String directory)

This method will do the following steps:

1)get a list of files that are in the directory
2)start a Producer thread for each of the files
3)start a Consumer thread
4)wait for all threads to end and then output �END OF LIST� to the report file �file_count_report� and exit

To avoid having too many threads competing for the system resources when there are a lot of files in the directory, we will restrict the number of active Producer threads (active threads are started threads that have not ended yet) during the run time. For this assignment, at any time there should not be more than 3 active Producer threads. If there are already three active Producer threads during the run time, the program should wait until one of them ends to start the next Producer thread.

We will use the following sample test program and command to test your code:

public class TestFileCounter {
public static void main(String[] args) {
FileCounter fc = new FileCounter();
fc.countFiles(args[0]);
}
}
A sample command to enter is:

java TestFileCounter c:\\ythrh\\a2
[ August 07, 2004: Message edited by: farrukh zaheen ]

[ August 07, 2004: Message edited by: farrukh zaheen ]
[ August 07, 2004: Message edited by: farrukh zaheen ]
farrukh zaheen
Greenhorn

Joined: Jul 01, 2004
Posts: 8
Here is what i have so far...as a counter to count number of lines please help me in the multithreading part :

import java.io.*;

public class LineCount {
private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}

private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}

private static void count(String streamName, InputStream input) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(input);
BufferedReader in = new BufferedReader(inputStreamReader);
count(streamName, in);
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static long countWords(String line) {
long numWords = 0;
int index = 0;
boolean prevWhitespace = true;
while (index < line.length()) {
char c = line.charAt(index++);
boolean currWhitespace = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace) {
numWords++;
}
prevWhitespace = currWhitespace;
}
return numWords;
}

public static void main(String[] args) {
if (args.length == 0) {
count("stdin", System.in);
} else {
for (int i = 0; i < args.length; i++) {
count(args[i]);
}
}
}
}
[ August 07, 2004: Message edited by: farrukh zaheen ]
 
GeeCON Prague 2014
 
subject: Please help me get started with this assignment