Question: need help with this java program. the only files you need to edit are: Lab8PracticeDriver.java and OrderQueue.java************* copy all the code at the bottom of
need help with this java program.
the only files you need to edit are: Lab8PracticeDriver.java and OrderQueue.java*************
copy all the code at the bottom of this and read the directions below, only make changes to Lab8practiceDriver.java and OrderQueue.java.
directions:
This code involves one taskmaster thread and several worker threads. The taskmaster thread creates new tasks and adds them to the order queue. Each task takes a certain number of milliseconds to complete. The worker threads pick up tasks from the order queue and simulate working on them by sleeping for the specified number of milliseconds. The program continues until a total of 100 tasks have been created.
Your tasks for this lab are:
1. Right now the queue can behave inconsistently. For example, occasionally two workers can both accept the same task. Modify the program so that this cannot occur.
2. Right now the program can say it is finished before all of the tasks have been made and completed. Modify the program so that this cannot occur.
3. Right now if a worker requests a task to work on but the queue is empty, the acceptTask method will loop until the queue is no longer empty, as shown
below. This is called a busy loop and it is not an efficient use of CPU time. Modify this code so that the method instead waits until it has been notified
that a new task has been created.
this is the method that needs changed(below) this method is in the OrderQueue.java file
public Task acceptTask() {
while (orders.isEmpty()) {
// looping until there is a task in the queue
}
return orders.poll();
}
4. Change the program so that if there are five tasks in the queue, the program will wait until the number of tasks falls below five before adding any more
tasks to the queue.
here are the files:
**********Lab8practicedriver.java*************(only make changes to this file and lab8driver)
package lab.pkg.lab8b;
public class Lab8bDriver {
public static void main(String[] args) throws Exception { OrderQueue orders = new OrderQueue(); Thread tm = new Thread(new TaskMaster(orders)); Thread w1 = new Thread(new Worker(orders, 1)); Thread w2 = new Thread(new Worker(orders, 2)); Thread w3 = new Thread(new Worker(orders, 3)); tm.start(); w1.start(); w2.start(); w3.start(); System.out.println("Finished?"); }
}
****end of file****** *****orderQueue.java*******(only make changes to this file and lab8driver)
package lab.pkg.lab8b;
import java.util.ArrayDeque; import java.util.Queue;
class OrderQueue { private Queue
***end of file****
***task.java****
package lab.pkg.lab8b;
class Task { private String label; private int timeToComplete; public Task(String label, int timeToComplete) { this.label = label; this.timeToComplete = timeToComplete; } public int getTimeToComplete() { return timeToComplete; } @Override public String toString() { return label + ": " + timeToComplete + " ms"; } }
**end of file***
****taskMaster,java******
package lab.pkg.lab8b;
class TaskMaster implements Runnable {
private final OrderQueue orders; private int ordersCreated; public TaskMaster(OrderQueue orders) { this.orders = orders; this.ordersCreated = 0; } @Override public void run() { while (ordersCreated++ < 100) { String orderLabel = "Task " + ordersCreated; int orderTime = (int) (Math.random() * 1000 + 250); orders.createTask(orderLabel, orderTime); } orders.setNoMoreOrders(); } }
****end of file*********
****worker.java******
package lab.pkg.lab8b;
class Worker implements Runnable { private final OrderQueue orders; private final int workerId; public Worker(OrderQueue orders, int workerId) { this.orders = orders; this.workerId = workerId; }
@Override public void run() { while (!orders.weAreDone()) { Task task = orders.acceptTask(); System.out.println("Worker " + workerId + " accepted " + task); try { Thread.sleep(task.getTimeToComplete()); } catch (InterruptedException e) { System.out.println("Worker " + workerId + " failed to complete " + task); } //System.out.println("Worker " + workerId + " finished " + task); } } }
***end of file****
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
