From a053915cea531aa7014929382f7d53ab01790058 Mon Sep 17 00:00:00 2001 From: 2210892 <2210892@stud.hs-mannheim.de> Date: Wed, 23 Oct 2024 21:05:17 +0200 Subject: [PATCH] ue1 --- .classpath | 24 +++++++++++++++++++ .gitignore | 1 + .project | 17 +++++++++++++ .../src/main/java/pp/Starter.java | 2 +- .../src/main/java/pp/StarterInner.java | 24 +++++++++++++++++++ .../src/main/java/pp/StarterLambda.java | 20 ++++++++++++++++ .../src/main/java/pp/Runner.java | 12 ++++++++++ 7 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 src/pp.01.02-Runnable/src/main/java/pp/StarterInner.java create mode 100644 src/pp.01.02-Runnable/src/main/java/pp/StarterLambda.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f7c13f2 --- /dev/null +++ b/.classpath @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..24927a7 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + PP-Labs_WS-2024-2025 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/pp.01.02-Runnable/src/main/java/pp/Starter.java b/src/pp.01.02-Runnable/src/main/java/pp/Starter.java index 7df7525..2e3b988 100644 --- a/src/pp.01.02-Runnable/src/main/java/pp/Starter.java +++ b/src/pp.01.02-Runnable/src/main/java/pp/Starter.java @@ -6,7 +6,7 @@ public class Starter { public static void main(String... args) { for (var i = 0; i < Starter.WORKERS; i++) { - var t = new Thread(new MyWorker(), String.format("Worker-%03d", i)); + var t = new Thread(new MyWorkerCoop(), String.format("Worker-%03d", i)); t.start(); } } diff --git a/src/pp.01.02-Runnable/src/main/java/pp/StarterInner.java b/src/pp.01.02-Runnable/src/main/java/pp/StarterInner.java new file mode 100644 index 0000000..0fb0867 --- /dev/null +++ b/src/pp.01.02-Runnable/src/main/java/pp/StarterInner.java @@ -0,0 +1,24 @@ +package pp; + +public class StarterInner { + + static int WORKERS = 200; + + public static void main(String... args) { + for (var i = 0; i < Starter.WORKERS; i++) { + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + Thread t = Thread.currentThread(); + while (true) { + System.out.println(t.getName() + ": ID => " + t.threadId()); + } + } + }, String.format("Worker-%03d", i)); + + t.start(); + + } + } +} diff --git a/src/pp.01.02-Runnable/src/main/java/pp/StarterLambda.java b/src/pp.01.02-Runnable/src/main/java/pp/StarterLambda.java new file mode 100644 index 0000000..97b42e1 --- /dev/null +++ b/src/pp.01.02-Runnable/src/main/java/pp/StarterLambda.java @@ -0,0 +1,20 @@ +package pp; + +public class StarterLambda { + + static int WORKERS = 200; + + public static void main(String... args) { + for (var i = 0; i < WORKERS; i++) { + Thread t = new Thread(() -> { + // Verwenden von Thread.currentThread() als Lambda-Ausdruck + Thread t1 = Thread.currentThread(); + while (true) { + System.out.println(t1.getName() + ": ID => " + t1.threadId()); + } + }, String.format("Worker-%03d", i)); + + t.start(); + } + } +} diff --git a/src/pp.01.03-EndThread/src/main/java/pp/Runner.java b/src/pp.01.03-EndThread/src/main/java/pp/Runner.java index 7123a5e..273edbe 100644 --- a/src/pp.01.03-EndThread/src/main/java/pp/Runner.java +++ b/src/pp.01.03-EndThread/src/main/java/pp/Runner.java @@ -12,5 +12,17 @@ public class Runner { e.printStackTrace(System.err); }); thread.start(); + + Thread s = new Thread( () -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + System.out.println("Task wird gestoppt."); + task.stopRequest(); + }); + s.start(); } }