Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class WordCounter implements Runnable
  6. {
  7.     private String fileName;
  8.     private File file;
  9.     private int x;
  10.    
  11.     private static int threadCount = 0;
  12.     private static int wordCountTotal;
  13.    
  14.     public WordCounter(String args)
  15.     {
  16.         fileName = args;
  17.         file = new File(args);
  18.     }
  19.  
  20.     @Override
  21.     public void run()
  22.     {
  23.         try
  24.         {
  25.             Scanner fileScanner = new Scanner(file);
  26.             Thread.sleep(100);
  27.             //String string = new String();
  28.             int count = 0;
  29.             while(fileScanner.hasNext())
  30.             {
  31.                 //string+=string + " ";
  32.                 fileScanner.next();
  33.                 count++;
  34.                 try
  35.                 {
  36.                     Test.lock.lock();
  37.                     wordCountTotal++;
  38.                 }
  39.                 finally
  40.                 {
  41.                     Test.lock.unlock();
  42.                 }
  43.             }
  44.             fileScanner.close();
  45.             threadCount++;
  46.             System.out.println(fileName + ": " + count);
  47.         }
  48.         catch(InterruptedException e)
  49.         {
  50.            
  51.         }
  52.         catch (FileNotFoundException e)
  53.         {
  54.             e.printStackTrace();
  55.         }
  56.         if(threadCount == Test.size)
  57.         {
  58.             System.out.println(fileName + " is last thread, total: " + wordCountTotal);
  59.         }
  60.     }
  61.  
  62.     public int getX() {
  63.         return x;
  64.     }
  65.  
  66.     public void setX(int x) {
  67.         this.x = x;
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement