Guide to Guava | common.util.concurrent (#1528)
Code and Tests for common.util.concurrent package
This commit is contained in:
parent
6e2dcfcf59
commit
6fe778979a
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.AtomicLongMap;
|
||||
|
||||
public class AtomicLongMapTutorials {
|
||||
|
||||
private AtomicLongMap<String> atomicLongMap;
|
||||
|
||||
public AtomicLongMapTutorials(){
|
||||
atomicLongMap = AtomicLongMap.create();
|
||||
}
|
||||
|
||||
|
||||
public void addKeys(){
|
||||
atomicLongMap.addAndGet("apple",250);
|
||||
atomicLongMap.addAndGet("bat",350);
|
||||
atomicLongMap.addAndGet("cat",450);
|
||||
atomicLongMap.addAndGet("dog",550);
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
AtomicLongMapTutorials atomicLongMapTutorials = new AtomicLongMapTutorials();
|
||||
atomicLongMapTutorials.addKeys();
|
||||
|
||||
System.out.println(atomicLongMapTutorials.atomicLongMap.get("2"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.guava.tutorial;
|
||||
|
||||
import com.google.common.util.concurrent.Monitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
public class MonitorExample {
|
||||
private List<String> students = new ArrayList<String>();
|
||||
private static final int MAX_SIZE = 100;
|
||||
|
||||
private Monitor monitor = new Monitor();
|
||||
|
||||
|
||||
public void addToCourse(String item) throws InterruptedException {
|
||||
Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
|
||||
monitor.enterWhen(studentsBelowCapacity);
|
||||
try {
|
||||
students.add(item);
|
||||
} finally {
|
||||
monitor.leave();
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean isStudentsCapacityUptoLimit(){
|
||||
return students.size() > MAX_SIZE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
import com.google.common.util.concurrent.AtomicLongMap;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AtomicLongMapTests {
|
||||
|
||||
private static final String SPRING_COURSE_KEY = "Spring";
|
||||
private static final String HIBERNATE_COURSE_KEY = "hibernate";
|
||||
private static final String GUAVA_COURSE_KEY = "Guava";
|
||||
|
||||
AtomicLongMap<String> courses = AtomicLongMap.create();
|
||||
|
||||
public void setUp(){
|
||||
courses.put(SPRING_COURSE_KEY, 1056);
|
||||
courses.put(HIBERNATE_COURSE_KEY, 259);
|
||||
courses.put(GUAVA_COURSE_KEY, 78);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void accumulateAndGet_withLongBinaryOperator_thenSuccessful(){
|
||||
long noOfStudents = 56;
|
||||
long oldValue = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long totalNotesRequired = courses.accumulateAndGet(
|
||||
"Guava",
|
||||
noOfStudents,
|
||||
(x,y) -> (x * y));
|
||||
|
||||
assertEquals(totalNotesRequired, oldValue * noOfStudents );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndAccumulate_withLongBinaryOperator_thenSuccessful(){
|
||||
long noOfStudents = 56;
|
||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long onUpdate = courses.accumulateAndGet("Guava",
|
||||
noOfStudents,
|
||||
(x,y) -> (x * y)
|
||||
);
|
||||
|
||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
assertEquals(onUpdate, afterUpdate);
|
||||
assertEquals(afterUpdate, beforeUpdate * noOfStudents);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateAndGet_withLongUnaryOperator_thenSuccessful(){
|
||||
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
long onUpdate = courses.updateAndGet(
|
||||
"Guava",
|
||||
(x) -> (x/2));
|
||||
|
||||
long afterUpdate = courses.get(SPRING_COURSE_KEY);
|
||||
|
||||
assertEquals(onUpdate, afterUpdate);
|
||||
assertEquals(afterUpdate, beforeUpdate/2);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
import com.google.common.util.concurrent.Monitor;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static com.google.common.util.concurrent.Uninterruptibles.joinUninterruptibly;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class MonitorUnitTests {
|
||||
|
||||
@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);
|
||||
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
private boolean returnTrue(){
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean returnFalse(){
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue