Commit 95a3dfa0 by Patryk Czarnik

Konto w wątkach - wypłacanie bez czekania

parent 0b9dea27
package p19_watki.konto.v0;
public class Wyplacanie {
public static void main(String[] args) {
Konto konto = new Konto(1, "Ala", 15_000);
System.out.println("początek main: " + konto);
Thread wplacacz = new Thread(() -> {
try {
while(true) {
Thread.sleep(10_000);
konto.wplata(10_000);
System.out.println("Po wpłacie jest " + konto.getSaldo());
}
} catch (InterruptedException e) {}
});
Thread wyplacacz = new Thread(() -> {
try {
while(true) {
try {
Thread.sleep(500);
konto.wyplata(700);
System.out.println("Po wypłacie jest " + konto.getSaldo());
} catch (BrakSrodkow e) {
System.err.println(e.getMessage());
}
}
} catch (InterruptedException e) {}
});
wplacacz.start();
wyplacacz.start();
System.out.println("wątki uruchomione");
}
}
package p19_watki.konto.v1;
public class Wyplacanie1 {
public static void main(String[] args) {
Konto konto = new Konto(1, "Ala", 15_000);
System.out.println("początek main: " + konto);
Thread wplacacz = new Thread(() -> {
try {
while(true) {
Thread.sleep(10_000);
konto.wplata(10_000);
System.out.println("Po wpłacie jest " + konto.getSaldo());
}
} catch (InterruptedException e) {}
});
Thread wyplacacz = new Thread(() -> {
try {
while(true) {
try {
Thread.sleep(500);
konto.wyplata(700);
System.out.println("Po wypłacie jest " + konto.getSaldo());
} catch (BrakSrodkow e) {
System.err.println(e.getMessage());
}
}
} catch (InterruptedException e) {}
});
wplacacz.start();
wyplacacz.start();
System.out.println("wątki uruchomione");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment