We are going to implement a simple downloader.
In this applcation, we can download multiple resources at the same time. But with a limitation.
The limitation is that we can download at most N files simultaneously. For example, if N is equal to 3, we can download 3 files at the same time. Then, when one of the threads has completed downloading a file, another thread will start its process.
You don't have to implement "download" part. Use Thread.sleep(500)
instead to simulate the download opeartion.
MAXIMUM_PERMITS
: The maximum number of downloaders that can download at the same time.
The DownloadManager should take a semaphore, URL, and the output file name.
Use logging to record each operation performed by your threads. Add the following dependencies to your pom.xml
file:
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.11</version>
</dependency>
</dependencies>
Use logger in your class:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ClassName {
private static final Logger log = LoggerFactory.getLogger(ClassName.class);
public void foo() {
log.info("Hello World");
}
}
Parametrize your log:
public class ClassName {
...
public void foo() {
String threadName = "Thread-1";
String url = "https://picsum.photos/200/400";
log.info("{} started with url: {}", threadName, url);
}
...
}
Implement the actual code for the "download" part to download the resources.