prometheus800

ОС Лаб 2: File System Scanning

Mar 23rd, 2021
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.HashSet;
  3. import java.util.Objects;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;
  6.  
  7. public class FileScanner extends Thread {
  8.  
  9.     private final String fileToScan;
  10.  
  11.  
  12.     //TODO: Initialize the start value of the counter
  13.     private static Long counter = 0L;
  14.     public static Lock lock = new ReentrantLock();
  15.  
  16.     public FileScanner(String fileToScan) {
  17.         this.fileToScan = fileToScan;
  18.         //TODO: Increment the counter on every creation of FileScanner object
  19.         try {
  20.             lock.lock();
  21.             counter++;
  22.         } finally {
  23.             lock.unlock();
  24.         }
  25.     }
  26.  
  27.     public static void printInfo(File file) {
  28.  
  29.         //TODO: Print the info for the @argument File file, according to the requirement of the task
  30.         if (file.isDirectory())
  31.             System.out.printf("dir: %s %d\n", file.getName(), Objects.requireNonNull(file.listFiles()).length);
  32.         else
  33.             System.out.printf("file: %s %d\n", file.getName(), file.length());
  34.     }
  35.  
  36.     public static Long getCounter() {
  37.         return counter;
  38.     }
  39.  
  40.  
  41.     public void run() {
  42.  
  43.         //TODO Create object File with the absolute path fileToScan.
  44.         File file;
  45.         file = new File(this.fileToScan);
  46.  
  47.         //TODO Create a list of all the files that are in the directory file.
  48.         File[] files;
  49.         files = file.listFiles();
  50.  
  51.         // Test test
  52.         HashSet<Thread> newThreads = new HashSet<>();
  53.  
  54.         for (File f : files) {
  55.  
  56.             /*
  57.              * TODO If the File f is not a directory, print its info using the function printInfo(f)
  58.              * */
  59.             if (!f.isDirectory()) {
  60.                 printInfo(f);
  61.             }
  62.  
  63.             /*
  64.              * TODO If the File f is a directory, create a thread from type FileScanner and start it.
  65.              * */
  66.             if (f.isDirectory()) {
  67.                 printInfo(f);
  68.                 FileScanner newFileScanner;
  69.                 newFileScanner = new FileScanner(f.getAbsolutePath());
  70.                 newFileScanner.start();
  71.                 newThreads.add(newFileScanner);
  72.             }
  73.  
  74.  
  75.             //TODO: wait for all the FileScanner-s to finish
  76.         }
  77.  
  78.         for (Thread thread : newThreads) {
  79.             try {
  80.                 thread.join();
  81.             } catch (InterruptedException e) {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.  
  86.     }
  87.  
  88.     public static void main(String[] args) throws InterruptedException {
  89.  
  90.         String FILE_TO_SCAN = "C:\\Users\\Hristijan\\Desktop\\Parent";
  91.  
  92.         //TODO Construct a FileScanner object with the fileToScan = FILE_TO_SCAN
  93.         FileScanner fileScanner;
  94.         fileScanner = new FileScanner(FILE_TO_SCAN);
  95.  
  96.         //TODO Start the thread from type FileScanner
  97.         long startTime = System.nanoTime();
  98.         fileScanner.start();
  99.         //TODO wait for the fileScanner to finish
  100.         fileScanner.join();
  101.         long endTime = System.nanoTime();
  102.         //TODO print a message that displays the number of thread that were created
  103.         System.out.println(getCounter());
  104.  
  105.         System.out.printf("Time of job completion: %fs", (endTime - startTime) * 0.000000001);
  106.     }
  107.  
  108. }
  109.  
Add Comment
Please, Sign In to add comment