From 28a223b02c05eb5aa61333130e481f4d74d06bfb Mon Sep 17 00:00:00 2001 From: Mobina Date: Fri, 12 Jun 2026 00:21:31 +0330 Subject: [PATCH] develop --- .idea/misc.xml | 2 +- Answers.md | 23 +++++++++++++++++++++++ src/main/java/DownloadWorker.java | 20 ++++++++++++++++++++ src/main/java/Main.java | 18 +++++++++++------- src/main/java/ProgressMonitor.java | 4 ++++ 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 Answers.md diff --git a/.idea/misc.xml b/.idea/misc.xml index fdc35ea..0c04b52 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/Answers.md b/Answers.md new file mode 100644 index 0000000..4cde53d --- /dev/null +++ b/Answers.md @@ -0,0 +1,23 @@ +1- +Calling run() +Running in: main +Calling start() +Running in: Thread-2 + +متود run() فقط داخل main نخ را اجرا می کند اما متود start() نخ را به صورت جداگانه اجرا می کند + +2- +Main thread ends. +چون اگر نخ اصلی ای در حال اجرا نباشد Daemon thread ها اجرا نمی شوند + +20 بار "Daemon thread running..." چاپ می شود + +نمایش درصد پیشرفت یک دانلود Daemon Thread است اما خود دانلود یک نخ اصلی است + +3- +Thread is running using a ...! +Lambda + + + + diff --git a/src/main/java/DownloadWorker.java b/src/main/java/DownloadWorker.java index 546de2d..b074f7f 100644 --- a/src/main/java/DownloadWorker.java +++ b/src/main/java/DownloadWorker.java @@ -23,8 +23,11 @@ public class DownloadWorker implements Runnable { public void run() { // TODO: Record the chunk start time in chunkStatus. double downloaded = 0.0; + chunkStatus.setStartTimeMs(System.currentTimeMillis()); + chunkStatus.setDownloadedMB(downloaded); // TODO: Print a message that this chunk has started downloading. + System.out.println("Chunk #" + chunkStatus.getChunkId() + " started downloading."); while (downloaded < chunkStatus.getChunkSizeMB()) { // TODO: Generate a random sleep delay between min and max delay. @@ -33,11 +36,28 @@ public class DownloadWorker implements Runnable { // 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. + int delay = config.getMinStepDelayMs() + random.nextInt(config.getMaxStepDelayMs() - config.getMinStepDelayMs()); + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + downloaded+=0.5; + + if (downloaded > chunkStatus.getChunkSizeMB()) + downloaded = chunkStatus.getChunkSizeMB(); + 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.setEndTimeMs(System.currentTimeMillis()); + chunkStatus.setCompleted(true); + System.out.println("Chunk #" + chunkStatus.getChunkId() + " finished downloading."); } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 82bd239..15c8e1a 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -47,23 +47,27 @@ public class Main { // Start the monitor thread before starting the workers // so that progress can be displayed while downloading happens. // - // Example idea: - // monitorThread.start(); + monitorThread.start(); // 5. Start worker threads // TODO: // Start each worker thread in workerThreads. // Use a loop and call start() on each thread. + for (Thread t:workerThreads){ + t.start(); + } // 6. Wait for workers to finish // TODO: // Wait for all worker threads to complete by calling join(). // This should be done inside a try-catch block for InterruptedException. - // - // Hint: - // for (Thread thread : workerThreads) { - // thread.join(); - // } + for (Thread t : workerThreads) { + try { + t.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } // TODO: // After all workers finish, the monitor thread may also need to stop. diff --git a/src/main/java/ProgressMonitor.java b/src/main/java/ProgressMonitor.java index 475c0e0..3837e2d 100644 --- a/src/main/java/ProgressMonitor.java +++ b/src/main/java/ProgressMonitor.java @@ -57,6 +57,10 @@ public class ProgressMonitor implements Runnable { // TODO: // If all chunks are completed, print a final message and exit the loop + if(completedChunks==chunks.size()){ + System.out.println("Procsses Done!"); + break; + } try { Thread.sleep(monitorDelayMs); -- 2.54.0