• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Please help me get started with this assignment

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
farrukh zaheen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what i have so far...as a counter to count number of lines please help me in the multithreading part :



[ August 07, 2004: Message edited by: farrukh zaheen ]
(Marilyn added code tags)
[ August 08, 2004: Message edited by: Marilyn de Queiroz ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are required to submit the following classes:

1.FileCountInfo �
2.Producer �
3.Consumer �
4.FileCounter �



Where does a class called LineCount fit in here? What if you stub out the required classes first, following the instructions, and build from there.
 
farrukh zaheen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LineCount i wrote just as a counter whose source code i will be using in the producer thread to count the number of lines.


here is my producer thread

public class Producer extends Thread {
public 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;
}





public void run() {

while (true) {
try {

if (Thread.activeCount() > 3)
{
sleep(100);
}


// seconds
} catch (Exception e) {
}



//try {

// FileReader fr = new FileReader(fileName);
// BufferedReader br = new BufferedReader(fr);


// String record = new String();
//while ((record = br.readLine()) != null) {
//count++;
// }

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]);
}
}
}
}


I dont know why its giving me this error :
producer.java:79: illegal start of expression
private static void count(String streamName, InputStream input) {
^
producer.java:126: ';' expected
 
I will suppress my every urge. But not this shameless plug:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic