Advertisement
fensa08

#OS LAB 2/3

Mar 29th, 2019
1,663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. #OS LAB 2/3 made by Fensa08
  2. import java.io.*;
  3. import java.io.File;
  4. import java.util.HashSet;
  5. import java.util.concurrent.Semaphore;
  6.  
  7.  
  8. public class FileScanner extends Thread {
  9.     private static Semaphore semaphore;
  10.     private String fileToScan;
  11.     private static Long counter = (long)0;
  12.  
  13.     public FileScanner (String fileToScan) {
  14.         this.fileToScan=fileToScan;
  15.         counter++;
  16.     }
  17.  
  18.     public static void printInfo(File file)  {
  19.         if(file.isDirectory()){
  20.             System.out.println("dir:" + file.getAbsolutePath() + " " + file.length());
  21.         }else if(file.isFile()){
  22.             System.out.println("file: " + file.getAbsolutePath() + " " + file.length());
  23.         }
  24.     }
  25.  
  26.     public static Long getCounter () {
  27.         return counter;
  28.     }
  29.  
  30.  
  31.     public void run() {
  32.  
  33.         File file = new File(fileToScan);
  34.  
  35.  
  36.         File [] files = file.listFiles();
  37.         HashSet<FileScanner> thread = new HashSet<>();
  38.         printInfo(file);
  39.         for (File f : files) {
  40.  
  41.             if(!f.isDirectory()){
  42.                 printInfo(f);
  43.             }else{
  44.                 FileScanner fajl = new FileScanner(f.getAbsolutePath());
  45.                 thread.add(fajl);
  46.                 fajl.start();
  47.             }
  48.  
  49.             //TODO: wait for all the FileScanner-s to finish
  50.             try{
  51.                 for(FileScanner fi : thread){
  52.                     fi.join();
  53.                 }
  54.             } catch (InterruptedException e) {
  55.                 System.out.println(e);
  56.             }
  57.  
  58.  
  59.  
  60.         }
  61.  
  62.     }
  63.  
  64.     public static void main (String [] args) throws InterruptedException {
  65.         String FILE_TO_SCAN = "C:\\Users\\Stefan\\Desktop\\Stefan";
  66.         Semaphore semaohore = new Semaphore(1);
  67.         counter = (long)0;
  68.  
  69.         FileScanner fileScanner = new FileScanner(FILE_TO_SCAN);
  70.         fileScanner.start();
  71.         fileScanner.join();
  72.         //TODO wait for the fileScanner to finish
  73.  
  74.  
  75.         System.out.println(getCounter());
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement