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 cff63de2fb389b8e7fda43732e0ea2b785f0068e..6e91dbf3694480d63d8e0d7e6d502f664c1a9838 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 a7aa648255c2642dec30b1ddc9880742c9444ba3..00ba50dab13eba35596b963f5d7a8b49674e67f6 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(); + } } }