Kap. 4
parent
dff9d7719f
commit
31710c55ea
|
@ -0,0 +1,12 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,6 @@
|
|||
package pp;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Expression<T> {
|
||||
public T eval();
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package pp;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String... args) throws InterruptedException {
|
||||
// hier programmieren
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,6 @@
|
|||
package pp;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Expression<T> {
|
||||
public T eval();
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,13 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String... args) {
|
||||
var executor = Executors.newCachedThreadPool();
|
||||
// hier programmieren
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,34 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,16 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
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
|
|
@ -0,0 +1,61 @@
|
|||
<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>
|
|
@ -0,0 +1,16 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue