UE19 runnable

This commit is contained in:
2025-04-27 18:22:50 +02:00
parent 4225066f97
commit e7df30b4e7
9 changed files with 151 additions and 5 deletions

View File

@@ -6,7 +6,7 @@
</inspection_tool> </inspection_tool>
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true"> <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages> <Languages>
<language minSize="49" name="Java" /> <language minSize="138" name="Java" />
</Languages> </Languages>
</inspection_tool> </inspection_tool>
<inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WARNING" enabled_by_default="true" /> <inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WARNING" enabled_by_default="true" />

View File

@@ -9,11 +9,11 @@ public class Main {
Ueberweiser bc = new Ueberweiser(b, c); Ueberweiser bc = new Ueberweiser(b, c);
Ueberweiser ca = new Ueberweiser(c, a); Ueberweiser ca = new Ueberweiser(c, a);
// long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ab.run(); ab.run();
bc.run(); bc.run();
ca.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("A: " + a.getKontostand());
System.out.println("B: " + b.getKontostand()); System.out.println("B: " + b.getKontostand());

View File

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

View File

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

View File

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

View File

@@ -9,7 +9,7 @@ public class Main {
Ueberweiser bc = new Ueberweiser(b, c); Ueberweiser bc = new Ueberweiser(b, c);
Ueberweiser ca = new Ueberweiser(c, a); Ueberweiser ca = new Ueberweiser(c, a);
// long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ab.start(); ab.start();
bc.start(); bc.start();
ca.start(); ca.start();
@@ -20,7 +20,7 @@ public class Main {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(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("A: " + a.getKontostand());
System.out.println("B: " + b.getKontostand()); System.out.println("B: " + b.getKontostand());

View File

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

View File

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

View File

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