You can create some kind of listener to get feedback from the stream.
public interface ResultListener { public synchronized void result(int words); } private String filename; private ResultListener listener; public void run() { int count = 0; try { Scanner in = new Scanner(new File(filename)); while (in.hasNext()) { in.next(); count++; } listener.result(count); } catch (FileNotFoundException e) { System.out.println(filename + " blev ikke fundet."); } } }
You can add the contructor parameter for the listener as well as for your file name.
public class Main { private static int totalCount = 0; private static ResultListener listener = new ResultListener(){ public synchronized void result(int words){ totalCount += words; } } public static void main(String args[]) { for (String filename : args) { Runnable tester = new WordCount(filename, listener); Thread t = new Thread(tester); t.start(); } } }
source share