64 lines
1.9 KiB
Java
64 lines
1.9 KiB
Java
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
DownloadConfig config = ConfigReader.readConfig("src/main/resources/download_config.txt");
|
|
|
|
List<ChunkStatus> chunks = new ArrayList<>();
|
|
|
|
double chunkSize = config.getTotalSizeMB() / config.getChunkCount();
|
|
|
|
for (int i = 0; i < config.getChunkCount(); i++) {
|
|
chunks.add(new ChunkStatus(i + 1, chunkSize));
|
|
}
|
|
|
|
ProgressMonitor monitor = new ProgressMonitor(chunks, config);
|
|
|
|
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
|
|
monitorThread.setDaemon(true);
|
|
monitorThread.start();
|
|
|
|
List<Thread> workerThreads = new ArrayList<>();
|
|
|
|
for (ChunkStatus chunk : chunks) {
|
|
Thread workerThread = new Thread(
|
|
new DownloadWorker(chunk, config),
|
|
"Worker-" + chunk.getChunkId()
|
|
);
|
|
|
|
workerThreads.add(workerThread);
|
|
workerThread.start();
|
|
}
|
|
|
|
try {
|
|
for (Thread thread : workerThreads) {
|
|
thread.join();
|
|
}
|
|
} catch (InterruptedException e) {
|
|
Thread.currentThread().interrupt();
|
|
System.err.println("Main thread interrupted while waiting for workers: " + e.getMessage());
|
|
}
|
|
|
|
double totalDownloaded = 0;
|
|
|
|
for (ChunkStatus chunk : chunks) {
|
|
totalDownloaded += chunk.getDownloadedMB();
|
|
}
|
|
|
|
double totalSize = config.getTotalSizeMB();
|
|
double progress = (totalDownloaded / totalSize) * 100;
|
|
|
|
System.out.println();
|
|
|
|
System.out.printf(
|
|
"Progress: %.2f%% (%.2f / %.2f MB)%n",
|
|
progress,
|
|
totalDownloaded,
|
|
totalSize
|
|
);
|
|
|
|
System.out.println("All workers finished.");
|
|
}
|
|
}
|