UE19 finish

This commit is contained in:
2025-04-27 18:37:07 +02:00
parent e7df30b4e7
commit cd0fa4154f
13 changed files with 259 additions and 179 deletions

View File

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

View File

@@ -20,4 +20,45 @@ public class Main {
System.out.println("C: " + c.getKontostand());
}
}
// 81ms
// 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);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -27,4 +27,45 @@ public class Main {
System.out.println("C: " + c.getKontostand());
}
}
// 501ms
// 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);
}
}
}

View File

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

View File

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

View File

@@ -27,4 +27,45 @@ public class Main {
System.out.println("C: " + c.getKontostand());
}
}
// 3838ms
// 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);
}
}
}

View File

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