UE19 finish
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -21,3 +21,44 @@ public class Main {
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
82
src/UE19_220425_Threads/reallyParallel/Main.java
Normal file
82
src/UE19_220425_Threads/reallyParallel/Main.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -28,3 +28,44 @@ public class Main {
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -28,3 +28,44 @@ public class Main {
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user