HW8
This commit is contained in:
@@ -1,12 +1,5 @@
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Simulates downloading a single chunk of a file.
|
||||
*
|
||||
* <p>This class is intentionally provided as a skeleton for students.
|
||||
* The main multithreading and simulation logic should be completed
|
||||
* in the run() method.</p>
|
||||
*/
|
||||
public class DownloadWorker implements Runnable {
|
||||
|
||||
private final ChunkStatus chunkStatus;
|
||||
@@ -21,23 +14,36 @@ public class DownloadWorker implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// TODO: Record the chunk start time in chunkStatus.
|
||||
chunkStatus.setStartTimeMs(System.currentTimeMillis());
|
||||
System.out.println(Thread.currentThread().getName() + " started downloading.");
|
||||
|
||||
double downloaded = 0.0;
|
||||
double chunkSize = chunkStatus.getChunkSizeMB();
|
||||
|
||||
// TODO: Print a message that this chunk has started downloading.
|
||||
while (downloaded < chunkSize) {
|
||||
int delayMs = config.getMinStepDelayMs() + random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs() + 1);
|
||||
|
||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||
// TODO: Generate a random sleep delay between min and max delay.
|
||||
// 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.
|
||||
try {
|
||||
Thread.sleep(delayMs);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(Thread.currentThread().getName() + " was interrupted.");
|
||||
return;
|
||||
}
|
||||
|
||||
double stepDownload = config.getMinStepDownloadMB() + (config.getMaxStepDownloadMB() - config.getMinStepDownloadMB()) * random.nextDouble();
|
||||
downloaded += stepDownload;
|
||||
|
||||
if (downloaded > chunkSize) {
|
||||
downloaded = chunkSize;
|
||||
}
|
||||
|
||||
chunkStatus.setDownloadedMB(downloaded);
|
||||
}
|
||||
|
||||
// TODO: Mark the chunk as completed.
|
||||
// TODO: Record the chunk end time in chunkStatus.
|
||||
// TODO: Print a message that this chunk has finished downloading.
|
||||
chunkStatus.setCompleted(true);
|
||||
chunkStatus.setEndTimeMs(System.currentTimeMillis());
|
||||
|
||||
System.out.println(Thread.currentThread().getName() + " finished downloading.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user