task/JAVA-13630

# Conflicts:
#	persistence-modules/fauna/pom.xml
This commit is contained in:
Dhawal Kapil
2022-08-11 00:03:03 +05:30
180 changed files with 1596 additions and 152 deletions
@@ -6,4 +6,6 @@ This module contains articles about the Java List collection
- [Working With a List of Lists in Java](https://www.baeldung.com/java-list-of-lists)
- [Reverse an ArrayList in Java](https://www.baeldung.com/java-reverse-arraylist)
- [Sort a List Alphabetically in Java](https://www.baeldung.com/java-sort-list-alphabetically)
- [Arrays.asList() vs Collections.singletonList()](https://www.baeldung.com/java-aslist-vs-singletonlist)
- [Replace Element at a Specific Index in a Java ArrayList](https://www.baeldung.com/java-arraylist-replace-at-index)
- [[<-- Prev]](/core-java-modules/core-java-collections-list-3)
@@ -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]");
}
}
@@ -8,4 +8,5 @@
- [Java Map – keySet() vs. entrySet() vs. values() Methods](https://www.baeldung.com/java-map-entries-methods)
- [Java IdentityHashMap Class and Its Use Cases](https://www.baeldung.com/java-identityhashmap)
- [How to Invert a Map in Java](https://www.baeldung.com/java-invert-map)
- [Implementing a Map with Multiple Keys in Java](https://www.baeldung.com/java-multiple-keys-map)
- More articles: [[<-- prev]](../core-java-collections-maps-4)
@@ -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)
@@ -7,3 +7,4 @@
- [Producer-Consumer Problem With Example in Java](https://www.baeldung.com/java-producer-consumer-problem)
- [Acquire a Lock by a Key in Java](https://www.baeldung.com/java-acquire-lock-by-key)
- [Differences Between set() and lazySet() in Java Atomic Variables](https://www.baeldung.com/java-atomic-set-vs-lazyset)
- [Volatile vs. Atomic Variables in Java](https://www.baeldung.com/java-volatile-vs-atomic)
@@ -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