Skip to content

Commit 350765e

Browse files
author
Tomasz Lelek
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-614
2 parents ea7e3db + 09fc6d9 commit 350765e

205 files changed

Lines changed: 7442 additions & 651 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.java9.language.stream;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
import java.util.function.Function;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class CollectorImprovementTest {
12+
private static class Blog {
13+
private String authorName;
14+
private List<String> comments;
15+
16+
public Blog(String authorName) {
17+
this.authorName = authorName;
18+
this.comments = new LinkedList<String>();
19+
}
20+
21+
public String getAuthorName() {
22+
return this.authorName;
23+
}
24+
25+
public List<String> getComments() {
26+
return new LinkedList<String>(this.comments);
27+
}
28+
29+
public void addComment(String comment) {
30+
this.comments.add(comment);
31+
}
32+
}
33+
34+
@Test
35+
public void testFiltering() {
36+
List<Integer> numbers = List.of(1, 2, 3, 5, 5);
37+
38+
Map<Integer, Long> result = numbers.stream()
39+
.filter(val -> val > 3).collect(Collectors.groupingBy(
40+
Function.identity(), Collectors.counting()));
41+
42+
assertEquals(1, result.size());
43+
44+
result = numbers.stream().collect(Collectors.groupingBy(
45+
Function.identity(), Collectors.filtering(val -> val > 3,
46+
Collectors.counting())));
47+
48+
assertEquals(4, result.size());
49+
}
50+
51+
@Test
52+
public void testFlatMapping() {
53+
Blog blog1 = new CollectorImprovementTest.Blog("1");
54+
blog1.addComment("Nice");
55+
blog1.addComment("Very Nice");
56+
Blog blog2 = new CollectorImprovementTest.Blog("2");
57+
blog2.addComment("Disappointing");
58+
blog2.addComment("Ok");
59+
blog2.addComment("Could be better");
60+
List<Blog> blogs = List.of(blog1, blog2);
61+
62+
Map<String, List<List<String>>> authorComments1 =
63+
blogs.stream().collect(Collectors.groupingBy(
64+
Blog::getAuthorName, Collectors.mapping(
65+
Blog::getComments, Collectors.toList())));
66+
67+
assertEquals(2, authorComments1.size());
68+
assertEquals(2, authorComments1.get("1").get(0).size());
69+
assertEquals(3, authorComments1.get("2").get(0).size());
70+
71+
Map<String, List<String>> authorComments2 =
72+
blogs.stream().collect(Collectors.groupingBy(
73+
Blog::getAuthorName, Collectors.flatMapping(
74+
blog -> blog.getComments().stream(),
75+
Collectors.toList())));
76+
77+
assertEquals(2, authorComments2.size());
78+
assertEquals(2, authorComments2.get("1").size());
79+
assertEquals(3, authorComments2.get("2").size());
80+
}
81+
}

‎core-java/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding)
5353
- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size)
5454
- [The Basics of Java Generics](http://www.baeldung.com/java-generics)
55-
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
55+
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
56+
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)

‎core-java/pom.xml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@
6464
<version>${grep4j.version}</version>
6565
</dependency>
6666

67-
<dependency>
68-
<groupId>com.lmax</groupId>
69-
<artifactId>disruptor</artifactId>
70-
<version>${disruptor.version}</version>
71-
</dependency>
7267
<!-- web -->
7368

7469
<!-- marshalling -->
@@ -369,7 +364,6 @@
369364
<unix4j.version>0.4</unix4j.version>
370365
<grep4j.version>1.8.7</grep4j.version>
371366
<lombok.version>1.16.12</lombok.version>
372-
<disruptor.version>3.3.6</disruptor.version>
373367

