Skip to content

Commit 53d22be

Browse files
alejandrogervasioKevinGilmore
authored andcommitted
BAEL-2287 - An Introduction to Synchronized Java Collections (eugenp#5385)
* Initial Commit * Add pom.xml * Update pom.xml * Update pom.xml * Update SynchronizedSortedMapUnitTest.java
1 parent 48b2316 commit 53d22be

8 files changed

Lines changed: 293 additions & 90 deletions

File tree

core-java-collections/pom.xml

Lines changed: 83 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,83 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
5-
<artifactId>core-java-collections</artifactId>
6-
<version>0.1.0-SNAPSHOT</version>
7-
<packaging>jar</packaging>
8-
<name>core-java-collections</name>
9-
10-
<parent>
11-
<groupId>com.baeldung</groupId>
12-
<artifactId>parent-java</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<relativePath>../parent-java</relativePath>
15-
</parent>
16-
17-
<dependencies>
18-
<dependency>
19-
<groupId>net.sourceforge.collections</groupId>
20-
<artifactId>collections-generic</artifactId>
21-
<version>${collections-generic.version}</version>
22-
</dependency>
23-
<dependency>
24-
<groupId>org.apache.commons</groupId>
25-
<artifactId>commons-collections4</artifactId>
26-
<version>${commons-collections4.version}</version>
27-
</dependency>
28-
<dependency>
29-
<groupId>com.jayway.awaitility</groupId>
30-
<artifactId>awaitility</artifactId>
31-
<version>${avaitility.version}</version>
32-
<scope>test</scope>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.apache.commons</groupId>
36-
<artifactId>commons-lang3</artifactId>
37-
<version>${commons-lang3.version}</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.eclipse.collections</groupId>
41-
<artifactId>eclipse-collections</artifactId>
42-
<version>${eclipse.collections.version}</version>
43-
</dependency>
44-
<dependency>
45-
<groupId>org.assertj</groupId>
46-
<artifactId>assertj-core</artifactId>
47-
<version>${assertj.version}</version>
48-
<scope>test</scope>
49-
</dependency>
50-
<dependency>
51-
<groupId>org.junit.platform</groupId>
52-
<artifactId>junit-platform-runner</artifactId>
53-
<version>${junit.platform.version}</version>
54-
<scope>test</scope>
55-
</dependency>
56-
<dependency>
57-
<groupId>org.openjdk.jmh</groupId>
58-
<artifactId>jmh-core</artifactId>
59-
<version>${openjdk.jmh.version}</version>
60-
</dependency>
61-
<dependency>
62-
<groupId>org.openjdk.jmh</groupId>
63-
<artifactId>jmh-generator-annprocess</artifactId>
64-
<version>${openjdk.jmh.version}</version>
65-
</dependency>
66-
<dependency>
67-
<groupId>org.apache.commons</groupId>
68-
<artifactId>commons-exec</artifactId>
69-
<version>1.3</version>
70-
</dependency>
71-
<dependency>
72-
<groupId>one.util</groupId>
73-
<artifactId>streamex</artifactId>
74-
<version>0.6.5</version>
75-
</dependency>
76-
77-
</dependencies>
78-
79-
80-
<properties>
81-
<openjdk.jmh.version>1.19</openjdk.jmh.version>
82-
<junit.platform.version>1.2.0</junit.platform.version>
83-
<commons-lang3.version>3.5</commons-lang3.version>
84-
<commons-collections4.version>4.1</commons-collections4.version>
85-
<collections-generic.version>4.01</collections-generic.version>
86-
<avaitility.version>1.7.0</avaitility.version>
87-
<assertj.version>3.6.1</assertj.version>
88-
<eclipse.collections.version>7.1.0</eclipse.collections.version>
89-
</properties>
90-
</project>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>core-java-collections</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>core-java-collections</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-java</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>net.sourceforge.collections</groupId>
20+
<artifactId>collections-generic</artifactId>
21+
<version>${collections-generic.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.commons</groupId>
25+
<artifactId>commons-collections4</artifactId>
26+
<version>${commons-collections4.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.jayway.awaitility</groupId>
30+
<artifactId>awaitility</artifactId>
31+
<version>${avaitility.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.apache.commons</groupId>
36+
<artifactId>commons-lang3</artifactId>
37+
<version>${commons-lang3.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.eclipse.collections</groupId>
41+
<artifactId>eclipse-collections</artifactId>
42+
<version>${eclipse.collections.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.assertj</groupId>
46+
<artifactId>assertj-core</artifactId>
47+
<version>${assertj.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.platform</groupId>
52+
<artifactId>junit-platform-runner</artifactId>
53+
<version>${junit.platform.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.openjdk.jmh</groupId>
58+
<artifactId>jmh-core</artifactId>
59+
<version>${openjdk.jmh.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.openjdk.jmh</groupId>
63+
<artifactId>jmh-generator-annprocess</artifactId>
64+
<version>${openjdk.jmh.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.apache.commons</groupId>
68+
<artifactId>commons-exec</artifactId>
69+
<version>1.3</version>
70+
</dependency>
71+
</dependencies>
72+
73+
<properties>
74+
<openjdk.jmh.version>1.19</openjdk.jmh.version>
75+
<junit.platform.version>1.2.0</junit.platform.version>
76+
<commons-lang3.version>3.5</commons-lang3.version>
77+
<commons-collections4.version>4.1</commons-collections4.version>
78+
<collections-generic.version>4.01</collections-generic.version>
79+
<avaitility.version>1.7.0</avaitility.version>
80+
<assertj.version>3.6.1</assertj.version>
81+
<eclipse.collections.version>7.1.0</eclipse.collections.version>
82+
</properties>
83+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.synchronizedcollections.application;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.logging.Logger;
7+
8+
public class Application {
9+
10+
private static final Logger LOGGER = Logger.getLogger(Application.class.getName());
11+
12+
public static void main(String[] args) throws InterruptedException {
13+
List<Integer> syncCollection = Collections.synchronizedList(Arrays.asList(1, 2, 3, 4, 5, 6));
14+
synchronized (syncCollection) {
15+
syncCollection.forEach((e) -> {LOGGER.info(e.toString());});
16+
}
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Collections;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import org.junit.Test;
9+
10+
public class SynchronizedCollectionUnitTest {
11+
12+
@Test
13+
public void givenSynchronizedCollection_whenTwoThreadsAddElements_thenCorrectCollectionSize() throws InterruptedException {
14+
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
15+
16+
Runnable listOperations = () -> {
17+
syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
18+
};
19+
Thread thread1 = new Thread(listOperations);
20+
Thread thread2 = new Thread(listOperations);
21+
thread1.start();
22+
thread2.start();
23+
thread1.join();
24+
thread2.join();
25+
26+
assertThat(syncCollection.size()).isEqualTo(12);
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import org.junit.Test;
8+
import static org.assertj.core.api.Assertions.*;
9+
10+
public class SynchronizedListUnitTest {
11+
12+
@Test
13+
public void givenSynchronizedList_whenTwoThreadsAddElements_thenCorrectListSize() throws InterruptedException {
14+
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
15+
16+
Runnable listOperations = () -> {
17+
syncList.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
18+
};
19+
Thread thread1 = new Thread(listOperations);
20+
Thread thread2 = new Thread(listOperations);
21+
thread1.start();
22+
thread2.start();
23+
thread1.join();
24+
thread2.join();
25+
26+
assertThat(syncList.size()).isEqualTo(12);
27+
}
28+
29+
@Test
30+
public void givenStringList_whenTwoThreadsIterateOnSynchronizedList_thenCorrectResult() throws InterruptedException {
31+
List<String> syncCollection = Collections.synchronizedList(Arrays.asList("a", "b", "c"));
32+
List<String> uppercasedCollection = new ArrayList<>();
33+
34+
Runnable listOperations = () -> {
35+
synchronized (syncCollection) {
36+
syncCollection.forEach((e) -> {
37+
uppercasedCollection.add(e.toUpperCase());
38+
});
39+
}
40+
};
41+
42+
Thread thread1 = new Thread(listOperations);
43+
Thread thread2 = new Thread(listOperations);
44+
thread1.start();
45+
thread2.start();
46+
thread1.join();
47+
thread2.join();
48+
49+
assertThat(uppercasedCollection.get(0)).isEqualTo("A");
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import org.junit.Test;
7+
import static org.assertj.core.api.Assertions.*;
8+
9+
public class SynchronizedMapUnitTest {
10+
11+
@Test
12+
public void givenSynchronizedMap_whenTwoThreadsAddElements_thenCorrectMapSize() throws InterruptedException {
13+
Map<Integer, String> syncMap = Collections.synchronizedMap(new HashMap<>());
14+
15+
Runnable mapOperations = () -> {
16+
syncMap.put(1, "one");
17+
syncMap.put(2, "two");
18+
syncMap.put(3, "three");
19+
20+
};
21+
Thread thread1 = new Thread(mapOperations);
22+
Thread thread2 = new Thread(mapOperations);
23+
thread1.start();
24+
thread2.start();
25+
thread1.join();
26+
thread2.join();
27+
28+
assertThat(syncMap.size()).isEqualTo(3);
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
import org.junit.Test;
8+
import static org.assertj.core.api.Assertions.*;
9+
10+
public class SynchronizedSetUnitTest {
11+
12+
@Test
13+
public void givenSynchronizedSet_whenTwoThreadsAddElements_thenCorrectSetSize() throws InterruptedException {
14+
Set<Integer> syncSet = Collections.synchronizedSet(new HashSet<>());
15+
16+
Runnable setOperations = () -> {syncSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
17+
Thread thread1 = new Thread(setOperations);
18+
Thread thread2 = new Thread(setOperations);
19+
thread1.start();
20+
thread2.start();
21+
thread1.join();
22+
thread2.join();
23+
24+
assertThat(syncSet.size()).isEqualTo(6);
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import org.junit.Test;
8+
9+
public class SynchronizedSortedMapUnitTest {
10+
11+
@Test
12+
public void givenSynchronizedSorteMap_whenTwoThreadsAddElements_thenCorrectSortedMapSize() throws InterruptedException {
13+
Map<Integer, String> syncSortedMap = Collections.synchronizedSortedMap(new TreeMap<>());
14+
15+
Runnable sortedMapOperations = () -> {
16+
syncSortedMap.put(1, "One");
17+
syncSortedMap.put(2, "Two");
18+
syncSortedMap.put(3, "Three");
19+
};
20+
Thread thread1 = new Thread(sortedMapOperations);
21+
Thread thread2 = new Thread(sortedMapOperations);
22+
thread1.start();
23+
thread2.start();
24+
thread1.join();
25+
thread2.join();
26+
27+
assertThat(syncSortedMap.size()).isEqualTo(3);
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.synchronizedcollections.test;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.SortedSet;
6+
import java.util.TreeSet;
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
import org.junit.Test;
9+
10+
public class SynchronizedSortedSetUnitTest {
11+
12+
@Test
13+
public void givenSynchronizedSortedSet_whenTwoThreadsAddElements_thenCorrectSortedSetSize() throws InterruptedException {
14+
SortedSet<Integer> syncSortedSet = Collections.synchronizedSortedSet(new TreeSet<>());
15+
16+
Runnable sortedSetOperations = () -> {syncSortedSet.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));};
17+
sortedSetOperations.run();
18+
sortedSetOperations.run();
19+
Thread thread1 = new Thread(sortedSetOperations);
20+
Thread thread2 = new Thread(sortedSetOperations);
21+
thread1.start();
22+
thread2.start();
23+
thread1.join();
24+
thread2.join();
25+
26+
assertThat(syncSortedSet.size()).isEqualTo(6);
27+
}
28+
}

0 commit comments

Comments
 (0)