Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import java.util.concurrent.CountDownLatch;
  2. import java.util.concurrent.ThreadLocalRandom;
  3. import java.util.function.Function;
  4.  
  5. public class RiverCrossing {
  6. private int numHackers = 0;
  7. private int numEmployees = 0;
  8. CountDownLatch seats = new CountDownLatch(4);
  9. long startMillis = System.currentTimeMillis();
  10. private Object boatMonitor = new Object();
  11.  
  12. public void hackerArrives(String name) throws InterruptedException {
  13. CountDownLatch seat = null;
  14.  
  15. synchronized (boatMonitor) {
  16. if (numEmployees + numHackers == 4 || numEmployees == 3 || (numHackers == 2 && numEmployees == 1))
  17. boatMonitor.wait();
  18.  
  19. if (numEmployees + numHackers < 4 && numEmployees < 3 && (numHackers == 2 && numEmployees == 1)) {
  20. int oldValue = numHackers;
  21. numHackers = oldValue + 1;
  22. long now = System.currentTimeMillis() - startMillis;
  23. System.out.printf("[+%04d] %s has boarded the boat.%n", now, name);
  24. seat = boardBoat();
  25. }
  26.  
  27. }
  28.  
  29. seat.await();
  30. synchronized (boatMonitor) {
  31. if (numEmployees + numHackers == 4)
  32. rowBoat();
  33. }
  34. long now = System.currentTimeMillis() - startMillis;
  35. System.out.printf("[+%04d] %s has left the river.%n", now, name);
  36. }
  37.  
  38. public void employeeArrives(String name) throws InterruptedException {
  39. CountDownLatch seat = null;
  40.  
  41. synchronized (boatMonitor) {
  42. if (numEmployees + numHackers == 4 || numHackers == 3 || (numEmployees == 2 && numHackers == 1))
  43. boatMonitor.wait();
  44.  
  45. if (numEmployees + numHackers < 4 && numHackers < 3 && (numEmployees == 2 && numHackers == 1)) {
  46. int oldValue = numEmployees;
  47. numEmployees = oldValue + 1;
  48. long now = System.currentTimeMillis() - startMillis;
  49. System.out.printf("[+%04d] %s has boarded the boat.%n", now, name);
  50. seat = boardBoat();
  51. }
  52. }
  53.  
  54. seat.await();
  55. synchronized (boatMonitor) {
  56. if (numEmployees + numHackers == 4)
  57. rowBoat();
  58. }
  59. long now = System.currentTimeMillis() - startMillis;
  60. System.out.printf("[+%04d] %s has left the river.%n", now, name);
  61. }
  62.  
  63. public CountDownLatch boardBoat() {
  64. seats.countDown();
  65. return seats;
  66. }
  67.  
  68. public void rowBoat() {
  69. long now = System.currentTimeMillis() - startMillis;
  70. System.out.printf("[+%04d] Toot toot.%n", now);
  71. numHackers = 0;
  72. numEmployees = 0;
  73. seats = new CountDownLatch(4);
  74. boatMonitor.notifyAll();
  75. }
  76.  
  77. public static void main(String[] args) throws InterruptedException {
  78. RiverCrossing rc = new RiverCrossing();
  79. Thread[] passengers = new Thread[40];
  80.  
  81. Function<Boolean, Runnable> threadMainFactory = (Boolean isHacker) -> () -> {
  82. try {
  83. String name = Thread.currentThread().getName();
  84. long now = System.currentTimeMillis() - rc.startMillis;
  85. System.out.printf("[+%04d] %s wants to board the boat.%n", now, name);
  86. Thread.sleep(ThreadLocalRandom.current().nextInt(1000));
  87.  
  88. if (isHacker)
  89. rc.hackerArrives(name);
  90. else
  91. rc.employeeArrives(name);
  92. } catch (InterruptedException ignored) {
  93. }
  94. };
  95.  
  96. for (int i = 0; i < 40; i ++) {
  97. boolean isHacker = ThreadLocalRandom.current().nextBoolean();
  98. passengers[i] = new Thread(threadMainFactory.apply(isHacker));
  99. passengers[i].setName(String.format("%s #%d", isHacker ? "Hacker" : "Employee", i));
  100. passengers[i].start();
  101. Thread.sleep(1000);
  102. }
  103.  
  104. for (int i = 0; i < 40; i ++) {
  105. passengers[i].join();
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement