Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
737d2e1b62
|
@ -100,3 +100,4 @@
|
|||
- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe)
|
||||
- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.concurrent.phaser;
|
||||
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
class LongRunningAction implements Runnable {
|
||||
private String threadName;
|
||||
private Phaser ph;
|
||||
|
||||
LongRunningAction(String threadName, Phaser ph) {
|
||||
this.threadName = threadName;
|
||||
this.ph = ph;
|
||||
ph.register();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("This is phase " + ph.getPhase());
|
||||
System.out.println("Thread " + threadName + " before long running action");
|
||||
ph.arriveAndAwaitAdvance();
|
||||
try {
|
||||
Thread.sleep(20);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ph.arriveAndDeregister();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.concurrent.phaser;
|
||||
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class PhaserTest {
|
||||
|
||||
@Test
|
||||
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() {
|
||||
//given
|
||||
ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
Phaser ph = new Phaser(1);
|
||||
assertEquals(0, ph.getPhase());
|
||||
|
||||
//when
|
||||
executorService.submit(new LongRunningAction("thread-1", ph));
|
||||
executorService.submit(new LongRunningAction("thread-2", ph));
|
||||
executorService.submit(new LongRunningAction("thread-3", ph));
|
||||
|
||||
//then
|
||||
ph.arriveAndAwaitAdvance();
|
||||
assertEquals(1, ph.getPhase());
|
||||
|
||||
//and
|
||||
executorService.submit(new LongRunningAction("thread-4", ph));
|
||||
executorService.submit(new LongRunningAction("thread-5", ph));
|
||||
ph.arriveAndAwaitAdvance();
|
||||
assertEquals(2, ph.getPhase());
|
||||
|
||||
|
||||
ph.arriveAndDeregister();
|
||||
}
|
||||
}
|
|
@ -1,18 +1,21 @@
|
|||
package com.baeldung.java.doublebrace;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.collectingAndThen;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DoubleBraceTest {
|
||||
|
||||
@Test
|
||||
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
|
||||
final Set<String> countries = new HashSet<String>();
|
||||
final Set<String> countries = new HashSet<>();
|
||||
countries.add("India");
|
||||
countries.add("USSR");
|
||||
countries.add("USA");
|
||||
|
@ -34,7 +37,9 @@ public class DoubleBraceTest {
|
|||
|
||||
@Test
|
||||
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
|
||||
final Set<String> countries = Collections.unmodifiableSet(Stream.of("India", "USSR", "USA").collect(toSet()));
|
||||
Set<String> countries = Stream.of("India", "USSR", "USA")
|
||||
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
|
||||
|
||||
assertTrue(countries.contains("India"));
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,9 @@ import javax.annotation.security.RolesAllowed;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
import com.baeldung.autoconfiguration.MySQLAutoconfiguration;
|
||||
|
||||
@SpringBootApplication(exclude = MySQLAutoconfiguration.class)
|
||||
public class DynamicValidationApp {
|
||||
@RolesAllowed("*")
|
||||
public static void main(String[] args) {
|
||||
|
|
Loading…
Reference in New Issue