Search code examples
javamultithreadingurldownloadthreadpool

Why the use of Threads doesn't speed up my program? Java


I'm pretty new to multi-processing and i was wondering how much was it convenient to use them in multiple file's download. Basically, i have an application (perfectly working that downloads files (images, videos...) from an URL and i wanted to speed up this downloads (now they are sequential) splitting them on multiple threads. So i created a class "PrimeThread" overriding the run method of the thread class and runned a Thread instance in the main for every download, but i don't notice any speed up in time performances. Here's the code i wrote (in main):

                for(int e = 0;e<videos.size();e++) //for every video i create a new thread
                {
                    PrimeThread thread= new PrimeThread(downloader,path1,path2);
                    thread.run();
                }

Here's the code I wrote in the Thread class:

 import java.io.IOException;

class PrimeThread extends Thread {
     HttpDownloadUtility scaricatore; //instance of the "downloader" class 
     String path1, path2;
     PrimeThread(HttpDownloadUtility scaricatore,String path1,String path2) {
         this.scaricatore = scaricatore;
         this.path1 = path1;
         this.path2 = path2;
     }

     public void run() {
          try {
            scaricatore.downloadMedia(path1, path2); //method of the "downloader" class that takes 2 paths in input and downloads from the 1st and put the file downloaded in the 2nd path
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }
}

Solution

  • Use Thread.start instead of Thread.run

    To clarify: You executed method PrimeThread.run, which is just an ordinary method, which is executed synchronously. This means, that in the loop, next "run" runs only after previous one finishes. You can safely delete "extends Thread" and your program will compile and run just fine.

    The real "magic" of the Thread class is in the method "start". It is handled in a special way by JVM. It creates a thread on OS level and starts executing whatever piece of code you put into "run".

    By the way, it is somehow not a good practice to extend from class Thread. Instead, you should define the code, which you want to run in an class, which implements java.lang.Runnable and use Thread constructor new Thread(runnable).

    Main loop:

    for(int e = 0;e<videos.size();e++) //for every video i create a new thread
    {
        Thread thread= new Thread(new PrimeRunnable(downloader,path1,path2));
        thread.start();
    }
    

    Runnable:

    class PrimeRunnable implements Runnable {
        HttpDownloadUtility scaricatore; //instance of the "downloader" class
        String path1, path2;
    
        PrimeRunnable(HttpDownloadUtility scaricatore,String path1,String path2) {
            this.scaricatore = scaricatore;
            this.path1 = path1;
            this.path2 = path2;
        }
    
        public void run() {
            try {
                scaricatore.downloadMedia(path1, path2); //method of the "downloader" class that takes 2 paths in input and downloads from the 1st and put the file downloaded in the 2nd path
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }