Merge remote-tracking branch 'upstream/master'

This commit is contained in:
anuragkumawat
2022-03-17 15:50:07 +05:30
144 changed files with 1602 additions and 382 deletions
+1 -1
View File
@@ -83,4 +83,4 @@
<olingo.version>2.0.11</olingo.version>
</properties>
</project>
</project>
@@ -34,6 +34,28 @@
</dependency>
</dependencies>
<build>
<finalName>core-java-9-new-features</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>https://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>incubator-features</id>
@@ -126,28 +148,6 @@
</profile>
</profiles>
<build>
<finalName>core-java-9-new-features</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>https://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<rxjava.version>3.0.0</rxjava.version>
<awaitility.version>4.0.2</awaitility.version>
@@ -5,3 +5,4 @@
- [Start Two Threads at the Exact Same Time in Java](https://www.baeldung.com/java-start-two-threads-at-same-time)
- [Volatile Variables and Thread Safety](https://www.baeldung.com/java-volatile-variables-thread-safety)
- [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)
@@ -0,0 +1,51 @@
package com.baeldung.producerconsumer;
public class Consumer implements Runnable {
private final DataQueue dataQueue;
private volatile boolean runFlag;
public Consumer(DataQueue dataQueue) {
this.dataQueue = dataQueue;
runFlag = true;
}
@Override
public void run() {
consume();
}
public void consume() {
while (runFlag) {
Message message;
if (dataQueue.isEmpty()) {
try {
dataQueue.waitOnEmpty();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
if (!runFlag) {
break;
}
message = dataQueue.remove();
dataQueue.notifyAllForFull();
useMessage(message);
}
System.out.println("Consumer Stopped");
}
private void useMessage(Message message) {
if (message != null) {
System.out.printf("[%s] Consuming Message. Id: %d, Data: %f\n", Thread.currentThread().getName(), message.getId(), message.getData());
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (message.getData() * 100));
}
}
public void stop() {
runFlag = false;
dataQueue.notifyAllForEmpty();
}
}
@@ -0,0 +1,59 @@
package com.baeldung.producerconsumer;
import java.util.LinkedList;
import java.util.Queue;
public class DataQueue {
private final Queue<Message> queue = new LinkedList<>();
private final int maxSize;
private final Object FULL_QUEUE = new Object();
private final Object EMPTY_QUEUE = new Object();
DataQueue(int maxSize) {
this.maxSize = maxSize;
}
public boolean isFull() {
return queue.size() == maxSize;
}
public boolean isEmpty() {
return queue.isEmpty();
}
public void waitOnFull() throws InterruptedException {
synchronized (FULL_QUEUE) {
FULL_QUEUE.wait();
}
}
public void waitOnEmpty() throws InterruptedException {
synchronized (EMPTY_QUEUE) {
EMPTY_QUEUE.wait();
}
}
public void notifyAllForFull() {
synchronized (FULL_QUEUE) {
FULL_QUEUE.notifyAll();
}
}
public void notifyAllForEmpty() {
synchronized (EMPTY_QUEUE) {
EMPTY_QUEUE.notifyAll();
}
}
public void add(Message message) {
synchronized (queue) {
queue.add(message);
}
}
public Message remove() {
synchronized (queue) {
return queue.poll();
}
}
}
@@ -0,0 +1,27 @@
package com.baeldung.producerconsumer;
public class Message {
private int id;
private double data;
public Message(int id, double data) {
this.id = id;
this.data = data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getData() {
return data;
}
public void setData(double data) {
this.data = data;
}
}
@@ -0,0 +1,53 @@
package com.baeldung.producerconsumer;
public class Producer implements Runnable {
private final DataQueue dataQueue;
private volatile boolean runFlag;
private static int idSequence = 0;
public Producer(DataQueue dataQueue) {
this.dataQueue = dataQueue;
runFlag = true;
}
@Override
public void run() {
produce();
}
public void produce() {
while (runFlag) {
Message message = generateMessage();
while (dataQueue.isFull()) {
try {
dataQueue.waitOnFull();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
if (!runFlag) {
break;
}
dataQueue.add(message);
dataQueue.notifyAllForEmpty();
}
System.out.println("Producer Stopped");
}
private Message generateMessage() {
Message message = new Message(++idSequence, Math.random());
System.out.printf("[%s] Generated Message. Id: %d, Data: %f\n", Thread.currentThread().getName(), message.getId(), message.getData());
//Sleeping on random time to make it realistic
ThreadUtil.sleep((long) (message.getData() * 100));
return message;
}
public void stop() {
runFlag = false;
dataQueue.notifyAllForFull();
}
}
@@ -0,0 +1,69 @@
package com.baeldung.producerconsumer;
import java.util.ArrayList;
import java.util.List;
import static com.baeldung.producerconsumer.ThreadUtil.*;
public class ProducerConsumerDemonstrator {
private static final int MAX_QUEUE_CAPACITY = 5;
public static void demoSingleProducerAndSingleConsumer() {
DataQueue dataQueue = new DataQueue(MAX_QUEUE_CAPACITY);
Producer producer = new Producer(dataQueue);
Thread producerThread = new Thread(producer);
Consumer consumer = new Consumer(dataQueue);
Thread consumerThread = new Thread(consumer);
producerThread.start();
consumerThread.start();
List<Thread> threads = new ArrayList<>();
threads.add(producerThread);
threads.add(consumerThread);
// let threads run for two seconds
sleep(2000);
// Stop threads
producer.stop();
consumer.stop();
waitForAllThreadsToComplete(threads);
}
public static void demoMultipleProducersAndMultipleConsumers() {
DataQueue dataQueue = new DataQueue(MAX_QUEUE_CAPACITY);
int producerCount = 3;
int consumerCount = 3;
List<Thread> threads = new ArrayList<>();
Producer producer = new Producer(dataQueue);
for(int i = 0; i < producerCount; i++) {
Thread producerThread = new Thread(producer);
producerThread.start();
threads.add(producerThread);
}
Consumer consumer = new Consumer(dataQueue);
for(int i = 0; i < consumerCount; i++) {
Thread consumerThread = new Thread(consumer);
consumerThread.start();
threads.add(consumerThread);
}
// let threads run for two seconds
sleep(2000);
// Stop threads
producer.stop();
consumer.stop();
waitForAllThreadsToComplete(threads);
}
public static void main(String[] args) {
demoSingleProducerAndSingleConsumer();
demoMultipleProducersAndMultipleConsumers();
}
}
@@ -0,0 +1,60 @@
package com.baeldung.producerconsumer;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import static com.baeldung.producerconsumer.ThreadUtil.sleep;
public class SimpleProducerConsumerDemonstrator {
BlockingQueue<Double> blockingQueue = new LinkedBlockingDeque<>(5);
private void produce() {
while (true) {
double value = generateValue();
try {
blockingQueue.put(value);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
System.out.printf("[%s] Value produced: %f\n", Thread.currentThread().getName(), value);
}
}
private void consume() {
while (true) {
Double value;
try {
value = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
// Consume value
System.out.printf("[%s] Value consumed: %f\n", Thread.currentThread().getName(), value);
}
}
private double generateValue() {
return Math.random();
}
private void runProducerConsumer() {
for (int i = 0; i < 2; i++) {
Thread producerThread = new Thread(this::produce);
producerThread.start();
}
for (int i = 0; i < 3; i++) {
Thread consumerThread = new Thread(this::consume);
consumerThread.start();
}
}
public static void main(String[] args) {
SimpleProducerConsumerDemonstrator simpleProducerConsumerDemonstrator = new SimpleProducerConsumerDemonstrator();
simpleProducerConsumerDemonstrator.runProducerConsumer();
sleep(2000);
System.exit(0);
}
}
@@ -0,0 +1,24 @@
package com.baeldung.producerconsumer;
import java.util.List;
public class ThreadUtil {
public static void waitForAllThreadsToComplete(List<Thread> threads) {
for(Thread thread: threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void sleep(long interval) {
try {
// Wait for some time to demonstrate threads
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@@ -24,4 +24,4 @@
</resources>
</build>
</project>
</project>
@@ -25,7 +25,6 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.darwinsys</groupId>
<artifactId>hirondelle-date4j</artifactId>
@@ -2,3 +2,4 @@
- [Java ArrayIndexOutOfBoundsException](https://www.baeldung.com/java-arrayindexoutofboundsexception)
- [Java Missing Return Statement](https://www.baeldung.com/java-missing-return-statement)
- [Convert long to int Type in Java](https://www.baeldung.com/java-convert-long-to-int)
+1 -1
View File
@@ -259,7 +259,7 @@
</plugins>
</build>
</profile>
</profiles>
<properties>
+8 -7
View File
@@ -64,13 +64,6 @@
</dependency>
</dependencies>
<properties>
<javaassist.version>3.27.0-GA</javaassist.version>
<sun.tools.version>1.8.0</sun.tools.version>
<jol-core.version>0.10</jol-core.version>
<asm.version>8.0.1</asm.version>
<bcel.version>6.5.0</bcel.version>
</properties>
<profiles>
<!-- java instrumentation profiles to build jars -->
<profile>
@@ -181,4 +174,12 @@
</profile>
</profiles>
<properties>
<javaassist.version>3.27.0-GA</javaassist.version>
<sun.tools.version>1.8.0</sun.tools.version>
<jol-core.version>0.10</jol-core.version>
<asm.version>8.0.1</asm.version>
<bcel.version>6.5.0</bcel.version>
</properties>
</project>
@@ -10,3 +10,4 @@ This module contains articles about core features in the Java language
- [Tiered Compilation in JVM](https://www.baeldung.com/jvm-tiered-compilation)
- [Fixing the “Declared package does not match the expected package” Error](https://www.baeldung.com/java-declared-expected-package-error)
- [Chaining Constructors in Java](https://www.baeldung.com/java-chain-constructors)
- [Difference Between POJO, JavaBeans, DTO and VO](https://www.baeldung.com/java-pojo-javabeans-dto-vo)
@@ -0,0 +1,48 @@
package com.baeldung.employee;
import java.io.Serializable;
import java.time.LocalDate;
public class EmployeeBean implements Serializable {
private static final long serialVersionUID = -3760445487636086034L;
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeeBean() {
}
public EmployeeBean(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.employee;
import java.time.LocalDate;
public class EmployeeDTO {
private String firstName;
private String lastName;
private LocalDate startDate;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
}
@@ -0,0 +1,49 @@
package com.baeldung.employee;
import java.time.LocalDate;
import java.util.Objects;
public class EmployeePOJO {
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeePOJO(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String name() {
return this.firstName + " " + this.lastName;
}
public LocalDate getStart() {
return this.startDate;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
}
@@ -0,0 +1,40 @@
package com.baeldung.employee;
import java.time.LocalDate;
import java.util.Objects;
public class EmployeeVO {
private String firstName;
private String lastName;
private LocalDate startDate;
public EmployeeVO(String firstName, String lastName, LocalDate startDate) {
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public LocalDate getStartDate() {
return startDate;
}
@Override
public boolean equals(Object obj) {
return Objects.equals(firstName, this.firstName)
&& Objects.equals(lastName, this.lastName)
&& Objects.equals(startDate, this.startDate);
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName, startDate);
}
}
@@ -64,7 +64,6 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -54,4 +54,5 @@
<validator.version>1.7</validator.version>
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
</properties>
</project>
-1
View File
@@ -69,7 +69,6 @@
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+1 -1
View File
@@ -137,4 +137,4 @@
</dependencies>
</dependencyManagement>
</project>
</project>
+1 -3
View File
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
<groupId>com.baeldung.dddmodules.infrastructure</groupId>
<artifactId>infrastructure</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
+1 -2
View File
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
+1 -3
View File
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
@@ -13,7 +12,6 @@
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<dependencies>
+2 -6
View File
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
@@ -14,7 +13,6 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modules>
@@ -70,10 +68,8 @@
<properties>
<source.version>9</source.version>
<target.version>9</target.version>
<compiler.plugin.version>3.8.1</compiler.plugin.version>
<appmodules.version>1.0</appmodules.version>
</properties>
</project>
</project>
+1 -3
View File
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
@@ -13,7 +12,6 @@
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<build>
+1 -3
View File
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
<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>
@@ -13,7 +12,6 @@
<groupId>com.baeldung.dddmodules</groupId>
<artifactId>ddd-modules</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<dependencies>

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