Compare commits

..

No commits in common. "0f5b0d8e126aa08028fdedae3b86622aa992533d" and "c7e293f3ecf9489c975a672f6eb600b852ee0611" have entirely different histories.

54 changed files with 1 additions and 1405 deletions

View File

@ -3,6 +3,7 @@ package pp;
public class Factory { public class Factory {
private static Type instance; private static Type instance;
private static Object lock = new Object();
public static Type getInstance() { public static Type getInstance() {
Type.prepare(); Type.prepare();

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Thread für ``Runnable`` mit Rückgabewert"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-01-RunnableWithResult.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-für-runnable-mit-rückgabewert)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile exec:java
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.01-RunnableReturn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.Main</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,6 +0,0 @@
package pp;
@FunctionalInterface
public interface Expression<T> {
public T eval();
}

View File

@ -1,9 +0,0 @@
package pp;
public class Main {
public static void main(String... args) throws InterruptedException {
// hier programmieren
}
}

View File

@ -1,28 +0,0 @@
package pp;
public class RunnableWithResult<T> implements Runnable {
private final Expression<T> expr;
public RunnableWithResult(Expression<T> expr) {
this.expr = expr;
}
@Override
public void run() {
// hier programmieren
}
public synchronized Boolean isAvailable() {
// hier programmieren
return null;
}
public synchronized T get() {
// hier programmieren
return null;
}
public Expression<T> expr() {
return expr;
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Thread für ``Runnable`` mit Rückgabewert"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-01-RunnableWithResult.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-für-runnable-mit-rückgabewert)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile exec:java
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.01-RunnableReturn_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.Main</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,6 +0,0 @@
package pp;
@FunctionalInterface
public interface Expression<T> {
public T eval();
}

View File

@ -1,35 +0,0 @@
package pp;
public class Main {
public static void main(String... args) throws InterruptedException {
var r1 = new RunnableWithResult2<>(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return 1 + 2;
});
var r2 = new RunnableWithResult2<>(() -> 3 + 4);
var r3 = new RunnableWithResult2<>(() -> {
while (!r1.isAvailable() || !r2.isAvailable()) {
System.out.println("waiting on r1 or r2");
}
return r1.get() + r2.get();
});
var thread1 = new Thread(r1);
var thread2 = new Thread(r2);
var thread3 = new Thread(r3);
thread1.start();
thread2.start();
thread3.start();
while (!r3.isAvailable()) {
System.out.println("waiting on r3");
}
System.out.println("result: " + r3.get());
}
}

View File

@ -1,28 +0,0 @@
package pp;
public class RunnableWithResult<T> implements Runnable {
private final Expression<T> expr;
public RunnableWithResult(Expression<T> expr) {
this.expr = expr;
}
@Override
public void run() {
// hier programmieren
}
public synchronized Boolean isAvailable() {
// hier programmieren
return null;
}
public synchronized T get() {
// hier programmieren
return null;
}
public Expression<T> expr() {
return expr;
}
}

View File

@ -1,31 +0,0 @@
package pp;
public class RunnableWithResult1<T> extends RunnableWithResult<T> {
private T result;
private boolean finished;
public RunnableWithResult1(Expression<T> expr) {
super(expr);
}
@Override
public void run() {
synchronized (this) { // Memorybarrier
this.finished = false;
}
this.result = expr().eval();
synchronized (this) {
this.finished = true;
} // Memorybarrier
}
@Override
public synchronized Boolean isAvailable() {
return this.finished;
}
@Override
public synchronized T get() {
return this.result;
}
}

View File

@ -1,26 +0,0 @@
package pp;
public class RunnableWithResult2<T> extends RunnableWithResult<T> {
private T result;
private volatile Thread self;
public RunnableWithResult2(Expression<T> expr) {
super(expr);
}
@Override
public void run() {
this.self = Thread.currentThread();
this.result = expr().eval();
}
@Override
public synchronized Boolean isAvailable() {
return (this.self != null) && !this.self.isAlive();
}
@Override
public synchronized T get() {
return this.result;
}
}

View File

@ -1,27 +0,0 @@
package pp;
public class RunnableWithResult3<T> extends RunnableWithResult<T> {
private T result;
private Thread executor;
public RunnableWithResult3(Expression<T> expr) {
super(expr);
this.executor = new Thread(this);
this.executor.start();
}
@Override
public void run() {
this.result = expr().eval();
}
@Override
public synchronized Boolean isAvailable() {
return (this.executor != null) && !this.executor.isAlive();
}
@Override
public synchronized T get() {
return this.result;
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Asynchrone Ausführung mit ``Callable`` und ``Future``"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-02-Future.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-asynchrone-ausführung-mit-callable-und-future)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile exec:java
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.02-Future</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.Main</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,13 +0,0 @@
package pp;
import java.util.concurrent.Executors;
public class Main {
public static void main(String... args) {
var executor = Executors.newCachedThreadPool();
// hier programmieren
executor.shutdown();
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Asynchrone Ausführung mit ``Callable`` und ``Future``"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-02-Future.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-asynchrone-ausführung-mit-callable-und-future)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile exec:java
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.02-Future_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.Main</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,34 +0,0 @@
package pp;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
public class Main {
public static void main(String... args) {
var executor = Executors.newCachedThreadPool();
// Lambda-Ausdruck, mehrere Statements, explizites return
var f1 = executor.submit(() -> {
return 1.0 + 2.0;
});
// Callable als Inner Class
var f2 = executor.submit(new Callable<Double>() {
@Override
public Double call() throws Exception {
return 3.0 + 4.0;
}
});
// Lambda-Ausdruck, knapp
var f3 = executor.submit(() -> f1.get() + f2.get());
try {
// get() blockiert, bis etwas vorliegt (auch oben)
System.out.println(f3.get());
} catch (InterruptedException | ExecutionException e) {
// Exceptions in f1 und f2 werden bis zum f3.get() verzögert
Thread.currentThread().interrupt();
} finally {
executor.shutdown();
}
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Thread Pools"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-03-ThreadPoolSize.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-pools)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile exec:java
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.03-ThreadPoolSize</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.Task</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,16 +0,0 @@
package pp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedRunner {
public static ExecutorService test(Runnable task, int tries) {
var pool = Executors.newCachedThreadPool();
for (var i = 1; i <= tries; i++) {
pool.execute(task);
}
return pool;
}
}

View File

@ -1,17 +0,0 @@
package pp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixedRunner {
private final static int SIZE = 6;
public static ExecutorService test(Runnable task, int tries) {
var pool = Executors.newFixedThreadPool(SIZE);
for (var i = 1; i <= tries; i++) {
pool.execute(task);
}
return pool;
}
}

View File

@ -1,17 +0,0 @@
package pp;
public class Task implements Runnable {
private static final int NUMBER_OF_TASKS = 1;
@Override
public void run() {
// TODO
}
public static void main(String... args) {
// var pool = CachedRunner.test(new Task(), NUMBER_OF_TASKS);
var pool = FixedRunner.test(new Task(), NUMBER_OF_TASKS);
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Thread Pools"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-03-ThreadPoolSize.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-pools)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)

View File

@ -1,20 +0,0 @@
default:
mvn clean compile exec:java
innerClass: (_exec "pp.TaskInnerClass" "")
lambda: (_exec "pp.TaskLambda" "")
staticClass: (_exec "pp.TaskStaticClass" "")
_exec class args:
mvn exec:java -Dexec.mainClass={{class}} -Dexec.args={{args}}
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,61 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.04.03-ThreadPoolSize_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<exec.mainClass>pp.TaskLambda</exec.mainClass>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,16 +0,0 @@
package pp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CachedRunner {
public static ExecutorService test(Runnable task, int tries) {
var pool = Executors.newCachedThreadPool();
for (var i = 1; i <= tries; i++) {
pool.execute(task);
}
return pool;
}
}

View File

@ -1,17 +0,0 @@
package pp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FixedRunner {
private final static int SIZE = 6;
public static ExecutorService test(Runnable task, int tries) {
var pool = Executors.newFixedThreadPool(SIZE);
for (var i = 1; i <= tries; i++) {
pool.execute(task);
}
return pool;
}
}

View File

@ -1,34 +0,0 @@
package pp;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TaskInnerClass {
private static final int NUMBER_OF_TASKS = 20;
public static void main(String... args) {
var pool1 = FixedRunner.test(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}, NUMBER_OF_TASKS);
var pool2 = CachedRunner.test(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}, NUMBER_OF_TASKS);
var scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(new Runnable() {
@Override
public void run() {
pool1.shutdown();
pool2.shutdown();
scheduler.shutdown();
}
}, 5, TimeUnit.SECONDS);
}
}

View File

@ -1,24 +0,0 @@
package pp;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TaskLambda {
private static final int NUMBER_OF_TASKS = 20;
public static void main(String... args) {
var pool1 = FixedRunner.test(
() -> System.out.println(Thread.currentThread().getName()),
NUMBER_OF_TASKS);
var pool2 = CachedRunner.test(
() -> System.out.println(Thread.currentThread().getName()),
NUMBER_OF_TASKS);
var scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(() -> {
pool1.shutdown();
pool2.shutdown();
scheduler.shutdown();
}, 5, TimeUnit.SECONDS);
}
}

View File

@ -1,37 +0,0 @@
package pp;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TaskStaticClass implements Runnable {
static class Shutdowner implements Runnable {
@Override
public void run() {
pool1.shutdown();
pool2.shutdown();
scheduler.shutdown();
}
}
private static final int NUMBER_OF_TASKS = 20;
private static ExecutorService pool1;
private static ExecutorService pool2;
private static ScheduledExecutorService scheduler;
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String... args) {
pool1 = FixedRunner.test(new TaskStaticClass(),
NUMBER_OF_TASKS);
pool2 = CachedRunner.test(new TaskStaticClass(),
NUMBER_OF_TASKS);
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(new Shutdowner(), 5, TimeUnit.SECONDS);
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Counter mit Atomics threadsicher machen"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-01-CounterAtomic.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-Solutions.html#laboraufgabe-counter-mit-atomics-threadsicher-machen)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/05-atomics.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/05-atomics.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile test
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,60 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.05.01-CounterAtomic</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,22 +0,0 @@
package pp;
class Counter {
private int c;
public Counter(int init) {
this.c = init;
}
public void inc() {
this.c++;
}
public void dec() {
this.c--;
}
public int get() {
return this.c;
}
}

View File

@ -1,19 +0,0 @@
package pp;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class CounterTest {
@Test
public void testInc() throws InterruptedException {
fail();
}
@Test
public void testDec() throws InterruptedException {
fail();
}
}

View File

@ -1,7 +0,0 @@
# Laboraufgabe "Counter mit Atomics threadsicher machen"
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-01-CounterAtomic.html)
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-Solutions.html#laboraufgabe-counter-mit-atomics-threadsicher-machen)
# Materialien
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/05-atomics.html)
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/05-atomics.html)

View File

@ -1,12 +0,0 @@
default:
mvn clean compile test
exec args:
mvn exec:java -Dexec.args={{args}}
clean:
mvn clean
compile:
mvn compile
test:
mvn test
javadoc:
mvn javadoc:javadoc

View File

@ -1,60 +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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pp</groupId>
<artifactId>pp.05.01-CounterAtomic_solution</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>10</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- für Unit-Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency><!-- für Lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency><!-- für net.jcip Annotationen -->
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin><!-- für Unit-Tests [mvn test] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin><!-- [mvn compile] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin><!-- [mvn exec:java] -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin><!-- [mvn javadoc:javadoc] -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,32 +0,0 @@
package pp;
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter1 implements ICounter {
private final AtomicInteger c;
public AtomicCounter1(int init) {
this.c = new AtomicInteger(init);
}
@Override
public void inc() {
var temp = this.c.get();
while (!this.c.compareAndSet(temp, temp + 1)) {
temp = this.c.get();
}
}
@Override
public void dec() {
var temp = this.c.get();
while (!this.c.compareAndSet(temp, temp - 1)) {
temp = this.c.get();
}
}
@Override
public int get() {
return this.c.get();
}
}

View File

@ -1,26 +0,0 @@
package pp;
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter2 implements ICounter {
private final AtomicInteger c;
public AtomicCounter2(int init) {
this.c = new AtomicInteger(init);
}
@Override
public void inc() {
this.c.incrementAndGet();
}
@Override
public void dec() {
this.c.decrementAndGet();
}
@Override
public int get() {
return this.c.get();
}
}

View File

@ -1,38 +0,0 @@
package pp;
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter3 implements ICounter {
private final AtomicInteger c;
public AtomicCounter3(int init) {
this.c = new AtomicInteger(init);
}
@Override
public void inc() {
for (;;) {
var current = this.c.get();
var next = current + 1;
if (this.c.compareAndSet(current, next)) {
break;
}
}
}
@Override
public void dec() {
for (;;) {
var current = this.c.get();
var next = current - 1;
if (this.c.compareAndSet(current, next)) {
break;
}
}
}
@Override
public int get() {
return this.c.get();
}
}

View File

@ -1,26 +0,0 @@
package pp;
import java.util.concurrent.atomic.AtomicInteger;
class AtomicCounter4 implements ICounter {
private final AtomicInteger c;
public AtomicCounter4(int init) {
this.c = new AtomicInteger(init);
}
@Override
public void inc() {
this.c.updateAndGet(i -> i + 1);
}
@Override
public void dec() {
this.c.updateAndGet(i -> i - 1);
}
@Override
public int get() {
return this.c.get();
}
}

View File

@ -1,24 +0,0 @@
package pp;
class Counter implements ICounter {
private int c;
public Counter(int init) {
this.c = init;
}
@Override
public void inc() {
this.c++;
}
@Override
public void dec() {
this.c--;
}
@Override
public int get() {
return this.c;
}
}

View File

@ -1,11 +0,0 @@
package pp;
interface ICounter {
void inc();
void dec();
int get();
}

View File

@ -1,24 +0,0 @@
package pp;
class SynchronizedCounter implements ICounter {
private int c;
public SynchronizedCounter(int init) {
this.c = init;
}
@Override
public synchronized void inc() {
this.c++;
}
@Override
public synchronized void dec() {
this.c--;
}
@Override
public synchronized int get() {
return this.c;
}
}

View File

@ -1,116 +0,0 @@
package pp;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class CounterTest {
private final static int RUNS = 500000;
private static void testIncrement(ICounter counter) {
assertEquals(0, counter.get());
var t1 = new Thread(() -> {
for (var j = 0; j < RUNS; j++) {
counter.inc();
}
});
var t2 = new Thread(() -> {
for (var j = 0; j < RUNS; j++) {
counter.inc();
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
assertEquals(RUNS * 2, counter.get());
}
@Test
void testCounterIncrement() throws InterruptedException {
testIncrement(new Counter(0));
}
@Test
void testSynchronizedCounterIncrement() throws InterruptedException {
testIncrement(new SynchronizedCounter(0));
}
@Test
void testAtomicCounter1Increment() throws InterruptedException {
testIncrement(new AtomicCounter1(0));
}
@Test
void testAtomicCounter2Increment() throws InterruptedException {
testIncrement(new AtomicCounter2(0));
}
@Test
void testAtomicCounter3Increment() throws InterruptedException {
testIncrement(new AtomicCounter3(0));
}
@Test
void testAtomicCounter4Increment() throws InterruptedException {
testIncrement(new AtomicCounter4(0));
}
private static void testDecrement(ICounter counter) {
assertEquals(RUNS * 2, counter.get());
var t1 = new Thread(() -> {
for (var j = 0; j < RUNS; j++) {
counter.dec();
}
});
var t2 = new Thread(() -> {
for (var j = 0; j < RUNS; j++) {
counter.dec();
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
assertEquals(0, counter.get());
}
@Test
void testCounterDecrement() throws InterruptedException {
testDecrement(new Counter(RUNS * 2));
}
@Test
void testSynchronizedCounterDecrement() throws InterruptedException {
testDecrement(new SynchronizedCounter(RUNS * 2));
}
@Test
void testAtomicCounter1Decrement() throws InterruptedException {
testDecrement(new AtomicCounter1(RUNS * 2));
}
@Test
void testAtomicCounter2Decrement() throws InterruptedException {
testDecrement(new AtomicCounter2(RUNS * 2));
}
@Test
void testAtomicCounter3Decrement() throws InterruptedException {
testDecrement(new AtomicCounter3(RUNS * 2));
}
@Test
void testAtomicCounter4Decrement() throws InterruptedException {
testDecrement(new AtomicCounter4(RUNS * 2));
}
}