java programming
Part 1.
Consider the three threads shown below. Using pseudo-code to show how it is possible to ensure that section a2 will run before section b2 and that section b2 will run
before section c2.
Thread A Thread B Thread C
section a1 section b1 section c1
section a2 section b2 section c2
Part 2
Process
Burst
Priority
P1
8
4
P2
6
1
P3
1
2
Using the above data
Find the Avg Wait time and Avg Turnaround time using
FCFS
SJF
non preemptive priority
Round Robin(Quantum of 1)
Part 3
use this code as a guide
/**
* Starter code for the Sandwich Shop Problem
*/
import java.util.concurrent.Semaphore;
public class SandwichShopStarter {
//DECLARE YOUR STATIC SEMAPHORES HERE
public static void main(String[] args) {
if (args.length != 1) {
printUsage();
}
int num = 0;
try {
num = Integer.parseInt(args[0]); // number of Customers
}
catch (NumberFormatException e) {
printUsage();
}
//INITIALIZE THE SEMAPHORES HERE THINK IF THEY START AT 0 OR 1
System.out.println(“Customer:\t\t\t\t\t\t\t\t\t\t\t| Employee:”);
System.out.print(“Traveling\tArrived\t\tOrdering\tBrowsing\tAt Register\tLeaving”);
System.out.println(“\t\t| Waiting\tSandwich Making\t\tAt Register\tPayment Accepted”);
System.out .println(“—————————————————”
+ “———————————————+——–”
+ “——————————————————————-“);
Thread emp = new EmployeeThread();
emp.start();
Thread[] custs = new Thread[num];
for (int i = 0; i < num; i++) { custs[i] = new CustomerThread(i); custs[i].start(); } for (int i = 0; i < num; i++) { try { custs[i].join(); } catch (InterruptedException e) { } } System.exit(0); } private static void printUsage() { System.out.println("Usage: java SandwichShop "); System.out.println(" : Number of customers."); System.exit(-1); } public static void randomSleep(int max) { try { Thread.sleep((int) (Math.random() * max)); } catch (InterruptedException e) { } } } class CustomerThread extends Thread { private int id; public CustomerThread(int id) { this.id = id; } public void run() { //HERE you add the acquire and release of the semaphore travelToShop(); arriveAtShop(); placeOrder(); browseShop(); atRegister(); leaveShop(); } private void travelToShop() { System.out.println("Customer " + id + "\t\t\t\t\t\t\t\t\t\t\t|"); SandwichShop.randomSleep(1000); } private void arriveAtShop() { System.out.println("\t\tCustomer " + id + "\t\t\t\t\t\t\t\t\t|"); } private void placeOrder() { System.out.println("\t\t\t\tCustomer " + id + "\t\t\t\t\t\t\t|"); } private void browseShop() { System.out.println("\t\t\t\t\t\tCustomer " + id + "\t\t\t\t\t|"); SandwichShop.randomSleep(1000); } private void atRegister() { System.out.println("\t\t\t\t\t\t\t\tCustomer " + id + "\t\t\t|"); } private void leaveShop() { System.out.println("\t\t\t\t\t\t\t\t\t\tCustomer " + id + "\t|"); } } class EmployeeThread extends Thread { public void run() { while (true) { //HERE you add the acquire and release of the semaphore waitForCustomer(); makeSandwich(); atCashRegister(); paymentAccepted(); } } private void waitForCustomer() { System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| Employee"); } private void makeSandwich() { System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\tEmployee"); SandwichShop.randomSleep(1000); } private void atCashRegister() { System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\tEmployee"); } private void paymentAccepted() { System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\t\t\tEmployee"); } } This code simulates a Sandwich shop. You read in from the command line the number of Customer Threads. There is just one Employee Thread. Here are the rules: 1.The Employee waits on one customer at a time. 2. The Employee cannot make the sandwich until the customer orders it. 3. After ordering the customer browses the store. After making the sandwich the employee waits at register for Customer. 4. Customer pays and leaves and the Employee gets the next customer