Fester Thread Pool
Ein begrenzter Platform-Thread-Pool muss dimensioniert und beendet werden.
Vollständiger Legacy-Modern-Vergleich aller Java-Klassen, Tests, Maven-POMs und relevanten Ressourcen dieses Moduls. Jede Datei ist genau einmal einer fachlich passenden Vergleichsgruppe zugeordnet.
Ein begrenzter Platform-Thread-Pool muss dimensioniert und beendet werden.
Pro Task wird ein leichtgewichtiger Virtual Thread verwendet.
Die Ähnlichkeit ist eine technische Orientierung auf normalisiertem Quelltext. Sie ersetzt keine fachliche Bewertung.
| Vergleichsgruppe | Legacy-Dateien | Modern-Dateien | Legacy-Zeilen | Modern-Zeilen | Ähnlichkeit |
|---|---|---|---|---|---|
| Build-Konfiguration | 1 | 1 | 34 | 34 | 100% |
| Task Runner und Test | 2 | 2 | 36 | 35 | 38% |
Das Pair-POM bindet die Legacy- und Modern-Submodule zusammen und gehört deshalb ebenfalls zur vollständigen Codeabdeckung.
Java-Version und Testkonfiguration wird verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| pom.xml | pom.xml | Direktvergleich Semantische Ähnlichkeit: 100% |
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>16concurrency-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>concurrency-legacy</artifactId>
<packaging>jar</packaging>
<name>Concurrency Legacy</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project><?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.aydin.academy</groupId>
<artifactId>16concurrency-pair</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>concurrency-modern</artifactId>
<packaging>jar</packaging>
<name>Concurrency Modern</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>Platform-Thread-Pool wird mit Virtual Threads verglichen.
| Legacy-Dateien | Modern-Dateien | Einordnung |
|---|---|---|
| LegacyTaskRunner.java LegacyTaskRunnerTest.java | VirtualThreadTaskRunner.java VirtualThreadTaskRunnerTest.java | Direktvergleich Semantische Ähnlichkeit: 38% |
package at.aydin.lab.concurrency.legacy;
import java.util.*;
import java.util.concurrent.*;
// Design Pattern: Task Executor
// Zweck: Begrenzter Plattform-Thread-Pool führt blockierende Aufgaben aus.
public final class LegacyTaskRunner {
public List<String> run(List<Callable<String>> tasks) {
ExecutorService pool = Executors.newFixedThreadPool(4);
try {
List<String> result = new ArrayList<>();
for (Future<String> f : pool.invokeAll(tasks)) result.add(f.get());
return result;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(ex);
} catch (ExecutionException ex) {
throw new IllegalStateException(ex.getCause());
} finally {
pool.shutdown();
}
}
}package at.aydin.lab.concurrency.legacy;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class LegacyTaskRunnerTest {
@Test
void executesTasks() {
assertEquals(List.of("A", "B"), new LegacyTaskRunner().run(List.of(() -> "A", () -> "B")));
}
}package at.aydin.lab.concurrency.modern;
import java.util.*;
import java.util.concurrent.*;
// Design Pattern: Task Executor
// Zweck: Ein Virtual Thread pro blockierender Aufgabe vereinfacht skalierbare Nebenläufigkeit.
public final class VirtualThreadTaskRunner {
public List<String> run(List<Callable<String>> tasks) {
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<String>> futures = tasks.stream().map(executor::submit).toList();
List<String> result = new ArrayList<>(futures.size());
for (Future<String> future : futures) result.add(future.get());
return List.copyOf(result);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new IllegalStateException(ex);
} catch (ExecutionException ex) {
throw new IllegalStateException(ex.getCause());
}
}
}package at.aydin.lab.concurrency.modern;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class VirtualThreadTaskRunnerTest {
@Test
void usesVirtualThreads() {
var runner = new VirtualThreadTaskRunner();
assertEquals(List.of("A", "B"), runner.run(List.of(() -> Thread.currentThread().isVirtual() ? "A" : "X", () -> "B")));
}
}