Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ThreadLocalRandom;
- import java.util.function.Function;
- public class RiverCrossing {
- private int numHackers = 0;
- private int numEmployees = 0;
- CountDownLatch seats = new CountDownLatch(4);
- long startMillis = System.currentTimeMillis();
- private Object boatMonitor = new Object();
- public void hackerArrives(String name) throws InterruptedException {
- CountDownLatch seat = null;
- synchronized (boatMonitor) {
- if (numEmployees + numHackers == 4 || numEmployees == 3 || (numHackers == 2 && numEmployees == 1))
- boatMonitor.wait();
- if (numEmployees + numHackers < 4 && numEmployees < 3 && (numHackers == 2 && numEmployees == 1)) {
- int oldValue = numHackers;
- numHackers = oldValue + 1;
- long now = System.currentTimeMillis() - startMillis;
- System.out.printf("[+%04d] %s has boarded the boat.%n", now, name);
- seat = boardBoat();
- }
- }
- seat.await();
- synchronized (boatMonitor) {
- if (numEmployees + numHackers == 4)
- rowBoat();
- }
- long now = System.currentTimeMillis() - startMillis;
- System.out.printf("[+%04d] %s has left the river.%n", now, name);
- }
- public void employeeArrives(String name) throws InterruptedException {
- CountDownLatch seat = null;
- synchronized (boatMonitor) {
- if (numEmployees + numHackers == 4 || numHackers == 3 || (numEmployees == 2 && numHackers == 1))
- boatMonitor.wait();
- if (numEmployees + numHackers < 4 && numHackers < 3 && (numEmployees == 2 && numHackers == 1)) {
- int oldValue = numEmployees;
- numEmployees = oldValue + 1;
- long now = System.currentTimeMillis() - startMillis;
- System.out.printf("[+%04d] %s has boarded the boat.%n", now, name);
- seat = boardBoat();
- }
- }
- seat.await();
- synchronized (boatMonitor) {
- if (numEmployees + numHackers == 4)
- rowBoat();
- }
- long now = System.currentTimeMillis() - startMillis;
- System.out.printf("[+%04d] %s has left the river.%n", now, name);
- }
- public CountDownLatch boardBoat() {
- seats.countDown();
- return seats;
- }
- public void rowBoat() {
- long now = System.currentTimeMillis() - startMillis;
- System.out.printf("[+%04d] Toot toot.%n", now);
- numHackers = 0;
- numEmployees = 0;
- seats = new CountDownLatch(4);
- boatMonitor.notifyAll();
- }
- public static void main(String[] args) throws InterruptedException {
- RiverCrossing rc = new RiverCrossing();
- Thread[] passengers = new Thread[40];
- Function<Boolean, Runnable> threadMainFactory = (Boolean isHacker) -> () -> {
- try {
- String name = Thread.currentThread().getName();
- long now = System.currentTimeMillis() - rc.startMillis;
- System.out.printf("[+%04d] %s wants to board the boat.%n", now, name);
- Thread.sleep(ThreadLocalRandom.current().nextInt(1000));
- if (isHacker)
- rc.hackerArrives(name);
- else
- rc.employeeArrives(name);
- } catch (InterruptedException ignored) {
- }
- };
- for (int i = 0; i < 40; i ++) {
- boolean isHacker = ThreadLocalRandom.current().nextBoolean();
- passengers[i] = new Thread(threadMainFactory.apply(isHacker));
- passengers[i].setName(String.format("%s #%d", isHacker ? "Hacker" : "Employee", i));
- passengers[i].start();
- Thread.sleep(1000);
- }
- for (int i = 0; i < 40; i ++) {
- passengers[i].join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement