Skip to content

Commit 737d2e1

Browse files
Merge remote-tracking branch 'origin/master'
2 parents aada595 + 1303533 commit 737d2e1

5 files changed

Lines changed: 80 additions & 4 deletions

File tree

‎core-java/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@
100100
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
101101
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
102102
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
103+
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.concurrent.phaser;
2+
3+
import java.util.concurrent.Phaser;
4+
5+
class LongRunningAction implements Runnable {
6+
private String threadName;
7+
private Phaser ph;
8+
9+
LongRunningAction(String threadName, Phaser ph) {
10+
this.threadName = threadName;
11+
this.ph = ph;
12+
ph.register();
13+
}
14+
15+
@Override
16+
public void run() {
17+
System.out.println("This is phase " + ph.getPhase());
18+
System.out.println("Thread " + threadName + " before long running action");
19+
ph.arriveAndAwaitAdvance();
20+
try {
21+
Thread.sleep(20);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
ph.arriveAndDeregister();
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.concurrent.phaser;
2+
3+
import org.junit.FixMethodOrder;
4+
import org.junit.Test;
5+
import org.junit.runners.MethodSorters;
6+
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.Phaser;
10+
11+
import static junit.framework.TestCase.assertEquals;
12+
13+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
14+
public class PhaserTest {
15+
16+
@Test
17+
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() {
18+
//given
19+
ExecutorService executorService = Executors.newCachedThreadPool();
20+
Phaser ph = new Phaser(1);
21+
assertEquals(0, ph.getPhase());
22+
23+
//when
24+
executorService.submit(new LongRunningAction("thread-1", ph));
25+
executorService.submit(new LongRunningAction("thread-2", ph));
26+
executorService.submit(new LongRunningAction("thread-3", ph));
27+
28+
//then
29+
ph.arriveAndAwaitAdvance();
30+
assertEquals(1, ph.getPhase());
31+
32+
//and
33+
executorService.submit(new LongRunningAction("thread-4", ph));
34+
executorService.submit(new LongRunningAction("thread-5", ph));
35+
ph.arriveAndAwaitAdvance();
36+
assertEquals(2, ph.getPhase());
37+
38+
39+
ph.arriveAndDeregister();
40+
}
41+
}

‎core-java/src/test/java/com/baeldung/java/doublebrace/DoubleBraceTest.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.baeldung.java.doublebrace;
22

3+
import org.junit.Test;
4+
35
import java.util.Collections;
46
import java.util.HashSet;
57
import java.util.Set;
68
import java.util.stream.Stream;
9+
10+
import static java.util.stream.Collectors.collectingAndThen;
711
import static java.util.stream.Collectors.toSet;
812
import static org.junit.Assert.assertTrue;
9-
import org.junit.Test;
1013

1114
public class DoubleBraceTest {
1215

1316
@Test
1417
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
15-
final Set<String> countries = new HashSet<String>();
18+
final Set<String> countries = new HashSet<>();
1619
countries.add("India");
1720
countries.add("USSR");
1821
countries.add("USA");
@@ -34,7 +37,9 @@ public void whenInitializeSetWithDoubleBraces_containsElements() {
3437

3538
@Test
3639
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
37-
final Set<String> countries = Collections.unmodifiableSet(Stream.of("India", "USSR", "USA").collect(toSet()));
40+
Set<String> countries = Stream.of("India", "USSR", "USA")
41+
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
42+
3843
assertTrue(countries.contains("India"));
3944
}
4045

‎spring-boot/src/main/java/com/baeldung/dynamicvalidation/DynamicValidationApp.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import org.springframework.boot.SpringApplication;
66
import org.springframework.boot.autoconfigure.SpringBootApplication;
77

8-
@SpringBootApplication
8+
import com.baeldung.autoconfiguration.MySQLAutoconfiguration;
9+
10+
@SpringBootApplication(exclude = MySQLAutoconfiguration.class)
911
public class DynamicValidationApp {
1012
@RolesAllowed("*")
1113
public static void main(String[] args) {

0 commit comments

Comments
 (0)