374368
<!-- testing -->
375369
<org.hamcrest.version>1.3</org.hamcrest.version>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
public class BrokenWorker implements Runnable {
7+
private final List<String> outputScraper;
8+
private final CountDownLatch countDownLatch;
9+
10+
public BrokenWorker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
11+
this.outputScraper = outputScraper;
12+
this.countDownLatch = countDownLatch;
13+
}
14+
15+
@Override
16+
public void run() {
17+
if (true) {
18+
throw new RuntimeException("Oh dear");
19+
}
20+
countDownLatch.countDown();
21+
outputScraper.add("Counted down");
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
public class WaitingWorker implements Runnable {
7+
8+
private final List<String> outputScraper;
9+
private final CountDownLatch readyThreadCounter;
10+
private final CountDownLatch callingThreadBlocker;
11+
private final CountDownLatch completedThreadCounter;
12+
13+
public WaitingWorker(final List<String> outputScraper,
14+
final CountDownLatch readyThreadCounter,
15+
final CountDownLatch callingThreadBlocker,
16+
CountDownLatch completedThreadCounter) {
17+
18+
this.outputScraper = outputScraper;
19+
this.readyThreadCounter = readyThreadCounter;
20+
this.callingThreadBlocker = callingThreadBlocker;
21+
this.completedThreadCounter = completedThreadCounter;
22+
}
23+
24+
@Override
25+
public void run() {
26+
// Mark this thread as read / started
27+
readyThreadCounter.countDown();
28+
try {
29+
callingThreadBlocker.await();
30+
outputScraper.add("Counted down");
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
} finally {
34+
completedThreadCounter.countDown();
35+
}
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import java.util.List;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
public class Worker implements Runnable {
7+
private final List<String> outputScraper;
8+
private final CountDownLatch countDownLatch;
9+
10+
public Worker(final List<String> outputScraper, final CountDownLatch countDownLatch) {
11+
this.outputScraper = outputScraper;
12+
this.countDownLatch = countDownLatch;
13+
}
14+
15+
@Override
16+
public void run() {
17+
// Do some work
18+
System.out.println("Doing some logic");
19+
countDownLatch.countDown();
20+
outputScraper.add("Counted down");
21+
}
22+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.baeldung.concurrent.countdownlatch;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.stream.Stream;
11+
12+
import static java.util.stream.Collectors.toList;
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class CountdownLatchExampleTest {
16+
@Test
17+
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion() throws InterruptedException {
18+
// Given
19+
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
20+
CountDownLatch countDownLatch = new CountDownLatch(5);
21+
List<Thread> workers = Stream
22+
.generate(() -> new Thread(new Worker(outputScraper, countDownLatch)))
23+
.limit(5)
24+
.collect(toList());
25+
26+
// When
27+
workers.forEach(Thread::start);
28+
countDownLatch.await(); // Block until workers finish
29+
outputScraper.add("Latch released");
30+
31+
// Then
32+
outputScraper.forEach(Object::toString);
33+
assertThat(outputScraper)
34+
.containsExactly(
35+
"Counted down",
36+
"Counted down",
37+
"Counted down",
38+
"Counted down",
39+
"Counted down",
40+
"Latch released"
41+
);
42+
}
43+
44+
@Test
45+
public void whenFailingToParallelProcess_thenMainThreadShouldTimeout() throws InterruptedException {
46+
// Given
47+
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
48+
CountDownLatch countDownLatch = new CountDownLatch(5);
49+
List<Thread> workers = Stream
50+
.generate(() -> new Thread(new BrokenWorker(outputScraper, countDownLatch)))
51+
.limit(5)
52+
.collect(toList());
53+
54+
// When
55+
workers.forEach(Thread::start);
56+
final boolean result = countDownLatch.await(3L, TimeUnit.SECONDS);
57+
58+
// Then
59+
assertThat(result).isFalse();
60+
}
61+
62+
@Test
63+
public void whenDoingLotsOfThreadsInParallel_thenStartThemAtTheSameTime() throws InterruptedException {
64+
// Given
65+
List<String> outputScraper = Collections.synchronizedList(new ArrayList<>());
66+
CountDownLatch readyThreadCounter = new CountDownLatch(5);
67+
CountDownLatch callingThreadBlocker = new CountDownLatch(1);
68+
CountDownLatch completedThreadCounter = new CountDownLatch(5);
69+
List<Thread> workers = Stream
70+
.generate(() -> new Thread(new WaitingWorker(outputScraper, readyThreadCounter, callingThreadBlocker, completedThreadCounter)))
71+
.limit(5)
72+
.collect(toList());
73+
74+
// When
75+
workers.forEach(Thread::start);
76+
readyThreadCounter.await(); // Block until workers start
77+
outputScraper.add("Workers ready");
78+
callingThreadBlocker.countDown(); // Start workers
79+
completedThreadCounter.await(); // Block until workers finish
80+
outputScraper.add("Workers complete");
81+
82+
// Then
83+
outputScraper.forEach(Object::toString);
84+
assertThat(outputScraper)
85+
.containsExactly(
86+
"Workers ready",
87+
"Counted down",
88+
"Counted down",
89+
"Counted down",
90+
"Counted down",
91+
"Counted down",
92+
"Workers complete"
93+
);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)