Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #OS LAB 2/3 made by Fensa08
- import java.io.*;
- import java.io.File;
- import java.util.HashSet;
- import java.util.concurrent.Semaphore;
- public class FileScanner extends Thread {
- private static Semaphore semaphore;
- private String fileToScan;
- private static Long counter = (long)0;
- public FileScanner (String fileToScan) {
- this.fileToScan=fileToScan;
- counter++;
- }
- public static void printInfo(File file) {
- if(file.isDirectory()){
- System.out.println("dir:" + file.getAbsolutePath() + " " + file.length());
- }else if(file.isFile()){
- System.out.println("file: " + file.getAbsolutePath() + " " + file.length());
- }
- }
- public static Long getCounter () {
- return counter;
- }
- public void run() {
- File file = new File(fileToScan);
- File [] files = file.listFiles();
- HashSet<FileScanner> thread = new HashSet<>();
- printInfo(file);
- for (File f : files) {
- if(!f.isDirectory()){
- printInfo(f);
- }else{
- FileScanner fajl = new FileScanner(f.getAbsolutePath());
- thread.add(fajl);
- fajl.start();
- }
- //TODO: wait for all the FileScanner-s to finish
- try{
- for(FileScanner fi : thread){
- fi.join();
- }
- } catch (InterruptedException e) {
- System.out.println(e);
- }
- }
- }
- public static void main (String [] args) throws InterruptedException {
- String FILE_TO_SCAN = "C:\\Users\\Stefan\\Desktop\\Stefan";
- Semaphore semaohore = new Semaphore(1);
- counter = (long)0;
- FileScanner fileScanner = new FileScanner(FILE_TO_SCAN);
- fileScanner.start();
- fileScanner.join();
- //TODO wait for the fileScanner to finish
- System.out.println(getCounter());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement