Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2015
    Thanked 0 Times in 0 Posts

    Thread read from file and put words in array not in arraylist, file has 267752 String

    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, 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();
    Runnable r = new Thread(read[i]);
    Thread t = new Thread(r);

    public class CommonObject {

    private final int K = 8;
    private String word;
    private int count = 0;

    synchronized void findeWord(String word) {
    this.word = word;

    synchronized void countWord(int count) {

    count = count;

    public class Finde implements Runnable {
    static volatile int i;

    public void run() {
    while(true) {


  2. #2
    New to the CF scene
    Join Date
    Sep 2018
    Thanked 0 Times in 0 Posts
    Interesting stuff. I too came across this issue and got a solution from here https://bit.ly/2zBDdDG. Hopes it may help you too. Thanks


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts