Skip to content
Snippets Groups Projects
Commit dc4f52f0 authored by Miska Sulander's avatar Miska Sulander
Browse files

excerise 2 ready

parent 7a48afd0
No related branches found
No related tags found
No related merge requests found
package fi.utu.tech.threadrunner2.assignment;
import java.util.concurrent.BlockingQueue;
import fi.utu.tech.threadrunner2.mediator.ControlSet;
import fi.utu.tech.threadrunner2.mediator.Mediator;
import fi.utu.tech.threadrunner2.works.Work;
public class MyRunnable implements Runnable{
private Mediator mediator;
private ControlSet control;
private BlockingQueue<Work> workList;
MyRunnable(Mediator mediator, ControlSet control) {
this.mediator = mediator;
this.control = control;
}
public void run() {
// register thread
mediator.registerThread(this.hashCode(), "Thread");
mediator.setRunStatus("Created", this.hashCode());
// Set thread status to running
mediator.setRunStatus("Running", this.hashCode());
// Run works from work list while works are present in work list
while (!((workList = mediator.getWorkSlice(control.getBlockSize())).isEmpty())) {
for( Work item : workList) {
mediator.setWorkStatus("Calculating", item);
item.work();
mediator.setWorkStatus("Done", item);
mediator.increaseCalculated(this.hashCode());
}
}
// Set thread status to ended when worklist is empty
mediator.setRunStatus("Ended", this.hashCode());
}
}
......@@ -2,16 +2,31 @@ package fi.utu.tech.threadrunner2.assignment;
import fi.utu.tech.threadrunner2.mediator.ControlSet;
import fi.utu.tech.threadrunner2.mediator.Mediator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Task2UsingExecutorDistributor implements Distributor {
private Mediator mediator;
private ControlSet control;
public Task2UsingExecutorDistributor(Mediator mediator, ControlSet control) {
this.mediator = mediator;
this.control = control;
}
public void execute() {
// Create ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(control.getThreadCount());
for (int i = 0; i<control.getThreadCount(); i++) {
// Create runnable and execute
MyRunnable worker = new MyRunnable(mediator, control);
executor.execute(worker);
}
// Shut down executor to conserve memory
executor.shutdown();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment