From 7a48afd078fcacf6b75fb53405c6e9cd738c9ecd Mon Sep 17 00:00:00 2001 From: Miska Sulander <mialsu@utu.fi> Date: Fri, 26 Nov 2021 13:11:19 +0200 Subject: [PATCH] assignment 1 ready --- .../tech/threadrunner2/assignment/MyThread.java | 15 +++++++++++---- .../assignment/Task1UsingThreadDistributor.java | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/fi/utu/tech/threadrunner2/assignment/MyThread.java b/src/main/java/fi/utu/tech/threadrunner2/assignment/MyThread.java index cff63de..6e91dbf 100644 --- a/src/main/java/fi/utu/tech/threadrunner2/assignment/MyThread.java +++ b/src/main/java/fi/utu/tech/threadrunner2/assignment/MyThread.java @@ -17,12 +17,11 @@ public class MyThread extends Thread { } public void run() { - mediator.registerThread(this.hashCode(), "Thread"); - mediator.setRunStatus("Created", this.hashCode()); + // Set thread status to running mediator.setRunStatus("Running", this.hashCode()); - int count = control.getBlockSize(); + + // Run works from work list while works are present in work list while (!((workList = mediator.getWorkSlice(control.getBlockSize())).isEmpty())) { - workList = mediator.getWorkSlice(count); //get count amount of works from work queue for( Work item : workList) { mediator.setWorkStatus("Calculating", item); item.work(); @@ -30,6 +29,14 @@ public class MyThread extends Thread { mediator.increaseCalculated(this.hashCode()); } } + + // Set thread status to ended when worklist is empty mediator.setRunStatus("Ended", this.hashCode()); } + + // Method for registering threads + public void register() { + mediator.registerThread(this.hashCode(), "Thread"); + mediator.setRunStatus("Created", this.hashCode()); + } } diff --git a/src/main/java/fi/utu/tech/threadrunner2/assignment/Task1UsingThreadDistributor.java b/src/main/java/fi/utu/tech/threadrunner2/assignment/Task1UsingThreadDistributor.java index a7aa648..00ba50d 100644 --- a/src/main/java/fi/utu/tech/threadrunner2/assignment/Task1UsingThreadDistributor.java +++ b/src/main/java/fi/utu/tech/threadrunner2/assignment/Task1UsingThreadDistributor.java @@ -14,11 +14,22 @@ public class Task1UsingThreadDistributor implements Distributor { } public void execute() { + + // Create list for storing threads Thread threads[] = new Thread[control.getThreadCount()]; - for (int i = 0 ; i < control.getThreadCount() ; ++i) { //get number of threads from UI and create threads + + /* Get number of threads from UI and create threads, + * then register threads. + */ + for (int i = 0 ; i < control.getThreadCount() ; ++i) { threads[i] = new MyThread(mediator, control); - threads[i].start(); - } + ((MyThread) threads[i]).register(); + } + + // Start all threads. + for (Thread thread : threads) { + thread.start(); + } } } -- GitLab