Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. /**
  2. * Starter code for the Sandwich Shop Problem
  3. */
  4. import java.util.concurrent.Semaphore;
  5. public class SandwichShopStarter {
  6. //DECLARE YOUR STATIC SEMAPHORES HERE
  7. private static Semaphore sema1 = new Semaphore(0);
  8. private static Semaphore sema2 = new Semaphore(0);
  9. private static int employee = 0;
  10. private static int customer = 0;
  11.  
  12. public static void main(String[] args) {
  13. if (args.length != 1) {
  14. printUsage();
  15. }
  16.  
  17. int num = 0;
  18. try {
  19. num = Integer.parseInt(args[0]); // number of Customers
  20. }
  21. catch (NumberFormatException e) {
  22. printUsage();
  23. }
  24.  
  25. //INITIALIZE THE SEMAPHORES HERE THINK IF THEY START AT 0 OR 1
  26.  
  27. System.out.println("Customer:\t\t\t\t\t\t\t\t\t\t\t| Employee:");
  28. System.out.print("Traveling\tArrived\t\tOrdering\tBrowsing\tAt Register\tLeaving");
  29. System.out.println("\t\t| Waiting\tSandwich Making\t\tAt Register\tPayment Accepted");
  30. System.out .println("---------------------------------------------------"
  31. + "---------------------------------------------+--------"
  32. + "-------------------------------------------------------------------");
  33.  
  34. Thread emp = new EmployeeThread();
  35. emp.start();
  36.  
  37. Thread[] custs = new Thread[num];
  38. for (int i = 0; i < num; i++) {
  39. custs[i] = new CustomerThread(i);
  40. custs[i].start();
  41. }
  42. for (int i = 0; i < num; i++) {
  43. try {
  44. custs[i].join();
  45. }
  46. catch (InterruptedException e) {
  47. }
  48. }
  49.  
  50. System.exit(0);
  51. }
  52.  
  53. private static void printUsage() {
  54. System.out.println("Usage: java SandwichShop <num>");
  55. System.out.println(" <num>: Number of customers.");
  56. System.exit(-1);
  57. }
  58.  
  59. public static void randomSleep(int max) {
  60. try {
  61. Thread.sleep((int) (Math.random() * max));
  62. }
  63. catch (InterruptedException e) {
  64. }
  65. }
  66.  
  67. private static class CustomerThread extends Thread {
  68.  
  69. private int id;
  70. public CustomerThread(int id) {
  71. this.id = id;
  72. }
  73. public void run() {
  74. //HERE you add the acquire and release of the semaphore
  75. travelToShop();
  76.  
  77. arriveAtShop();
  78. placeOrder();
  79. sema1.release();
  80. browseShop();
  81. try {sema2.acquire();} catch(InterruptedException e){}
  82. atRegister();
  83. sema1.release();
  84. try {sema2.acquire();} catch(InterruptedException e){}
  85. leaveShop();
  86.  
  87.  
  88. }
  89.  
  90. private void travelToShop() {
  91. System.out.println("Customer " + id + "\t\t\t\t\t\t\t\t\t\t\t|");
  92. SandwichShopStarter.randomSleep(1000);
  93. }
  94. private void arriveAtShop() {
  95. System.out.println("\t\tCustomer " + id + "\t\t\t\t\t\t\t\t\t|");
  96. }
  97. private void placeOrder() {
  98. System.out.println("\t\t\t\tCustomer " + id + "\t\t\t\t\t\t\t|");
  99. }
  100. private void browseShop() {
  101. System.out.println("\t\t\t\t\t\tCustomer " + id + "\t\t\t\t\t|");
  102. SandwichShopStarter.randomSleep(1000);
  103. }
  104.  
  105. private void atRegister() {
  106. System.out.println("\t\t\t\t\t\t\t\tCustomer " + id + "\t\t\t|");
  107. }
  108.  
  109. private void leaveShop() {
  110. System.out.println("\t\t\t\t\t\t\t\t\t\tCustomer " + id + "\t|");
  111. }
  112. }
  113.  
  114. private static class EmployeeThread extends Thread {
  115.  
  116. public void run() {
  117. //HERE you add the acquire and release of the semaphore
  118. //try {sema1.acquire();} catch(InterruptedException e){}
  119. waitForCustomer();
  120. //insert semaphore here
  121. try {sema1.acquire();} catch(InterruptedException e){}
  122. makeSandwich();
  123. atCashRegister();
  124. sema2.release();
  125. try {sema1.acquire();} catch(InterruptedException e){}
  126. paymentAccepted();
  127. }
  128.  
  129.  
  130. private void waitForCustomer() {
  131. System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| Employee");
  132. }
  133.  
  134. private void makeSandwich() {
  135. System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\tEmployee");
  136. SandwichShopStarter.randomSleep(1000);
  137. }
  138.  
  139. private void atCashRegister() {
  140. System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\tEmployee");
  141. }
  142.  
  143. private void paymentAccepted() {
  144. System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t| \t\t\t\t\t\t\tEmployee");
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement