From 31710c55ea1903d48468fe1dedb551e7ef2cd359 Mon Sep 17 00:00:00 2001 From: Sandro Leuchter Date: Wed, 16 Oct 2024 10:26:05 +0200 Subject: [PATCH] Kap. 4 --- src/pp.04.01-RunnableReturn/justfile | 12 ++++ src/pp.04.01-RunnableReturn/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/Expression.java | 6 ++ .../src/main/java/pp/Main.java | 9 +++ .../src/main/java/pp/RunnableWithResult.java | 28 +++++++++ src/pp.04.01-RunnableReturn_solution/justfile | 12 ++++ src/pp.04.01-RunnableReturn_solution/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/Expression.java | 6 ++ .../src/main/java/pp/Main.java | 35 +++++++++++ .../src/main/java/pp/RunnableWithResult.java | 28 +++++++++ .../src/main/java/pp/RunnableWithResult1.java | 31 ++++++++++ .../src/main/java/pp/RunnableWithResult2.java | 26 ++++++++ .../src/main/java/pp/RunnableWithResult3.java | 27 ++++++++ src/pp.04.02-Future/justfile | 12 ++++ src/pp.04.02-Future/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/Main.java | 13 ++++ src/pp.04.02-Future_solution/justfile | 12 ++++ src/pp.04.02-Future_solution/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/Main.java | 34 +++++++++++ src/pp.04.03-ThreadPoolSize/justfile | 12 ++++ src/pp.04.03-ThreadPoolSize/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/CachedRunner.java | 16 +++++ .../src/main/java/pp/FixedRunner.java | 17 ++++++ .../src/main/java/pp/Task.java | 17 ++++++ src/pp.04.03-ThreadPoolSize_solution/justfile | 20 ++++++ src/pp.04.03-ThreadPoolSize_solution/pom.xml | 61 +++++++++++++++++++ .../src/main/java/pp/CachedRunner.java | 16 +++++ .../src/main/java/pp/FixedRunner.java | 17 ++++++ .../src/main/java/pp/TaskInnerClass.java | 34 +++++++++++ .../src/main/java/pp/TaskLambda.java | 24 ++++++++ .../src/main/java/pp/TaskStaticClass.java | 37 +++++++++++ 31 files changed, 867 insertions(+) create mode 100644 src/pp.04.01-RunnableReturn/justfile create mode 100644 src/pp.04.01-RunnableReturn/pom.xml create mode 100644 src/pp.04.01-RunnableReturn/src/main/java/pp/Expression.java create mode 100644 src/pp.04.01-RunnableReturn/src/main/java/pp/Main.java create mode 100644 src/pp.04.01-RunnableReturn/src/main/java/pp/RunnableWithResult.java create mode 100644 src/pp.04.01-RunnableReturn_solution/justfile create mode 100644 src/pp.04.01-RunnableReturn_solution/pom.xml create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Expression.java create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Main.java create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult.java create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult1.java create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult2.java create mode 100644 src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult3.java create mode 100644 src/pp.04.02-Future/justfile create mode 100644 src/pp.04.02-Future/pom.xml create mode 100644 src/pp.04.02-Future/src/main/java/pp/Main.java create mode 100644 src/pp.04.02-Future_solution/justfile create mode 100644 src/pp.04.02-Future_solution/pom.xml create mode 100644 src/pp.04.02-Future_solution/src/main/java/pp/Main.java create mode 100644 src/pp.04.03-ThreadPoolSize/justfile create mode 100644 src/pp.04.03-ThreadPoolSize/pom.xml create mode 100644 src/pp.04.03-ThreadPoolSize/src/main/java/pp/CachedRunner.java create mode 100644 src/pp.04.03-ThreadPoolSize/src/main/java/pp/FixedRunner.java create mode 100644 src/pp.04.03-ThreadPoolSize/src/main/java/pp/Task.java create mode 100644 src/pp.04.03-ThreadPoolSize_solution/justfile create mode 100644 src/pp.04.03-ThreadPoolSize_solution/pom.xml create mode 100644 src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/CachedRunner.java create mode 100644 src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/FixedRunner.java create mode 100644 src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskInnerClass.java create mode 100644 src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskLambda.java create mode 100644 src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskStaticClass.java diff --git a/src/pp.04.01-RunnableReturn/justfile b/src/pp.04.01-RunnableReturn/justfile new file mode 100644 index 0000000..848084f --- /dev/null +++ b/src/pp.04.01-RunnableReturn/justfile @@ -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 diff --git a/src/pp.04.01-RunnableReturn/pom.xml b/src/pp.04.01-RunnableReturn/pom.xml new file mode 100644 index 0000000..2a010e3 --- /dev/null +++ b/src/pp.04.01-RunnableReturn/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.01-RunnableReturn + 1.0-SNAPSHOT + jar + + + pp.Main + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.01-RunnableReturn/src/main/java/pp/Expression.java b/src/pp.04.01-RunnableReturn/src/main/java/pp/Expression.java new file mode 100644 index 0000000..7cf3de2 --- /dev/null +++ b/src/pp.04.01-RunnableReturn/src/main/java/pp/Expression.java @@ -0,0 +1,6 @@ +package pp; + +@FunctionalInterface +public interface Expression { + public T eval(); +} diff --git a/src/pp.04.01-RunnableReturn/src/main/java/pp/Main.java b/src/pp.04.01-RunnableReturn/src/main/java/pp/Main.java new file mode 100644 index 0000000..c2873f8 --- /dev/null +++ b/src/pp.04.01-RunnableReturn/src/main/java/pp/Main.java @@ -0,0 +1,9 @@ +package pp; + +public class Main { + + public static void main(String... args) throws InterruptedException { + // hier programmieren + } + +} diff --git a/src/pp.04.01-RunnableReturn/src/main/java/pp/RunnableWithResult.java b/src/pp.04.01-RunnableReturn/src/main/java/pp/RunnableWithResult.java new file mode 100644 index 0000000..ec4d5d1 --- /dev/null +++ b/src/pp.04.01-RunnableReturn/src/main/java/pp/RunnableWithResult.java @@ -0,0 +1,28 @@ +package pp; + +public class RunnableWithResult implements Runnable { + private final Expression expr; + + public RunnableWithResult(Expression 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 expr() { + return expr; + } +} diff --git a/src/pp.04.01-RunnableReturn_solution/justfile b/src/pp.04.01-RunnableReturn_solution/justfile new file mode 100644 index 0000000..848084f --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/justfile @@ -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 diff --git a/src/pp.04.01-RunnableReturn_solution/pom.xml b/src/pp.04.01-RunnableReturn_solution/pom.xml new file mode 100644 index 0000000..267481c --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.01-RunnableReturn_solution + 1.0-SNAPSHOT + jar + + + pp.Main + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Expression.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Expression.java new file mode 100644 index 0000000..7cf3de2 --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Expression.java @@ -0,0 +1,6 @@ +package pp; + +@FunctionalInterface +public interface Expression { + public T eval(); +} diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Main.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Main.java new file mode 100644 index 0000000..24432cb --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/Main.java @@ -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()); + } +} diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult.java new file mode 100644 index 0000000..ec4d5d1 --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult.java @@ -0,0 +1,28 @@ +package pp; + +public class RunnableWithResult implements Runnable { + private final Expression expr; + + public RunnableWithResult(Expression 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 expr() { + return expr; + } +} diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult1.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult1.java new file mode 100644 index 0000000..61ced1e --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult1.java @@ -0,0 +1,31 @@ +package pp; + +public class RunnableWithResult1 extends RunnableWithResult { + private T result; + private boolean finished; + + public RunnableWithResult1(Expression 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; + } +} diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult2.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult2.java new file mode 100644 index 0000000..32d4190 --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult2.java @@ -0,0 +1,26 @@ +package pp; + +public class RunnableWithResult2 extends RunnableWithResult { + private T result; + private volatile Thread self; + + public RunnableWithResult2(Expression 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; + } +} diff --git a/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult3.java b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult3.java new file mode 100644 index 0000000..ebb440b --- /dev/null +++ b/src/pp.04.01-RunnableReturn_solution/src/main/java/pp/RunnableWithResult3.java @@ -0,0 +1,27 @@ +package pp; + +public class RunnableWithResult3 extends RunnableWithResult { + private T result; + private Thread executor; + + public RunnableWithResult3(Expression 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; + } +} diff --git a/src/pp.04.02-Future/justfile b/src/pp.04.02-Future/justfile new file mode 100644 index 0000000..848084f --- /dev/null +++ b/src/pp.04.02-Future/justfile @@ -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 diff --git a/src/pp.04.02-Future/pom.xml b/src/pp.04.02-Future/pom.xml new file mode 100644 index 0000000..a390e40 --- /dev/null +++ b/src/pp.04.02-Future/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.02-Future + 1.0-SNAPSHOT + jar + + + pp.Main + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.02-Future/src/main/java/pp/Main.java b/src/pp.04.02-Future/src/main/java/pp/Main.java new file mode 100644 index 0000000..429e70b --- /dev/null +++ b/src/pp.04.02-Future/src/main/java/pp/Main.java @@ -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(); + } + +} diff --git a/src/pp.04.02-Future_solution/justfile b/src/pp.04.02-Future_solution/justfile new file mode 100644 index 0000000..848084f --- /dev/null +++ b/src/pp.04.02-Future_solution/justfile @@ -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 diff --git a/src/pp.04.02-Future_solution/pom.xml b/src/pp.04.02-Future_solution/pom.xml new file mode 100644 index 0000000..1839240 --- /dev/null +++ b/src/pp.04.02-Future_solution/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.02-Future_solution + 1.0-SNAPSHOT + jar + + + pp.Main + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.02-Future_solution/src/main/java/pp/Main.java b/src/pp.04.02-Future_solution/src/main/java/pp/Main.java new file mode 100644 index 0000000..2174492 --- /dev/null +++ b/src/pp.04.02-Future_solution/src/main/java/pp/Main.java @@ -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() { + @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(); + } + } +} diff --git a/src/pp.04.03-ThreadPoolSize/justfile b/src/pp.04.03-ThreadPoolSize/justfile new file mode 100644 index 0000000..848084f --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize/justfile @@ -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 diff --git a/src/pp.04.03-ThreadPoolSize/pom.xml b/src/pp.04.03-ThreadPoolSize/pom.xml new file mode 100644 index 0000000..56792dd --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.03-ThreadPoolSize + 1.0-SNAPSHOT + jar + + + pp.Task + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.03-ThreadPoolSize/src/main/java/pp/CachedRunner.java b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/CachedRunner.java new file mode 100644 index 0000000..ee3b654 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/CachedRunner.java @@ -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; + } + +} diff --git a/src/pp.04.03-ThreadPoolSize/src/main/java/pp/FixedRunner.java b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/FixedRunner.java new file mode 100644 index 0000000..ed47c09 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/FixedRunner.java @@ -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; + } + +} diff --git a/src/pp.04.03-ThreadPoolSize/src/main/java/pp/Task.java b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/Task.java new file mode 100644 index 0000000..132370a --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize/src/main/java/pp/Task.java @@ -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); + } + +} diff --git a/src/pp.04.03-ThreadPoolSize_solution/justfile b/src/pp.04.03-ThreadPoolSize_solution/justfile new file mode 100644 index 0000000..edbee11 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/justfile @@ -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 diff --git a/src/pp.04.03-ThreadPoolSize_solution/pom.xml b/src/pp.04.03-ThreadPoolSize_solution/pom.xml new file mode 100644 index 0000000..a89caae --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.04.03-ThreadPoolSize_solution + 1.0-SNAPSHOT + jar + + + pp.TaskLambda + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/CachedRunner.java b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/CachedRunner.java new file mode 100644 index 0000000..ee3b654 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/CachedRunner.java @@ -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; + } + +} diff --git a/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/FixedRunner.java b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/FixedRunner.java new file mode 100644 index 0000000..ed47c09 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/FixedRunner.java @@ -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; + } + +} diff --git a/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskInnerClass.java b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskInnerClass.java new file mode 100644 index 0000000..3575380 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskInnerClass.java @@ -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); + } + +} diff --git a/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskLambda.java b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskLambda.java new file mode 100644 index 0000000..8420014 --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskLambda.java @@ -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); + } +} diff --git a/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskStaticClass.java b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskStaticClass.java new file mode 100644 index 0000000..3423b7a --- /dev/null +++ b/src/pp.04.03-ThreadPoolSize_solution/src/main/java/pp/TaskStaticClass.java @@ -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); + } + +}