Complete the classes

This commit is contained in:
2026-06-04 23:22:30 +03:30
parent 9e5c715088
commit 3111f23b1c
3 changed files with 51 additions and 64 deletions
+21 -12
View File
@@ -21,23 +21,32 @@ public class DownloadWorker implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: Record the chunk start time in chunkStatus. chunkStatus.setStartTimeMs(System.currentTimeMillis());
double downloaded = 0.0; double downloaded = 0.0;
// TODO: Print a message that this chunk has started downloading. System.out.println("[Worker] Start downloading for " + chunkStatus.getChunkId());
while (downloaded < chunkStatus.getChunkSizeMB()) { while (downloaded < chunkStatus.getChunkSizeMB()) {
// TODO: Generate a random sleep delay between min and max delay. long sleepTime = (long) config.getMinStepDelayMs() + (long) (random.nextDouble() * (config.getMaxStepDelayMs() - config.getMinStepDelayMs()));
// TODO: Sleep for that delay.
// TODO: Generate a random download amount for this step.
// TODO: Increase downloaded, but do not go beyond chunk size.
// TODO: Save the updated downloaded value into chunkStatus.
// TODO: Optionally print step-by-step progress.
}
// TODO: Mark the chunk as completed. try {
// TODO: Record the chunk end time in chunkStatus. Thread.sleep(sleepTime);
// TODO: Print a message that this chunk has finished downloading. } catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} }
double stepSize = config.getMinStepDownloadMB() + (random.nextDouble() * (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB()));
downloaded += stepSize;
if (downloaded > chunkStatus.getChunkSizeMB()) downloaded = chunkStatus.getChunkSizeMB();
chunkStatus.setDownloadedMB(downloaded);
System.out.println("Chunk " + chunkStatus.getChunkId() + " progress: " + downloaded + "/" + chunkStatus.getChunkSizeMB());
}
chunkStatus.setCompleted(true);
chunkStatus.setEndTimeMs(System.currentTimeMillis());
System.out.println("[Worker] Finished download for " + chunkStatus.getChunkId());
}
} }
+23 -38
View File
@@ -5,7 +5,6 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("=== Simulated Download Manager ==="); System.out.println("=== Simulated Download Manager ===");
// 1. Read config
DownloadConfig config; DownloadConfig config;
try { try {
config = ConfigReader.readConfig("download_config.txt"); config = ConfigReader.readConfig("download_config.txt");
@@ -19,13 +18,11 @@ public class Main {
System.out.println("Chunk count: " + config.getChunkCount()); System.out.println("Chunk count: " + config.getChunkCount());
System.out.println(); System.out.println();
// 2. Create chunks
List<ChunkStatus> chunks = ChunkUtils.createChunks( List<ChunkStatus> chunks = ChunkUtils.createChunks(
config.getTotalSizeMB(), config.getTotalSizeMB(),
config.getChunkCount() config.getChunkCount()
); );
// 3. Create worker threads
List<Thread> workerThreads = new ArrayList<>(); List<Thread> workerThreads = new ArrayList<>();
for (ChunkStatus chunk : chunks) { for (ChunkStatus chunk : chunks) {
@@ -34,50 +31,38 @@ public class Main {
workerThreads.add(workerThread); workerThreads.add(workerThread);
// TODO: System.out.println("[Main] Assigned chunk ID " + chunk.getChunkId() +
// Students may print helpful debug information here, " (Size: " + chunk.getChunkSizeMB() + " MB) to " + workerThread.getName());
// for example which chunk is assigned to which worker thread.
} }
// 4. Create and start monitor thread
ProgressMonitor monitor = new ProgressMonitor(config, chunks); ProgressMonitor monitor = new ProgressMonitor(config, chunks);
Thread monitorThread = new Thread(monitor, "Progress-Monitor"); Thread monitorThread = new Thread(monitor, "Progress-Monitor");
// TODO: monitorThread.start();
// Start the monitor thread before starting the workers System.out.println("[Main] Monitor thread started");
// so that progress can be displayed while downloading happens.
//
// Example idea:
// monitorThread.start();
// 5. Start worker threads for (Thread workerThread : workerThreads) {
// TODO: workerThread.start();
// Start each worker thread in workerThreads. }
// Use a loop and call start() on each thread. System.out.println("[Main] All worker threads started.");
// 6. Wait for workers to finish try {
// TODO: for (Thread thread : workerThreads) {
// Wait for all worker threads to complete by calling join(). thread.join();
// This should be done inside a try-catch block for InterruptedException. }
// System.out.println("[Main] All workers finished.");
// Hint: } catch (InterruptedException e) {
// for (Thread thread : workerThreads) { System.out.println("[Main] Main thread interrupted while waiting for workers.");
// thread.join(); Thread.currentThread().interrupt();
// } }
// TODO: try {
// After all workers finish, the monitor thread may also need to stop. monitorThread.join();
// Depending on how ProgressMonitor is implemented, students may: System.out.println("[Main] Monitor finished.");
// - wait for it to finish on its own, or } catch (InterruptedException e) {
// - add a stopping mechanism in ProgressMonitor later. System.out.println("[Main] Main thread interrupted while waiting for monitor.");
// }
// If your monitor finishes automatically, you may join it here.
// NOTE:
// this final report may show 0 progress because no worker has actually run yet.
// Until students complete the thread start/join TODOs above,
// 7. Print final report
System.out.println(); System.out.println();
System.out.println("=== Final Report ==="); System.out.println("=== Final Report ===");
+6 -13
View File
@@ -16,16 +16,7 @@ public class ProgressMonitor implements Runnable {
@Override @Override
public void run() { public void run() {
// TODO: System.out.println("[Monitor] Started monitoring progress...");
// Repeatedly check chunk progress until all chunks are completed.
// In each loop:
// 1. Read the downloaded size from every chunk
// 2. Add all downloaded amounts to totalDownloadedMB
// 3. Count completed chunks
// 4. Print a progress message
// 5. If completedChunks == chunks.size(), print a final monitor message and stop
// 6. Otherwise sleep for monitorDelayMs and continue
while (true) { while (true) {
double totalDownloadedMB = 0.0; double totalDownloadedMB = 0.0;
@@ -54,9 +45,11 @@ public class ProgressMonitor implements Runnable {
chunks.size() chunks.size()
); );
if (completedChunks == chunks.size())
// TODO: {
// If all chunks are completed, print a final message and exit the loop System.out.println("[Monitor] All chunks downloaded successfully!");
break;
}
try { try {
Thread.sleep(monitorDelayMs); Thread.sleep(monitorDelayMs);