From 4225066f974f467bdf3dcb84de3f798d5e0fc8de Mon Sep 17 00:00:00 2001 From: Alexander Bachinger Date: Sun, 27 Apr 2025 18:15:58 +0200 Subject: [PATCH] UE19 --- .idea/inspectionProfiles/Project_Default.xml | 5 ++++ src/UE19_220425_Threads/thread/Konto.java | 23 ++++++++++++++ src/UE19_220425_Threads/thread/Main.java | 30 +++++++++++++++++++ .../thread/Ueberweiser.java | 20 +++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/UE19_220425_Threads/thread/Konto.java create mode 100644 src/UE19_220425_Threads/thread/Main.java create mode 100644 src/UE19_220425_Threads/thread/Ueberweiser.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index fb15d0f..74b9df4 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -4,6 +4,11 @@ + + + + + diff --git a/src/UE19_220425_Threads/thread/Konto.java b/src/UE19_220425_Threads/thread/Konto.java new file mode 100644 index 0000000..e55e063 --- /dev/null +++ b/src/UE19_220425_Threads/thread/Konto.java @@ -0,0 +1,23 @@ +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 new file mode 100644 index 0000000..2a901f0 --- /dev/null +++ b/src/UE19_220425_Threads/thread/Main.java @@ -0,0 +1,30 @@ +package UE19_220425_Threads.thread; + +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()); + } +} +// 501ms \ No newline at end of file diff --git a/src/UE19_220425_Threads/thread/Ueberweiser.java b/src/UE19_220425_Threads/thread/Ueberweiser.java new file mode 100644 index 0000000..c1c3628 --- /dev/null +++ b/src/UE19_220425_Threads/thread/Ueberweiser.java @@ -0,0 +1,20 @@ +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); + } + } +}