From cd0fa4154f32737b8f9776219d7a2799c98d3e12 Mon Sep 17 00:00:00 2001 From: Alexander Bachinger Date: Sun, 27 Apr 2025 18:37:07 +0200 Subject: [PATCH] UE19 finish --- src/UE19_220425_Threads/nothread/Konto.java | 23 ------ src/UE19_220425_Threads/nothread/Main.java | 43 +++++++++- .../nothread/Ueberweiser.java | 20 ----- .../reallyParallel/Main.java | 82 +++++++++++++++++++ src/UE19_220425_Threads/runnable/Konto.java | 23 ------ src/UE19_220425_Threads/runnable/Main.java | 55 ++++++++++++- .../runnable/Ueberweiser.java | 20 ----- src/UE19_220425_Threads/thread/Konto.java | 23 ------ src/UE19_220425_Threads/thread/Main.java | 43 +++++++++- .../thread/Ueberweiser.java | 20 ----- .../thread_right/Konto.java | 23 ------ .../thread_right/Main.java | 43 +++++++++- .../thread_right/Ueberweiser.java | 20 ----- 13 files changed, 259 insertions(+), 179 deletions(-) delete mode 100644 src/UE19_220425_Threads/nothread/Konto.java delete mode 100644 src/UE19_220425_Threads/nothread/Ueberweiser.java create mode 100644 src/UE19_220425_Threads/reallyParallel/Main.java delete mode 100644 src/UE19_220425_Threads/runnable/Konto.java delete mode 100644 src/UE19_220425_Threads/runnable/Ueberweiser.java delete mode 100644 src/UE19_220425_Threads/thread/Konto.java delete mode 100644 src/UE19_220425_Threads/thread/Ueberweiser.java delete mode 100644 src/UE19_220425_Threads/thread_right/Konto.java delete mode 100644 src/UE19_220425_Threads/thread_right/Ueberweiser.java diff --git a/src/UE19_220425_Threads/nothread/Konto.java b/src/UE19_220425_Threads/nothread/Konto.java deleted file mode 100644 index 0d25318..0000000 --- a/src/UE19_220425_Threads/nothread/Konto.java +++ /dev/null @@ -1,23 +0,0 @@ -package UE19_220425_Threads.nothread; - -public class Konto { - private int kontostand; - - public Konto() { - this.kontostand = 0; - } - - public int getKontostand() { - return kontostand; - } - - public void setKontostand(int kontostand) { - this.kontostand = kontostand; - } - - public void add(int betrag) { - int wert = getKontostand(); - wert = wert + betrag; - setKontostand(wert); - } -} \ No newline at end of file diff --git a/src/UE19_220425_Threads/nothread/Main.java b/src/UE19_220425_Threads/nothread/Main.java index 9696065..c6b4848 100644 --- a/src/UE19_220425_Threads/nothread/Main.java +++ b/src/UE19_220425_Threads/nothread/Main.java @@ -20,4 +20,45 @@ public class Main { System.out.println("C: " + c.getKontostand()); } } -// 81ms \ No newline at end of file +// 81ms + +class Konto { + private int kontostand; + + public Konto() { + this.kontostand = 0; + } + + public int getKontostand() { + return kontostand; + } + + public void setKontostand(int kontostand) { + this.kontostand = kontostand; + } + + public void add(int betrag) { + int wert = getKontostand(); + wert = wert + betrag; + setKontostand(wert); + } +} + +class Ueberweiser { + private static final int anzahl = 10_000_000; + private static final int betrag = 10; + private final Konto von; + private final Konto nach; + + public Ueberweiser(Konto von, Konto nach) { + this.von = von; + this.nach = nach; + } + + public void run() { + for (int i = 0; i < anzahl; i++) { + von.add(-betrag); + nach.add(betrag); + } + } +} \ No newline at end of file diff --git a/src/UE19_220425_Threads/nothread/Ueberweiser.java b/src/UE19_220425_Threads/nothread/Ueberweiser.java deleted file mode 100644 index 2a0f2d3..0000000 --- a/src/UE19_220425_Threads/nothread/Ueberweiser.java +++ /dev/null @@ -1,20 +0,0 @@ -package UE19_220425_Threads.nothread; - -public class Ueberweiser { - private static final int anzahl = 10_000_000; - private static final int betrag = 10; - private final Konto von; - private final Konto nach; - - public Ueberweiser(Konto von, Konto nach) { - this.von = von; - this.nach = nach; - } - - public void run() { - for (int i = 0; i < anzahl; i++) { - von.add(-betrag); - nach.add(betrag); - } - } -} diff --git a/src/UE19_220425_Threads/reallyParallel/Main.java b/src/UE19_220425_Threads/reallyParallel/Main.java new file mode 100644 index 0000000..c5e1aec --- /dev/null +++ b/src/UE19_220425_Threads/reallyParallel/Main.java @@ -0,0 +1,82 @@ +package UE19_220425_Threads.reallyParallel; + +public class Main { + public static void main(String[] args) { + Konto a = new Konto(); + Konto b = new Konto(); + Konto c = new Konto(); + + Runnable rAB = new Ueberweiser(a, b); + Runnable rBC = new Ueberweiser(b, c); + Runnable rCA = new Ueberweiser(c, a); + Thread ab = new Thread(rAB); + Thread bc = new Thread(rBC); + Thread ca = new Thread(rCA); + + long start = System.currentTimeMillis(); + + ab.start(); + bc.start(); + ca.start(); + + try { + ab.join(); + bc.join(); + ca.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println(System.currentTimeMillis() - start + "ms"); + + System.out.println("A: " + a.getKontostand()); + System.out.println("B: " + b.getKontostand()); + System.out.println("C: " + c.getKontostand()); + } +} +// 1883ms + +class Konto { + private int kontostand; + + public Konto() { + this.kontostand = 0; + } + + public int getKontostand() { + return kontostand; + } + + public void setKontostand(int kontostand) { + this.kontostand = kontostand; + } + + public synchronized void add(int betrag) { + int wert = getKontostand(); + wert = wert + betrag; + setKontostand(wert); + } +} + +class Ueberweiser implements Runnable { + private static final int anzahl = 1000; + private static final int betrag = 10; + private final Konto von; + private final Konto nach; + + public Ueberweiser(Konto von, Konto nach) { + this.von = von; + this.nach = nach; + } + + public void run() { + for (int i = 0; i < anzahl; i++) { + von.add(-betrag); + nach.add(betrag); + try { + Thread.sleep(1); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/src/UE19_220425_Threads/runnable/Konto.java b/src/UE19_220425_Threads/runnable/Konto.java deleted file mode 100644 index a310a32..0000000 --- a/src/UE19_220425_Threads/runnable/Konto.java +++ /dev/null @@ -1,23 +0,0 @@ -package UE19_220425_Threads.runnable; - -public class Konto { - private int kontostand; - - public Konto() { - this.kontostand = 0; - } - - public int getKontostand() { - return kontostand; - } - - public void setKontostand(int kontostand) { - this.kontostand = kontostand; - } - - public synchronized void add(int betrag) { - int wert = getKontostand(); - wert = wert + betrag; - setKontostand(wert); - } -} \ No newline at end of file diff --git a/src/UE19_220425_Threads/runnable/Main.java b/src/UE19_220425_Threads/runnable/Main.java index 73f7e1b..8be1e9f 100644 --- a/src/UE19_220425_Threads/runnable/Main.java +++ b/src/UE19_220425_Threads/runnable/Main.java @@ -5,14 +5,20 @@ public class Main { Konto a = new Konto(); Konto b = new Konto(); Konto c = new Konto(); - Ueberweiser ab = new Ueberweiser(a, b); - Ueberweiser bc = new Ueberweiser(b, c); - Ueberweiser ca = new Ueberweiser(c, a); + + Runnable rAB = new Ueberweiser(a, b); + Runnable rBC = new Ueberweiser(b, c); + Runnable rCA = new Ueberweiser(c, a); + Thread ab = new Thread(rAB); + Thread bc = new Thread(rBC); + Thread ca = new Thread(rCA); long start = System.currentTimeMillis(); + ab.start(); bc.start(); ca.start(); + try { ab.join(); bc.join(); @@ -27,4 +33,45 @@ public class Main { System.out.println("C: " + c.getKontostand()); } } -// 3838ms \ No newline at end of file +// 3950ms + +class Konto { + private int kontostand; + + public Konto() { + this.kontostand = 0; + } + + public int getKontostand() { + return kontostand; + } + + public void setKontostand(int kontostand) { + this.kontostand = kontostand; + } + + public synchronized void add(int betrag) { + int wert = getKontostand(); + wert = wert + betrag; + setKontostand(wert); + } +} + +class Ueberweiser implements Runnable { + private static final int anzahl = 10_000_000; + private static final int betrag = 10; + private final Konto von; + private final Konto nach; + + public Ueberweiser(Konto von, Konto nach) { + this.von = von; + this.nach = nach; + } + + public void run() { + for (int i = 0; i < anzahl; i++) { + von.add(-betrag); + nach.add(betrag); + } + } +} diff --git a/src/UE19_220425_Threads/runnable/Ueberweiser.java b/src/UE19_220425_Threads/runnable/Ueberweiser.java deleted file mode 100644 index 2674970..0000000 --- a/src/UE19_220425_Threads/runnable/Ueberweiser.java +++ /dev/null @@ -1,20 +0,0 @@ -package UE19_220425_Threads.runnable; - -public class Ueberweiser extends Thread { - private static final int anzahl = 10_000_000; - private static final int betrag = 10; - private final Konto von; - private final Konto nach; - - public Ueberweiser(Konto von, Konto nach) { - this.von = von; - this.nach = nach; - } - - public void run() { - for (int i = 0; i < anzahl; i++) { - von.add(-betrag); - nach.add(betrag); - } - } -} diff --git a/src/UE19_220425_Threads/thread/Konto.java b/src/UE19_220425_Threads/thread/Konto.java deleted file mode 100644 index e55e063..0000000 --- a/src/UE19_220425_Threads/thread/Konto.java +++ /dev/null @@ -1,23 +0,0 @@ -package UE19_220425_Threads.thread; - -public class Konto { - private int kontostand; - - public Konto() { - this.kontostand = 0; - } - - public int getKontostand() { - return kontostand; - } - - public void setKontostand(int kontostand) { - this.kontostand = kontostand; - } - - public void add(int betrag) { - int wert = getKontostand(); - wert = wert + betrag; - setKontostand(wert); - } -} \ No newline at end of file diff --git a/src/UE19_220425_Threads/thread/Main.java b/src/UE19_220425_Threads/thread/Main.java index 911a3b9..889b91c 100644 --- a/src/UE19_220425_Threads/thread/Main.java +++ b/src/UE19_220425_Threads/thread/Main.java @@ -27,4 +27,45 @@ public class Main { System.out.println("C: " + c.getKontostand()); } } -// 501ms \ No newline at end of file +// 501ms + +class Konto { + private int kontostand; + + public Konto() { + this.kontostand = 0; + } + + public int getKontostand() { + return kontostand; + } + + public void setKontostand(int kontostand) { + this.kontostand = kontostand; + } + + public void add(int betrag) { + int wert = getKontostand(); + wert = wert + betrag; + setKontostand(wert); + } +} + +class Ueberweiser extends Thread { + private static final int anzahl = 10_000_000; + private static final int betrag = 10; + private final Konto von; + private final Konto nach; + + public Ueberweiser(Konto von, Konto nach) { + this.von = von; + this.nach = nach; + } + + public void run() { + for (int i = 0; i < anzahl; i++) { + von.add(-betrag); + nach.add(betrag); + } + } +} \ No newline at end of file diff --git a/src/UE19_220425_Threads/thread/Ueberweiser.java b/src/UE19_220425_Threads/thread/Ueberweiser.java deleted file mode 100644 index c1c3628..0000000 --- a/src/UE19_220425_Threads/thread/Ueberweiser.java +++ /dev/null @@ -1,20 +0,0 @@ -package UE19_220425_Threads.thread; - -public class Ueberweiser extends Thread { - private static final int anzahl = 10_000_000; - private static final int betrag = 10; - private final Konto von; - private final Konto nach; - - public Ueberweiser(Konto von, Konto nach) { - this.von = von; - this.nach = nach; - } - - public void run() { - for (int i = 0; i < anzahl; i++) { - von.add(-betrag); - nach.add(betrag); - } - } -} diff --git a/src/UE19_220425_Threads/thread_right/Konto.java b/src/UE19_220425_Threads/thread_right/Konto.java deleted file mode 100644 index 2a73f4e..0000000 --- a/src/UE19_220425_Threads/thread_right/Konto.java +++ /dev/null @@ -1,23 +0,0 @@ -package UE19_220425_Threads.thread_right; - -public class Konto { - private int kontostand; - - public Konto() { - this.kontostand = 0; - } - - public int getKontostand() { - return kontostand; - } - - public void setKontostand(int kontostand) { - this.kontostand = kontostand; - } - - public synchronized void add(int betrag) { - int wert = getKontostand(); - wert = wert + betrag; - setKontostand(wert); - } -} \ No newline at end of file diff --git a/src/UE19_220425_Threads/thread_right/Main.java b/src/UE19_220425_Threads/thread_right/Main.java index 460b1fd..580a32b 100644 --- a/src/UE19_220425_Threads/thread_right/Main.java +++ b/src/UE19_220425_Threads/thread_right/Main.java @@ -27,4 +27,45 @@ public class Main { System.out.println("C: " + c.getKontostand()); } } -// 3838ms \ No newline at end of file +// 3838ms + +class Konto { + private int kontostand; + + public Konto() { + this.kontostand = 0; + } + + public int getKontostand() { + return kontostand; + } + + public void setKontostand(int kontostand) { + this.kontostand = kontostand; + } + + public synchronized void add(int betrag) { + int wert = getKontostand(); + wert = wert + betrag; + setKontostand(wert); + } +} + +class Ueberweiser extends Thread { + private static final int anzahl = 10_000_000; + private static final int betrag = 10; + private final Konto von; + private final Konto nach; + + public Ueberweiser(Konto von, Konto nach) { + this.von = von; + this.nach = nach; + } + + public void run() { + for (int i = 0; i < anzahl; i++) { + von.add(-betrag); + nach.add(betrag); + } + } +} \ No newline at end of file diff --git a/src/UE19_220425_Threads/thread_right/Ueberweiser.java b/src/UE19_220425_Threads/thread_right/Ueberweiser.java deleted file mode 100644 index 0ccc8a9..0000000 --- a/src/UE19_220425_Threads/thread_right/Ueberweiser.java +++ /dev/null @@ -1,20 +0,0 @@ -package UE19_220425_Threads.thread_right; - -public class Ueberweiser extends Thread { - private static final int anzahl = 10_000_000; - private static final int betrag = 10; - private final Konto von; - private final Konto nach; - - public Ueberweiser(Konto von, Konto nach) { - this.von = von; - this.nach = nach; - } - - public void run() { - for (int i = 0; i < anzahl; i++) { - von.add(-betrag); - nach.add(betrag); - } - } -}