Commit 890a847c authored by Colin DAMON's avatar Colin DAMON

Java concurrence

parent ae0a6556
......@@ -28,3 +28,4 @@ include:
- local: "/fizz-buzz/.gitlab-ci.yml"
- local: "/leap-years/.gitlab-ci.yml"
- local: "/employee-report/.gitlab-ci.yml"
- local: "/java-concurrence/.gitlab-ci.yml"
......@@ -9,13 +9,26 @@ cache:
stages:
- build
.java:
.java-parent:
image: maven:3.6.3-jdk-14
stage: build
tags:
- docker
before_script:
- cd $PROJECT_FOLDER
.java-no-jacoco:
extends: .java-parent
script:
- mvn $MAVEN_CLI_OPTS clean package
artifacts:
reports:
junit: $PROJECT_FOLDER/target/surefire-reports/TEST-*.xml
name: "$PROJECT_FOLDER-report"
expire_in: 1 day
.java:
extends: .java-parent
script:
- mvn $MAVEN_CLI_OPTS clean package
- awk -F"," '{ branches += $4 + $5 + $6 + $7; covered += $5 + $7 } END { print covered, "/", branches, "branches covered"; print 100*covered/branches, "%covered" }' target/site/jacoco/jacoco.csv
......
package-java-concurrence:
variables:
PROJECT_FOLDER: "java-concurrence"
extends: .java-no-jacoco
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-concurrence/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>java-concurrence</artifactId>
<name>JavaConcurrence</name>
<developers>
<developer>
<email>arey@ippon.fr</email>
<name>Anthony REY</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Code && coffee : Concurrence en java
Code & coffee sur la concurrence en Java
- **Auteurs** : Anthony REY et Colin DAMON
- **Date** : 17/03/2021
- **Langage** : Java
- **Niveau** : Enervé
- **Replay** : TODO
package fr.craft.kata;
import static org.assertj.core.api.Assertions.*;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
public class ThreadingTest {
@Test
void shouldStopThread() throws InterruptedException {
StopableThread process = new StopableThread();
Thread thread = new Thread(process);
thread.start();
Thread.sleep(100);
process.stop();
Thread.sleep(200);
assertThat(thread.isAlive()).isFalse();
}
@Test
void shouldPlayWithThreads() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(5);
AtomicInteger counter = new AtomicInteger();
Set<Future<Integer>> results = new HashSet<>();
for (int i = 0; i < 100; i++) {
results.add(executor.submit(() -> counter.incrementAndGet()));
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.SECONDS);
assertThat(results).extracting(result -> result.get()).hasSize(100);
}
@Test
void shouldHaveThreadSafeCollection() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(5);
Collection<UUID> ids = Collections.newSetFromMap(new ConcurrentHashMap<>());
for (int i = 0; i < 100; i++) {
executor.submit(() -> ids.add(UUID.randomUUID()));
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.SECONDS);
assertThat(ids).hasSize(100);
}
@Test
void shouldHaveOldDates() throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(5);
Collection<Integer> days = Collections.newSetFromMap(new ConcurrentHashMap<>());
for (int day = 0; day < 31; day++) {
GregorianCalendar date = new GregorianCalendar(2021, 00, day);
executor.submit(
() -> {
SimpleDateFormat format = new SimpleDateFormat("dd");
return days.add(Integer.parseInt(format.format(date.getTime())));
}
);
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.SECONDS);
assertThat(days).hasSize(31);
}
private static class StopableThread implements Runnable {
private AtomicBoolean running = new AtomicBoolean(true);
@Override
public void run() {
while (running.get()) {}
}
public void stop() {
running.set(false);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment