I have problem to understand threads. Can someone help me to know how i can do rest of this program. Here is program text first:
The program will read a file where the first line contains the number of words in the rest of the file, one word on each line. The words should be stored in a table (array), then the k threads find how many times a given word occurs in the table. If your program is called Finde.java shall example. started up this way:
java Finde father myfile.txt 8
You will find how many times the word father in the file myfile.txt using 8 threads. Here is thus k = 8
Remember to take into account that k may be 1. When the file is read into shall main - thread share the table for about k equally long parts, and start up k threads to look at each part. When a thread has found the number of occurrences of the word in their part shall report this into a common object (a monitor). Main - thread should wait until all threads have finished searching, retrieving the total number of occurrences of the monitor and finally write this number out.
Here is the code i came up for now:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
CommonObject co = new CommonObject();
Finde f = new Finde();
Scanner file = null;
String[] read = new String[267752];
try{
file = new Scanner(new File("myfile.txt"));
}
catch(IOException e){
e.printStackTrace();
}
while(file.hasNext()){
for ( int i = 0; i < read.length; i++){
read[i] = file.next();
}
}
}
}
public class CommonObject {
private final int K = 8;
synchronized void findeWord() {
}
}
public class Finde implements Runnable{
public void run() {
}
}