Download Files with Parallel in Java Threads


Create package demo and create new java class as below:

package demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ParallelTasks {

	private final Collection<Runnable> tasks = new ArrayList<Runnable>();

	public void add(final Runnable task) {
		tasks.add(task);
	}

	public void go() throws InterruptedException {
		final ExecutorService threads = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
		try {
			final CountDownLatch latch = new CountDownLatch(tasks.size());
			for (final Runnable task : tasks)
				threads.execute(new Runnable() {
					public void run() {
						try {
							task.run();
						} finally {
							latch.countDown();
						}
					}
				});
			latch.await();
		} finally {
			threads.shutdown();
		}
	}

}




In demo package, create new java class as below:

package demo;

import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;

public class DownloadFileTask implements Runnable {

	private String url;
	private final String toPath;

	public DownloadFileTask(String url, String toPath) {
		this.url = url;
		this.toPath = toPath;
	}

	@Override
	public void run() {
		try (InputStream in = URI.create(url).toURL().openStream()) {
			Files.copy(in, Paths.get(toPath));
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

}

In demo package, create Main java class as below:

package demo;

import java.io.File;

public class Main {

	public static void main(String[] args) {

		try {
			ParallelTasks tasks = new ParallelTasks();
			File file1 = new File("http://octopuscodes.com/my_files/a.txt");
			File file2 = new File("http://octopuscodes.com/my_files/b.txt");
			File file3 = new File("http://octopuscodes.com/my_files/c.txt");
			File file4 = new File("http://octopuscodes.com/my_files/d.txt");
			File file5 = new File("http://octopuscodes.com/my_files/e.txt");
			File[] files = { file1, file2, file3, file4, file5 };
			for (File file : files) {
				tasks.add(new DownloadFileTask(file.getAbsolutePath(), "c:\\to_my_files\\" + file.getName()));
			}
			tasks.go();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}

	}

}