com.google.common.util.concurrent.Monitor Java Examples

The following examples show how to use com.google.common.util.concurrent.Monitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MonitorUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenGaurdConditionIsTrue_IsSuccessful() {
    Monitor monitor = new Monitor();
    boolean enteredInCriticalSection = false;

    Monitor.Guard gaurdCondition = monitor.newGuard(this::returnTrue);

    if (monitor.enterIf(gaurdCondition)) {
        try {
            System.out.println("Entered in critical section");
            enteredInCriticalSection = true;
        } finally {
            monitor.leave();
        }
    }

    Assert.assertTrue(enteredInCriticalSection);

}
 
Example #2
Source File: MonitorUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenGaurdConditionIsFalse_IsSuccessful() {
    Monitor monitor = new Monitor();
    boolean enteredInCriticalSection = false;

    Monitor.Guard gaurdCondition = monitor.newGuard(this::returnFalse);

    if (monitor.enterIf(gaurdCondition)) {
        try {
            System.out.println("Entered in critical section");
            enteredInCriticalSection = true;
        } finally {
            monitor.leave();
        }
    }

    Assert.assertFalse(enteredInCriticalSection);
}
 
Example #3
Source File: MonitorExample.java    From tutorials with MIT License 5 votes vote down vote up
public void addToCourse(String item) throws InterruptedException {
    Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
    monitor.enterWhen(studentsBelowCapacity);
    try {
        students.add(item);
    } finally {
        monitor.leave();
    }
}