add bonus tasks
This commit is contained in:
+13
-14
@@ -20,13 +20,13 @@ public class Main {
|
||||
System.out.println("Chunk count: " + config.getChunkCount());
|
||||
System.out.println();
|
||||
|
||||
// 2. Create chunks
|
||||
|
||||
List<ChunkStatus> chunks = ChunkUtils.createChunks(
|
||||
config.getTotalSizeMB(),
|
||||
config.getChunkCount()
|
||||
);
|
||||
|
||||
// 3. Create worker threads
|
||||
|
||||
List<Thread> workerThreads = new ArrayList<>();
|
||||
|
||||
for (ChunkStatus chunk : chunks) {
|
||||
@@ -35,12 +35,13 @@ public class Main {
|
||||
|
||||
workerThreads.add(workerThread);
|
||||
|
||||
// TODO:
|
||||
// Students may print helpful debug information here,
|
||||
// for example which chunk is assigned to which worker thread.
|
||||
System.out.println("Assigned Worker-" + chunk.getChunkId()
|
||||
+ " to chunk #" + chunk.getChunkId()
|
||||
+ " (" + chunk.getChunkSizeMB() + " MB)");
|
||||
}
|
||||
|
||||
// 4. Create and start monitor thread
|
||||
System.out.println();
|
||||
|
||||
ProgressMonitor monitor = new ProgressMonitor(config, chunks);
|
||||
Thread monitorThread = new Thread(monitor, "Progress-Monitor");
|
||||
|
||||
@@ -78,17 +79,15 @@ public class Main {
|
||||
completedChunks++;
|
||||
}
|
||||
|
||||
System.out.println(
|
||||
"Chunk " + chunk.getChunkId()
|
||||
+ ": " + chunk.getDownloadedMB()
|
||||
+ "/" + chunk.getChunkSizeMB()
|
||||
+ " MB"
|
||||
);
|
||||
System.out.printf(" Chunk %d: %.2f/%.0f MB | Duration: %d ms%n",
|
||||
chunk.getChunkId(), chunk.getDownloadedMB(),
|
||||
chunk.getChunkSizeMB(), chunk.getDownloadDurationMs());
|
||||
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Completed chunks : " + completedChunks + "/" + chunks.size());
|
||||
System.out.println("Downloaded total: " + downloadedMB + "/" + config.getTotalSizeMB() + " MB");
|
||||
System.out.println("Simulation finished.");
|
||||
System.out.printf("Downloaded total : %.2f/%d MB%n", downloadedMB, config.getTotalSizeMB());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public class ProgressMonitor implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
double previousProgress = 0.0;
|
||||
|
||||
while (true) {
|
||||
double totalDownloadedMB = 0.0;
|
||||
@@ -34,15 +35,36 @@ public class ProgressMonitor implements Runnable {
|
||||
percent = (totalDownloadedMB * 100.0) / totalSizeMB;
|
||||
}
|
||||
|
||||
System.out.printf(
|
||||
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n",
|
||||
fileName,
|
||||
totalDownloadedMB,
|
||||
(double) totalSizeMB,
|
||||
percent,
|
||||
completedChunks,
|
||||
chunks.size()
|
||||
);
|
||||
|
||||
double deltaMB = totalDownloadedMB - previousProgress;
|
||||
double speedMBps = deltaMB / (monitorDelayMs / 1000.0);
|
||||
previousProgress = totalDownloadedMB;
|
||||
|
||||
|
||||
double remainingMB = totalSizeMB - totalDownloadedMB;
|
||||
long etaSeconds = (speedMBps > 0) ? (long)(remainingMB / speedMBps) : 0;
|
||||
|
||||
|
||||
int barWidth = 30;
|
||||
int filled = (int)(percent / 100.0 * barWidth);
|
||||
filled = Math.min(filled, barWidth);
|
||||
String bar = "=".repeat(filled) + (filled < barWidth ? ">" : "") + " ".repeat(Math.max(0, barWidth - filled - 1));
|
||||
|
||||
|
||||
System.out.printf("\r[%-30s] %5.1f%% | %5.1f/%d MB | Speed: %.2f MB/s | ETA: %ds | Chunks: %d/%d ",
|
||||
bar, percent, totalDownloadedMB, totalSizeMB,
|
||||
speedMBps, etaSeconds,
|
||||
completedChunks, chunks.size());
|
||||
|
||||
// System.out.printf(
|
||||
// "Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n",
|
||||
// fileName,
|
||||
// totalDownloadedMB,
|
||||
// (double) totalSizeMB,
|
||||
// percent,
|
||||
// completedChunks,
|
||||
// chunks.size()
|
||||
// );
|
||||
|
||||
|
||||
if (completedChunks == chunks.size()) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
fileName=movie.mkv
|
||||
totalSizeMB=120
|
||||
chunkCount=10
|
||||
minStepDelayMs=80
|
||||
maxStepDelayMs=200
|
||||
minStepDownloadMB=2
|
||||
maxStepDownloadMB=6
|
||||
minStepDelayMs=200
|
||||
maxStepDelayMs=600
|
||||
minStepDownloadMB=.1
|
||||
maxStepDownloadMB=.4
|
||||
Reference in New Issue
Block a user