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

            file = new Scanner(new File("myfile.txt")); 
        catch(IOException e){ 


            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() {


Hello Mazahir.

Exactly what help do you need? Nobody here will write the solution for you, but if there's some specific problem that is stopping you, we will help with that.

i need to know how can i create many threads that can count the words from array without crashing to eachother.

You create threads by creating new instances of the Thread class - just look up the API doc or Google for the Oracle tutorial.
If you do as instructed, and have each thread process a different part of the array, then you will have no "crashing".
Just go ahead and try - it's not as hard as it looks.

I am going to take the hints from everybody else in the forum, and not give away homework answers, but I coded something like this in C# just now, and noticed that the multithreaded searching algorithims worked better for only massive strings, like in a 45 MB text file. Anything less is just inefficient. Wonder how I would learn about optimization and stuff, are there books on it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.