Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class WordCounter implements Runnable
- {
- private String fileName;
- private File file;
- private int x;
- private static int threadCount = 0;
- private static int wordCountTotal;
- public WordCounter(String args)
- {
- fileName = args;
- file = new File(args);
- }
- @Override
- public void run()
- {
- try
- {
- Scanner fileScanner = new Scanner(file);
- Thread.sleep(100);
- //String string = new String();
- int count = 0;
- while(fileScanner.hasNext())
- {
- //string+=string + " ";
- fileScanner.next();
- count++;
- try
- {
- Test.lock.lock();
- wordCountTotal++;
- }
- finally
- {
- Test.lock.unlock();
- }
- }
- fileScanner.close();
- threadCount++;
- System.out.println(fileName + ": " + count);
- }
- catch(InterruptedException e)
- {
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- if(threadCount == Test.size)
- {
- System.out.println(fileName + " is last thread, total: " + wordCountTotal);
- }
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement