UE19
This commit is contained in:
5
.idea/inspectionProfiles/Project_Default.xml
generated
5
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -4,6 +4,11 @@
|
|||||||
<inspection_tool class="CommentedOutCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
<inspection_tool class="CommentedOutCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
<option name="minLines" value="11" />
|
<option name="minLines" value="11" />
|
||||||
</inspection_tool>
|
</inspection_tool>
|
||||||
|
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<Languages>
|
||||||
|
<language minSize="49" name="Java" />
|
||||||
|
</Languages>
|
||||||
|
</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" />
|
||||||
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="false" level="INFORMATION" enabled_by_default="false" />
|
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="false" level="INFORMATION" enabled_by_default="false" />
|
||||||
</profile>
|
</profile>
|
||||||
|
|||||||
23
src/UE19_220425_Threads/thread/Konto.java
Normal file
23
src/UE19_220425_Threads/thread/Konto.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/UE19_220425_Threads/thread/Main.java
Normal file
30
src/UE19_220425_Threads/thread/Main.java
Normal 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
|
||||||
20
src/UE19_220425_Threads/thread/Ueberweiser.java
Normal file
20
src/UE19_220425_Threads/thread/Ueberweiser.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user