Merge branch 'eugenp:master' into master

This commit is contained in:
Ulisses Lima
2022-08-08 13:34:28 -03:00
committed by GitHub
247 changed files with 4059 additions and 524 deletions
+1 -1
View File
@@ -5,4 +5,4 @@
- [Introduction to HexFormat in Java 17](https://www.baeldung.com/java-hexformat)
- [New Features in Java 17](https://www.baeldung.com/java-17-new-features)
- [Random Number Generators in Java 17](https://www.baeldung.com/java-17-random-number-generators)
- [Sealed Classes and Interfaces in Java 17](https://www.baeldung.com/java-sealed-classes-interfaces)
- [Sealed Classes and Interfaces in Java](https://www.baeldung.com/java-sealed-classes-interfaces)
@@ -0,0 +1,48 @@
package com.baeldung.list.aslistvssingletonlist;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
class ArraysAsListVsSingletonListUnitTest {
@Test
void givenListFromArraysAsList_whenChangingStructureAndElement_thenGetExpectedResult() {
List<String> arraysAsList = Arrays.asList("ONE");
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
() -> arraysAsList.add("TWO")
);
arraysAsList.set(0, "A brand new string");
assertThat(arraysAsList.get(0)).isEqualTo("A brand new string");
}
@Test
void givenSingletonList_whenChangingStructureAndElement_thenThrowException() {
List<String> singletonList = Collections.singletonList("ONE");
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
() -> singletonList.add("TWO")
);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
() -> singletonList.set(0, "A brand new string")
);
}
@Test
void givenAnArray_whengetListByArraysAsList_thenTheListIsBackedByTheArray() {
String[] theArray = new String[] { "ONE", "TWO" };
List<String> theList = Arrays.asList(theArray);
//changing the list, the array is changed too
theList.set(0, "ONE [changed in list]");
assertThat(theArray[0]).isEqualTo("ONE [changed in list]");
//changing the array, the list is changed too
theArray[1] = "TWO [changed in array]";
assertThat(theList.get(1)).isEqualTo("TWO [changed in array]");
}
}
@@ -0,0 +1,21 @@
package com.baeldung.list.replace;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class ReplaceUsingIndexInArrayListUnitTest {
private static final List<Integer> EXPECTED = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
@Test void givenArrayList_updateUsingSet() {
List<Integer> aList = new ArrayList<>(Arrays.asList(1, 2, 7, 4, 5));
aList.set(2, 3);
assertThat(aList).isEqualTo(EXPECTED);
}
}
@@ -11,7 +11,6 @@ This module contains articles about advanced topics about multithreading with co
- [Java CyclicBarrier vs CountDownLatch](https://www.baeldung.com/java-cyclicbarrier-countdownlatch)
- [Guide to the Fork/Join Framework in Java](https://www.baeldung.com/java-fork-join)
- [Guide to ThreadLocalRandom in Java](https://www.baeldung.com/java-thread-local-random)
- [The Thread.join() Method in Java](https://www.baeldung.com/java-thread-join)
- [Passing Parameters to Java Threads](https://www.baeldung.com/java-thread-parameters)
[[<-- previous]](/core-java-modules/core-java-concurrency-advanced)[[next -->]](/core-java-modules/core-java-concurrency-advanced-3)
@@ -0,0 +1,16 @@
package com.baeldung.atomicvsvolatile;
import java.util.concurrent.atomic.AtomicInteger;
public class SafeAtomicCounter {
private final AtomicInteger counter = new AtomicInteger(0);
public int getValue() {
return counter.get();
}
public void increment() {
counter.incrementAndGet();
}
}
@@ -0,0 +1,14 @@
package com.baeldung.atomicvsvolatile;
public class UnsafeCounter {
private int counter;
public int getValue() {
return counter;
}
public void increment() {
counter++;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.atomicvsvolatile;
public class UnsafeVolatileCounter {
private volatile int counter;
public int getValue() {
return counter;
}
public void increment() {
counter++;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.atomicvsvolatile;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.junit.Test;
public class SafeAtomicCounterUnitTest {
private static final int INCREMENT_COUNTER = 1000;
private static final int TIMEOUT = 100;
private static final int POOL_SIZE = 3;
@Test
public void givenMultiThread_whenSafeAtomicCounterIncrement() throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(POOL_SIZE);
SafeAtomicCounter safeCounter = new SafeAtomicCounter();
IntStream.range(0, INCREMENT_COUNTER).forEach(count -> service.submit(safeCounter::increment));
service.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS);
assertEquals(INCREMENT_COUNTER, safeCounter.getValue());
}
}
@@ -12,5 +12,4 @@ This module contains articles about advanced topics about multithreading with co
- [Guide to the Java Phaser](https://www.baeldung.com/java-phaser)
- [An Introduction to Atomic Variables in Java](https://www.baeldung.com/java-atomic-variables)
- [CyclicBarrier in Java](https://www.baeldung.com/java-cyclic-barrier)
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
- More Articles: [[next -->]](/core-java-modules/core-java-concurrency-advanced-2)
@@ -5,10 +5,7 @@ This module contains articles about basic Java concurrency
### Relevant Articles:
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
- [wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
- [Difference Between Wait and Sleep in Java](https://www.baeldung.com/java-wait-and-sleep)
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
- [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference)
- [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe)
- [How to Stop Execution After a Certain Time in Java](https://www.baeldung.com/java-stop-execution-after-certain-time)
@@ -3,8 +3,6 @@
This module contains articles about basic Java concurrency
### Relevant Articles:
- [Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
- [Guide to java.util.concurrent.Future](https://www.baeldung.com/java-future)
- [Overview of the java.util.concurrent](https://www.baeldung.com/java-util-concurrent)
- [Implementing a Runnable vs Extending a Thread](https://www.baeldung.com/java-runnable-vs-extending-thread)
@@ -12,5 +10,4 @@ This module contains articles about basic Java concurrency
- [ExecutorService – Waiting for Threads to Finish](https://www.baeldung.com/java-executor-wait-for-threads)
- [Runnable vs. Callable in Java](https://www.baeldung.com/java-runnable-callable)
- [What is Thread-Safety and How to Achieve it?](https://www.baeldung.com/java-thread-safety)
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
- [[Next -->]](/core-java-modules/core-java-concurrency-basic-2)
@@ -0,0 +1,18 @@
### Mockito Articles that are also part of the e-book
This module contains articles about Java Concurrency that are also part of an Ebook.
## Relevant articles:
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
- [Thread's wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
- [The Thread.join() Method in Java](https://www.baeldung.com/java-thread-join)
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
- [Guide to the Volatile Keyword in Java](https://www.baeldung.com/java-volatile)
- [A Guide to the Java ExecutorService](https://www.baeldung.com/java-executor-service-tutorial)
- [Guide To CompletableFuture](https://www.baeldung.com/java-completablefuture)
### NOTE:
Since this is a module tied to an e-book, it should **not** be moved or used to store the code for any further article.
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-concurrency-simple</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-concurrency-simple</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>java-concurrency-simple</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

Some files were not shown because too many files have changed in this diff Show More