2016-12-14 2 views
0

Я пытаюсь получить ПОЧТУ «FileHandler.java», чтобы вернуть значение (final_match) в «main.java» и из put. Проблема заключается в том, что final_match выходы как 0. В «FileHandler.java» За пределами моего добытчика final_match имеет правильное значение, но при вызове/вернулись из значения геттера равнины 0Java Setter и Getter

main.java

package textreader; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class main { 

public String data = ""; 

public static void main(String[] args) { 

    Scanner user_input = new Scanner(System.in); 
    final File directory = new File("E:\\test\\filename.txt"); 
    // final File directory2 = new File("E:\\test\\filename2.txt"); 
    ArrayList<File> f = new ArrayList<>(); 
    f.add(directory); 
    // f.add(directory2); 
    final String words; 

    System.out.println("The word you would like to search for. "); 
    System.out.println("----------------------------------------"); 
    words = user_input.next(); 

    main aMain = new main(f, words); 

} 

main(ArrayList<File> f, String words) { //constructor 
    ArrayList<FileHandler> threadArray = new ArrayList<>();//arraylist of type filehandler 
    for (File file : f) { 
     FileHandler fh = new FileHandler(this, words, file);// instance of filehandler = fh 
     threadArray.add(fh); 
     fh.start(); 

     for (FileHandler x : threadArray) { 
      if (x.isFinished()) { 
       x.setFinished(true); 
       synchronized (x) { 
        x.notify();// notify next thread to continue 

       } 
      } 

      int answer = x.getfinal_match(); // CALLING THE GETTER FOR VALUE 
      System.out.println("----------------------------------------"); 
      System.out.println("this word has appeared: " + answer + " times."); 
      System.out.println("----------------------------------------"); 

     } 
    } 
} 
} 

FileHander.java

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 

public class FileHandler extends Thread { 

BufferedReader br = null; 
FileReader fr = null; 
String words; 
File file; 
int counter; 
int match; 
int final_match; 
boolean done = true; 
boolean finished; 
private final main main; 

public FileHandler(main instance, String words, File file) { //constructor 
    this.words = words; 
    this.file = file; 
    this.main = instance; 
} 

@Override 
public synchronized void run() { 
    try { 
     fr = new FileReader(this.file);//file path 
     br = new BufferedReader(fr);// reads the words in the file 
     String theLine; 

     while ((theLine = br.readLine()) != null) { 
      String List[] = theLine.split(" "); 
      for (String List1 : List) { 
       if (List1.equals(List[counter])) {// of words are the same as "word" increment match 
        match++; //Amount of occurrences 
       } 

       counter++;//loop through each word 

      } 

      synchronized (this) { 
       //System.out.println("test2 " + match); 
       this.finished = true; 
      } 
     } 
    } catch (IOException e) { 
    } finally { 
     try { 
      if (br != null) { 
       br.close(); 
      } 
      if (fr != null) { 
       fr.close(); 
      } 
     } catch (IOException ex) { 
     } 
    } 

    final_match = match; 

    System.out.println("testing " + final_match); // THIS TEST WORKS AS VALUE WAS OUTPUTTED AS 5 
} 

public void setfinal_match(int test) { 

    final_match = test; 

} 

public Integer getfinal_match() { 

    System.out.println("testing 123456 " + final_match); // THIS VALUE DOES NOT WORK AS IT OUTPUTS A BIG FAT 0 
    return final_match; 
} 

public boolean isFinished() { 
    return this.finished; 
} 

public void setFinished(boolean finished) { 
    this.finished = finished; 

} 

} 

//OUTPUT 
//run: 
//the word you would like to search for. 
//---------------------------------------- 
//dog 
//testing GETTER 123456 0 
//---------------------------------------- 
//this word has appeared: 0 times. 
//---------------------------------------- 
//testing 5 
//BUILD SUCCESSFUL (total time: 2 seconds) 




//EXPECTED OUTPUT 
//run: 
//The word you would like to search for. 
//---------------------------------------- 
//dog 
//testing GETTER 123456 5 
//---------------------------------------- 
//this word has appeared: 5 times. (THIS IS THE MAIN VALUE THAT HAS TO CHANGE) 
//---------------------------------------- 
//testing 5 
//BUILD SUCCESSFUL (total time: 2 seconds) 
+1

Какова ваша мощность и ожидаемый выход? – shmosel

+0

Ваш геттер работает до обновления. Вам нужно дождаться завершения потока. – shmosel

+0

@shmosel, если я могу спросить, как это делается –

ответ

0

Ваш final_match 0 в момент выхода, так как заходов резьбы и еще не закончена, когда вы выводите значение final_match.

+0

если я могу спросить, как я могу решить эту проблему –

+0

Вы могли бы, например, изменить объявление boolean done = true; to boolean done = false; так что вы уже не запускаете FileHandler. – rob