java – Uploading files to multiple streams

Question:

public class LoaderBean {


    protected void download(List<File> files, String path, String nThreads) {
        for (File file : files) {
            new Downloader(file.getUrl(), path, file.getName()).startDownload();
            System.out.println(Thread.currentThread().getName());
            System.out.println(" ============================== ");
        }
    }
}

There is such a code for downloading files. At the entrance comes a file with a list of files, the path where to save and the number of threads to download these files. I don't quite understand how loading via streams should happen.

protected void download(List<File> files, String path, String nThreads) {
        for (File file : files) {
            ExecutorService pool = Executors.newFixedThreadPool(Integer.parseInt(nThreads));
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    new Downloader(file.getUrl(), path, file.getName()).startDownload();
                    System.out.println(Thread.currentThread().getName());
                    System.out.println(" ============================== ");
                }

            });
        }
    }

I tried code like this but only one thread worked.

Answer:

This code started working, however, if you remove join, it stops downloading. What could be the problem?
The fix was that I missed the = in the line

if(countTreads + threads.length <= files.size()) {

public class LoaderBean {


        protected void download(List<File> files, String path, String nThreads) throws InterruptedException {

            int countTreads = 0;
            int n = Integer.parseInt(nThreads);

            while(countTreads < files.size()) {
                Thread[] threads = new Thread[Integer.parseInt(nThreads)];

                for (int j = 0; j < n && j + countTreads != files.size(); ++j) {
                    threads[j] = new Thread(new MyThread(files.get(countTreads + j), path),
                            String.format("Thread %d", j));
                }

                if(countTreads + threads.length <= files.size()) {
                    countTreads = countTreads + threads.length;
                }
                startThreads(threads);
            }
        }

        private static void startThreads(Thread[] threads) throws InterruptedException {
            for (Thread thread : threads) {
                if(thread == null){
                    System.exit(0);
                }
                thread.start();
                //thread.join();
            }

        }
    }

    class MyThread implements Runnable {

        private String path;
        private File file;

        MyThread(File file, String path){
            this.path = path;
            this.file = file;
        }

        public  void run() {
            new Downloader(file.getUrl(), path, file.getName()).startDownload();
            System.out.println(Thread.currentThread().getName());
            System.out.println(" ============================== ");
        }

    }
Scroll to Top
AllEscort