Commit a1de33df by Patryk Czarnik

Poprawki w trakcie omawiania wątków

parent f6470812
...@@ -19,4 +19,8 @@ public class Afrykarium3 { ...@@ -19,4 +19,8 @@ public class Afrykarium3 {
public void zwiedzającyWychodzi() { public void zwiedzającyWychodzi() {
miejsca.release(); // ++ akademicko: V miejsca.release(); // ++ akademicko: V
} }
public void grupaWychodzi(int ilu) {
miejsca.release(ilu);
}
} }
...@@ -28,12 +28,12 @@ class WyplacanieZOczekiwaniem { ...@@ -28,12 +28,12 @@ class WyplacanieZOczekiwaniem {
Thread wyplacacz = new Thread(new Runnable() { Thread wyplacacz = new Thread(new Runnable() {
public void run() { public void run() {
while (!koniec) { while (!koniec) {
konto.wyplataCzekaj(100);
System.out.println("wypłacacz: wypłaciłem 100, saldo = " + konto.getSaldo());
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
konto.wyplataCzekaj(100);
System.out.println("wypłacacz: wypłaciłem 100, saldo = " + konto.getSaldo());
} }
} }
}); });
...@@ -49,7 +49,8 @@ class WyplacanieZOczekiwaniem { ...@@ -49,7 +49,8 @@ class WyplacanieZOczekiwaniem {
} }
koniec = true; koniec = true;
wplacacz.interrupt();
wyplacacz.interrupt();
try { try {
wplacacz.join(); wplacacz.join();
wyplacacz.join(); wyplacacz.join();
......
...@@ -4,16 +4,16 @@ public class Watki1 { ...@@ -4,16 +4,16 @@ public class Watki1 {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Początek main"); System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1)); Thread th1 = new Thread(new WatekWypisujacy("A", 200, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1)); Thread th2 = new Thread(new WatekWypisujacy("B", 200, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1)); Thread th3 = new Thread(new WatekWypisujacy("C", 200, 1));
th1.start(); th1.start();
th2.start(); th2.start();
th3.start(); th3.start();
System.out.println("main: wątki uruchomione"); System.out.println("main: wątki uruchomione");
try { try {
Thread.sleep(50); Thread.sleep(90);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
System.out.println("main: poczekałem sobie"); System.out.println("main: poczekałem sobie");
......
...@@ -4,9 +4,9 @@ public class Watki2_Join { ...@@ -4,9 +4,9 @@ public class Watki2_Join {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Początek main"); System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1)); Thread th1 = new Thread(new WatekWypisujacy("A", 200, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1)); Thread th2 = new Thread(new WatekWypisujacy("B", 200, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1)); Thread th3 = new Thread(new WatekWypisujacy("C", 200, 1));
System.out.println("stan przed start: " + th1.getState()); System.out.println("stan przed start: " + th1.getState());
...@@ -15,7 +15,7 @@ public class Watki2_Join { ...@@ -15,7 +15,7 @@ public class Watki2_Join {
th3.start(); th3.start();
System.out.println("main: wątki uruchomione, mój nr " + Thread.currentThread().getId()); System.out.println("main: wątki uruchomione, mój nr " + Thread.currentThread().getId());
try { try {
Thread.sleep(50); Thread.sleep(90);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
System.out.println("main: Teraz będę czekał na wątki za pomocą join"); System.out.println("main: Teraz będę czekał na wątki za pomocą join");
......
...@@ -4,9 +4,9 @@ public class Watki3_Demon { ...@@ -4,9 +4,9 @@ public class Watki3_Demon {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Początek main"); System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1)); Thread th1 = new Thread(new WatekWypisujacy("A", 200, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1)); Thread th2 = new Thread(new WatekWypisujacy("B", 200, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1)); Thread th3 = new Thread(new WatekWypisujacy("C", 200, 1));
th1.setDaemon(true); th1.setDaemon(true);
th2.setDaemon(true); th2.setDaemon(true);
...@@ -22,7 +22,7 @@ public class Watki3_Demon { ...@@ -22,7 +22,7 @@ public class Watki3_Demon {
System.out.println("main: wątki uruchomione"); System.out.println("main: wątki uruchomione");
try { try {
Thread.sleep(50); Thread.sleep(90);
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
System.out.println("main: poczekałem sobie"); System.out.println("main: poczekałem sobie");
......
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