From e7df30b4e7c9dfb8087355acfb75f762ca510a69 Mon Sep 17 00:00:00 2001 From: Alexander Bachinger Date: Sun, 27 Apr 2025 18:22:50 +0200 Subject: [PATCH] UE19 runnable --- .idea/inspectionProfiles/Project_Default.xml | 2 +- src/UE19_220425_Threads/nothread/Main.java | 4 +-- src/UE19_220425_Threads/runnable/Konto.java | 23 ++++++++++++++ src/UE19_220425_Threads/runnable/Main.java | 30 +++++++++++++++++++ .../runnable/Ueberweiser.java | 20 +++++++++++++ src/UE19_220425_Threads/thread/Main.java | 4 +-- .../thread_right/Konto.java | 23 ++++++++++++++ .../thread_right/Main.java | 30 +++++++++++++++++++ .../thread_right/Ueberweiser.java | 20 +++++++++++++ 9 files changed, 151 insertions(+), 5 deletions(-) create mode 100644 src/UE19_220425_Threads/runnable/Konto.java create mode 100644 src/UE19_220425_Threads/runnable/Main.java create mode 100644 src/UE19_220425_Threads/runnable/Ueberweiser.java create mode 100644 src/UE19_220425_Threads/thread_right/Konto.java create mode 100644 src/UE19_220425_Threads/thread_right/Main.java create mode 100644 src/UE19_220425_Threads/thread_right/Ueberweiser.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 74b9df4..3bc5e45 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -6,7 +6,7 @@ - + diff --git a/src/UE19_220425_Threads/nothread/Main.java b/src/UE19_220425_Threads/nothread/Main.java index 3e83cbd..9696065 100644 --- a/src/UE19_220425_Threads/nothread/Main.java +++ b/src/UE19_220425_Threads/nothread/Main.java @@ -9,11 +9,11 @@ public class Main { Ueberweiser bc = new Ueberweiser(b, c); Ueberweiser ca = new Ueberweiser(c, a); -// long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); ab.run(); bc.run(); ca.run(); -// System.out.println(System.currentTimeMillis() - start + "ms"); + System.out.println(System.currentTimeMillis() - start + "ms"); System.out.println("A: " + a.getKontostand()); System.out.println("B: " + b.getKontostand()); diff --git a/src/UE19_220425_Threads/runnable/Konto.java b/src/UE19_220425_Threads/runnable/Konto.java new file mode 100644 index 0000000..a310a32 --- /dev/null +++ b/src/UE19_220425_Threads/runnable/Konto.java @@ -0,0 +1,23 @@ +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 new file mode 100644 index 0000000..73f7e1b --- /dev/null +++ b/src/UE19_220425_Threads/runnable/Main.java @@ -0,0 +1,30 @@ +package UE19_220425_Threads.runnable; + +public class Main { + public static void main(String[] args) { + 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); + + 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()); + } +} +// 3838ms \ No newline at end of file diff --git a/src/UE19_220425_Threads/runnable/Ueberweiser.java b/src/UE19_220425_Threads/runnable/Ueberweiser.java new file mode 100644 index 0000000..2674970 --- /dev/null +++ b/src/UE19_220425_Threads/runnable/Ueberweiser.java @@ -0,0 +1,20 @@ +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/Main.java b/src/UE19_220425_Threads/thread/Main.java index 2a901f0..911a3b9 100644 --- a/src/UE19_220425_Threads/thread/Main.java +++ b/src/UE19_220425_Threads/thread/Main.java @@ -9,7 +9,7 @@ public class Main { Ueberweiser bc = new Ueberweiser(b, c); Ueberweiser ca = new Ueberweiser(c, a); -// long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); ab.start(); bc.start(); ca.start(); @@ -20,7 +20,7 @@ public class Main { } catch (InterruptedException e) { throw new RuntimeException(e); } -// System.out.println(System.currentTimeMillis() - start + "ms"); + System.out.println(System.currentTimeMillis() - start + "ms"); System.out.println("A: " + a.getKontostand()); System.out.println("B: " + b.getKontostand()); diff --git a/src/UE19_220425_Threads/thread_right/Konto.java b/src/UE19_220425_Threads/thread_right/Konto.java new file mode 100644 index 0000000..2a73f4e --- /dev/null +++ b/src/UE19_220425_Threads/thread_right/Konto.java @@ -0,0 +1,23 @@ +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 new file mode 100644 index 0000000..460b1fd --- /dev/null +++ b/src/UE19_220425_Threads/thread_right/Main.java @@ -0,0 +1,30 @@ +package UE19_220425_Threads.thread_right; + +public class Main { + public static void main(String[] args) { + 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); + + 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()); + } +} +// 3838ms \ 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 new file mode 100644 index 0000000..0ccc8a9 --- /dev/null +++ b/src/UE19_220425_Threads/thread_right/Ueberweiser.java @@ -0,0 +1,20 @@ +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); + } + } +}