add bonus tasks

This commit is contained in:
2026-06-03 11:28:11 +03:30
parent a0a186c90f
commit 6840d9374b
3 changed files with 49 additions and 28 deletions
+14 -15
View File
@@ -20,13 +20,13 @@ 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) {
@@ -35,12 +35,13 @@ public class Main {
workerThreads.add(workerThread); workerThreads.add(workerThread);
// TODO: System.out.println("Assigned Worker-" + chunk.getChunkId()
// Students may print helpful debug information here, + " to chunk #" + chunk.getChunkId()
// for example which chunk is assigned to which worker thread. + " (" + chunk.getChunkSizeMB() + " MB)");
} }
// 4. Create and start monitor thread System.out.println();
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");
@@ -78,17 +79,15 @@ public class Main {
completedChunks++; completedChunks++;
} }
System.out.println( System.out.printf(" Chunk %d: %.2f/%.0f MB | Duration: %d ms%n",
"Chunk " + chunk.getChunkId() chunk.getChunkId(), chunk.getDownloadedMB(),
+ ": " + chunk.getDownloadedMB() chunk.getChunkSizeMB(), chunk.getDownloadDurationMs());
+ "/" + chunk.getChunkSizeMB()
+ " MB"
);
} }
System.out.println(); System.out.println();
System.out.println("Completed chunks: " + completedChunks + "/" + chunks.size()); System.out.println("Completed chunks : " + completedChunks + "/" + chunks.size());
System.out.println("Downloaded total: " + downloadedMB + "/" + config.getTotalSizeMB() + " MB"); System.out.printf("Downloaded total : %.2f/%d MB%n", downloadedMB, config.getTotalSizeMB());
System.out.println("Simulation finished.");
} }
} }
+31 -9
View File
@@ -16,6 +16,7 @@ public class ProgressMonitor implements Runnable {
@Override @Override
public void run() { public void run() {
double previousProgress = 0.0;
while (true) { while (true) {
double totalDownloadedMB = 0.0; double totalDownloadedMB = 0.0;
@@ -34,15 +35,36 @@ public class ProgressMonitor implements Runnable {
percent = (totalDownloadedMB * 100.0) / totalSizeMB; percent = (totalDownloadedMB * 100.0) / totalSizeMB;
} }
System.out.printf(
"Progress for %s: %.1f/%.1f MB (%.2f%%), completed chunks: %d/%d%n", double deltaMB = totalDownloadedMB - previousProgress;
fileName, double speedMBps = deltaMB / (monitorDelayMs / 1000.0);
totalDownloadedMB, previousProgress = totalDownloadedMB;
(double) totalSizeMB,
percent,
completedChunks, double remainingMB = totalSizeMB - totalDownloadedMB;
chunks.size() 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()) { if (completedChunks == chunks.size()) {
+4 -4
View File
@@ -1,7 +1,7 @@
fileName=movie.mkv fileName=movie.mkv
totalSizeMB=120 totalSizeMB=120
chunkCount=10 chunkCount=10
minStepDelayMs=80 minStepDelayMs=200
maxStepDelayMs=200 maxStepDelayMs=600
minStepDownloadMB=2 minStepDownloadMB=.1
maxStepDownloadMB=6 maxStepDownloadMB=.4