Skip to content

Commit 6fe7789

Browse files
daveRanjanpivovarit
authored andcommitted
Guide to Guava | common.util.concurrent (eugenp#1528)
Code and Tests for common.util.concurrent package
1 parent 6e2dcfc commit 6fe7789

4 files changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.util.concurrent.AtomicLongMap;
4+
5+
public class AtomicLongMapTutorials {
6+
7+
private AtomicLongMap<String> atomicLongMap;
8+
9+
public AtomicLongMapTutorials(){
10+
atomicLongMap = AtomicLongMap.create();
11+
}
12+
13+
14+
public void addKeys(){
15+
atomicLongMap.addAndGet("apple",250);
16+
atomicLongMap.addAndGet("bat",350);
17+
atomicLongMap.addAndGet("cat",450);
18+
atomicLongMap.addAndGet("dog",550);
19+
}
20+
21+
public static void main(String[] args){
22+
AtomicLongMapTutorials atomicLongMapTutorials = new AtomicLongMapTutorials();
23+
atomicLongMapTutorials.addKeys();
24+
25+
System.out.println(atomicLongMapTutorials.atomicLongMap.get("2"));
26+
}
27+
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.guava.tutorial;
2+
3+
import com.google.common.util.concurrent.Monitor;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.function.BooleanSupplier;
8+
9+
public class MonitorExample {
10+
private List<String> students = new ArrayList<String>();
11+
private static final int MAX_SIZE = 100;
12+
13+
private Monitor monitor = new Monitor();
14+
15+
16+
public void addToCourse(String item) throws InterruptedException {
17+
Monitor.Guard studentsBelowCapacity = monitor.newGuard(this::isStudentsCapacityUptoLimit);
18+
monitor.enterWhen(studentsBelowCapacity);
19+
try {
20+
students.add(item);
21+
} finally {
22+
monitor.leave();
23+
}
24+
}
25+
26+
public Boolean isStudentsCapacityUptoLimit(){
27+
return students.size() > MAX_SIZE;
28+
}
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import com.google.common.util.concurrent.AtomicLongMap;
2+
import org.junit.Assert;
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class AtomicLongMapTests {
8+
9+
private static final String SPRING_COURSE_KEY = "Spring";
10+
private static final String HIBERNATE_COURSE_KEY = "hibernate";
11+
private static final String GUAVA_COURSE_KEY = "Guava";
12+
13+
AtomicLongMap<String> courses = AtomicLongMap.create();
14+
15+
public void setUp(){
16+
courses.put(SPRING_COURSE_KEY, 1056);
17+
courses.put(HIBERNATE_COURSE_KEY, 259);
18+
courses.put(GUAVA_COURSE_KEY, 78);
19+
}
20+
21+
22+
@Test
23+
public void accumulateAndGet_withLongBinaryOperator_thenSuccessful(){
24+
long noOfStudents = 56;
25+
long oldValue = courses.get(SPRING_COURSE_KEY);
26+
27+
long totalNotesRequired = courses.accumulateAndGet(
28+
"Guava",
29+
noOfStudents,
30+
(x,y) -> (x * y));
31+
32+
assertEquals(totalNotesRequired, oldValue * noOfStudents );
33+
}
34+
35+
@Test
36+
public void getAndAccumulate_withLongBinaryOperator_thenSuccessful(){
37+
long noOfStudents = 56;
38+
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
39+
40+
long onUpdate = courses.accumulateAndGet("Guava",
41+
noOfStudents,
42+
(x,y) -> (x * y)
43+
);
44+
45+
long afterUpdate = courses.get(SPRING_COURSE_KEY);
46+
47+
assertEquals(onUpdate, afterUpdate);
48+
assertEquals(afterUpdate, beforeUpdate * noOfStudents);
49+
}
50+
51+
@Test
52+
public void updateAndGet_withLongUnaryOperator_thenSuccessful(){
53+
long beforeUpdate = courses.get(SPRING_COURSE_KEY);
54+
55+
long onUpdate = courses.updateAndGet(
56+
"Guava",
57+
(x) -> (x/2));
58+
59+
long afterUpdate = courses.get(SPRING_COURSE_KEY);
60+
61+
assertEquals(onUpdate, afterUpdate);
62+
assertEquals(afterUpdate, beforeUpdate/2);
63+
}
64+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import com.google.common.util.concurrent.Monitor;
2+
import org.junit.Assert;
3+
import org.junit.Test;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.concurrent.atomic.AtomicReference;
8+
9+
import static com.google.common.util.concurrent.Uninterruptibles.joinUninterruptibly;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNull;
12+
13+
public class MonitorUnitTests {
14+
15+
@Test
16+
public void whenGaurdConditionIsTrue_IsSuccessful() {
17+
Monitor monitor = new Monitor();
18+
boolean enteredInCriticalSection = false;
19+
20+
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnTrue);
21+
22+
if(monitor.enterIf(gaurdCondition)){
23+
try{
24+
System.out.println("Entered in critical section");
25+
enteredInCriticalSection = true;
26+
}finally {
27+
monitor.leave();
28+
}
29+
}
30+
31+
Assert.assertTrue(enteredInCriticalSection);
32+
33+
}
34+
35+
@Test
36+
public void whenGaurdConditionIsFalse_IsSuccessful() {
37+
Monitor monitor = new Monitor();
38+
boolean enteredInCriticalSection = false;
39+
40+
Monitor.Guard gaurdCondition = monitor.newGuard(this::returnFalse);
41+
42+
if(monitor.enterIf(gaurdCondition)){
43+
try{
44+
System.out.println("Entered in critical section");
45+
enteredInCriticalSection = true;
46+
}finally {
47+
monitor.leave();
48+
}
49+
}
50+
51+
Assert.assertFalse(enteredInCriticalSection);
52+
}
53+
54+
private boolean returnTrue(){
55+
return true;
56+
}
57+
58+
private boolean returnFalse(){
59+
return false;
60+
}
61+
}

0 commit comments

Comments
 (0)