Percy Jackson
Percy Jackson

Reputation: 97

Java : How to implement Multi-Threading into a recursive folder size finder algorithm?

So, I have already developed a single threaded version of the folder size finder, but to make it more interesting I wanted to improve it's speed and hopefully develop my understanding of multi-threaded processes. However after ~ 30 revisions there always seemed to be a problem, whether it was:
- That the main thread printed the result before all threads were done
- The long type overflowed when it shouldn't have (possible double counts maybe)
- The multi-threaded version takes longer than the single threaded version

I'm at a loss, I don't know if such a problem is just not suitable for multi-threading or if I'm going about it wrong. I've placed my most 'successful' revision below.

I realize that it is technically starting then immediately joining the worker thread, making it almost worse than single treading. However it is the only way I have gotten the main thread to display the correct result.

So my main problem is getting the worker thread to only join at the end, making it explore all the sub-folders in parallel, then joining with the main thread to display the correct result.

package sizeBrowserCode;

import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class Main {
static long lengthy;
static Thread worker;
static long sum;

private static long getLength() {
    return Main.lengthy;
}

private static void setLength(long var) {
    Main.lengthy=var;
}

private static long getFolderSize(File folder) {
    long length=0;
    File[] files = folder.listFiles();

    int count = files.length;

    for (int i = 0; i < count; i++) {
        if (files[i].isFile()) {
            length += files[i].length();
        }
        else {
            length += getFolderSize(files[i],true);
        }
    }
    System.out.println(folder+" // "+length);

    return length;
}

private static long getFolderSize(File folder,boolean multiThreaded) {
    if(multiThreaded) {
        long length;
        worker=new Thread(new Runnable() {
             public void run() {

                setLength(getFolderSize(folder));
                 }
                });
            worker.start();
            try {
                worker.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            length=getLength();
        return length;

    }else {
        return getFolderSize(folder);
    }
}


public static String convert_Bytes(long bytes,String prefered_Unit) {
    prefered_Unit=prefered_Unit.toLowerCase().trim();
    switch (prefered_Unit) {
    case "b": return (bytes+" Bytes");
    case "kb": return (bytes/1024+" Kilobytes");
    case "mb": return (bytes/Math.pow(1024,2)+" Megabytes");
    case "gb": return (bytes/Math.pow(1024,3)+" Gigabytes");
    case "tb": return (bytes/Math.pow(1024,4)+" Terabytes");
    default:System.out.println("Incorrect notation; Please use\nb\nkb\nmb\ngb\ntb");
            System.exit(0);
            return null;

    }
}

public static String convert_Bytes(long bytes) {
    double Kb=0;
    double Mb=0;
    double Gb=0;
    double Tb=0;
    String result;
    if (bytes>=1024) {
        Kb=bytes/1024;
        bytes=0;
        result=" Kilobytes";
        if(Kb>=1024) {
            Mb=Kb/1024;
            Kb=0;
            result=" Megabytes";
            if(Mb>=1024) {
                Gb=Mb/1024;
                Mb=0;
                result=" Gigabytes";
                if(Gb>=1024) {
                    Tb=Gb/1024;
                    Gb=0;
                    result=" Terabyte";
                }
            }
        }
    }else {
        result=" Bytes";
    }
    double sum=bytes+Mb+Gb+Tb;
    return (sum+result);
}

public static void main(String[] args) {

    String chosen_Folder =args[0];
    String prefered_Unit;
    String full_Size;
    try {
        prefered_Unit =args[1];
    }catch (Exception e) {
        prefered_Unit=null;
    }

    String local_Folder=System.getProperty("user.dir");
    File full_Path;
    String backslash   ="\\";
    String forwardslash="/";
    String seperater;
    if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0) {
        seperater=backslash;
    }else {
        seperater=forwardslash;
    }
    full_Path=new File(local_Folder+seperater+chosen_Folder);



    System.out.println(full_Path);
    long startTime =System.nanoTime();
    if(prefered_Unit!=null) {
        full_Size=convert_Bytes(getFolderSize(full_Path),prefered_Unit);

    }else {
        full_Size=convert_Bytes(getFolderSize(full_Path));
    }
    long endTime =System.nanoTime();

    System.out.println("The size of "+chosen_Folder+" is: "+full_Size);
    System.out.println("Took "+TimeUnit.NANOSECONDS.toSeconds(endTime-startTime)+" seconds to execute.");
    File size_Indicator = new File(full_Path+seperater+"FileSize.txt");
    try {
    size_Indicator.createNewFile();
    FileWriter writer = new FileWriter(size_Indicator);
    writer.write(full_Size);
    writer.close();
    }catch(Exception e) {
        System.err.println(e);
    }

}

}

Upvotes: 0

Views: 344

Answers (1)

The Impaler
The Impaler

Reputation: 48770

Just to complement other answers.

Try to avoid using

worker = new Thread(new Runnable() {
    ...
  }

Use an Executor instead. They are much easier to use, to schedule, to run, and to get their results back. Plus they take care of Thread pooling and other overhead you have.

Read this.

Executors are available since Java 5 and they are an integral part of any JVM.

Upvotes: 2

Related Questions