Develop #1
@@ -22,22 +22,45 @@ public class DownloadWorker implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
// TODO: Record the chunk start time in chunkStatus.
|
// 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.
|
// TODO: Print a message that this chunk has started downloading.
|
||||||
|
System.out.println(Thread.currentThread().getName() + " started downloading Chunk #" + chunkStatus.getChunkId());
|
||||||
|
|
||||||
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
while (downloaded < chunkStatus.getChunkSizeMB()) {
|
||||||
// TODO: Generate a random sleep delay between min and max delay.
|
// TODO: Generate a random sleep delay between min and max delay.
|
||||||
|
int minDelay = config.getMinStepDelayMs() ;
|
||||||
|
int maxDelay = config.getMaxStepDalayMs() ;
|
||||||
|
int delay = minDelay + random.nextInt(maxDelay-minDelay +1) ;
|
||||||
|
|
||||||
// TODO: Sleep for that delay.
|
// TODO: Sleep for that delay.
|
||||||
|
Thread.sleep(delay) ;
|
||||||
// TODO: Generate a random download amount for this step.
|
// TODO: Generate a random download amount for this step.
|
||||||
|
double minStep = config.getMinStepDownloadMB() ;
|
||||||
|
double maxStep = config.getMaxStepDownloadMB() ;
|
||||||
|
double stepDownload = minStep + ((maxStep-minStep)*random.nextDouble()) ;
|
||||||
|
|
||||||
|
|
||||||
// TODO: Increase downloaded, but do not go beyond chunk size.
|
// TODO: Increase downloaded, but do not go beyond chunk size.
|
||||||
|
downloaded += stepDownload ;
|
||||||
|
if (downloaded > chunkStatus.getChunkSizeMB())
|
||||||
|
{
|
||||||
|
downloaded = chunkStatus.getChunkSizeMB() ;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Save the updated downloaded value into chunkStatus.
|
// TODO: Save the updated downloaded value into chunkStatus.
|
||||||
|
chunkStatus.setDownloadedMB(downloaded) ;
|
||||||
// TODO: Optionally print step-by-step progress.
|
// TODO: Optionally print step-by-step progress.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: Mark the chunk as completed.
|
// TODO: Mark the chunk as completed.
|
||||||
|
chunkStatus.setCompleted(true );
|
||||||
// TODO: Record the chunk end time in chunkStatus.
|
// TODO: Record the chunk end time in chunkStatus.
|
||||||
|
chunkStatus.setEndTimeMs(System.currentTimeMillis()) ;
|
||||||
// TODO: Print a message that this chunk has finished downloading.
|
// TODO: Print a message that this chunk has finished downloading.
|
||||||
|
System.out.println(Thread.currentThread().getName() + " finished downloading Chunk #" + chunkStatus.getChunkId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ public class ProgressMonitor implements Runnable {
|
|||||||
// 6. Otherwise sleep for monitorDelayMs and continue
|
// 6. Otherwise sleep for monitorDelayMs and continue
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
double totalDownloadedMB = 0.0;
|
double totalDownloadedMB = 0.0;
|
||||||
int completedChunks = 0;
|
int completedChunks = 0;
|
||||||
Reference in New Issue
Block a user