This commit is contained in:
2025-04-27 18:15:58 +02:00
parent d9b7b19543
commit 4225066f97
4 changed files with 78 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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);
}
}
}