Search⌘ K
AI Features

Solution: Download Progress Simulator

Explore how to implement a download progress simulator using Java threads. Understand how to run multiple tasks concurrently with proper thread management, simulate network latency, and synchronize thread completion to ensure accurate progress tracking.

We'll cover the following...
Java 25
public class DownloadSimulator {
static class AssetDownloader implements Runnable {
private String assetName;
private int fileSize;
public AssetDownloader(String assetName, int fileSize) {
this.assetName = assetName;
this.fileSize = fileSize;
}
@Override
public void run() {
try {
// We increment by 100MB per step (constant download speed)
for (int current = 0; current <= fileSize; current += 100) {
System.out.println(assetName + ": " + current + "/" + fileSize + " MB");
// Constant latency per chunk
Thread.sleep(100);
}
System.out.println(">>> " + assetName + " finished.");
} catch (InterruptedException e) {
System.out.println(assetName + " interrupted!");
}
}
}
public static void main(String[] args) {
System.out.println("Launcher starting downloads...");
// Larger files = More loop iterations = Longer runtime
Runnable task1 = new AssetDownloader("Texture Pack", 1000);
Runnable task2 = new AssetDownloader("Sound Pack", 600);
Runnable task3 = new AssetDownloader("Map Pack", 200);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
Thread thread3 = new Thread(task3);
thread1.start();
thread2.start();
thread3.start();
try {
// Wait for all downloads to finish
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Game Ready!");
}
}
...