Commit a58a8b2c by Patryk Czarnik

joiny i demony

parent 95f69a8d
package p28_watki.a_postawy;
class WatekWypisujacy implements Runnable {
private final String tekst;
private final int iloscPowtorzen;
private final int pauza;
public WatekWypisujacy(String tekst, int iloscPowtorzen, int pauza) {
this.tekst = tekst;
this.iloscPowtorzen = iloscPowtorzen;
this.pauza = pauza;
}
public void run() {
System.out.println(tekst + " : start wątku, nr " + Thread.currentThread().getId());
try {
for(int i = 1; i <= iloscPowtorzen; i++) {
if(pauza > 0) {
Thread.sleep(pauza);
}
System.out.println(tekst + " " + i);
}
} catch (InterruptedException e) {
System.err.println(e);
}
System.out.println(tekst + " : koniec wątku, nr " + Thread.currentThread().getId());
}
}
package p28_watki.a_postawy;
public class Watki1 {
public static void main(String[] args) {
System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1));
th1.start();
th2.start();
th3.start();
System.out.println("main: wątki uruchomione");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
System.out.println("main: poczekałem sobie");
System.out.println("koniec main");
// main dochodzi do końca, a wątki działają dalej
}
}
package p28_watki.a_postawy;
public class Watki2_Join {
public static void main(String[] args) {
System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1));
System.out.println("stan przed start: " + th1.getState());
th1.start();
th2.start();
th3.start();
System.out.println("main: wątki uruchomione, mój nr " + Thread.currentThread().getId());
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
System.out.println("main: Teraz będę czekał na wątki za pomocą join");
System.out.println("stan przed join: " + th1.getState());
// wątek main czeka na zakończenie wątków th1, th2, th3
// jeśli one się skończyły wcześniej, to od razu przechodzi dalej
try {
th1.join();
th2.join();
th3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main: doczekałem się na zakończenie wątków");
System.out.println("stan po join: " + th1.getState());
System.out.println("koniec main");
}
}
package p28_watki.a_postawy;
public class Watki3_Demon {
public static void main(String[] args) {
System.out.println("Początek main");
Thread th1 = new Thread(new WatekWypisujacy("A", 100, 1));
Thread th2 = new Thread(new WatekWypisujacy("B", 100, 1));
Thread th3 = new Thread(new WatekWypisujacy("C", 100, 1));
th1.setDaemon(true);
th2.setDaemon(true);
th3.setDaemon(true);
th1.start();
th2.start();
th3.start();
// setDaemon wywołane po start jest niepoprawne - kończy się wyjątkiem
// th1.setDaemon(false);
// th1.setDaemon(true);
System.out.println("main: wątki uruchomione");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
System.out.println("main: poczekałem sobie");
System.out.println("koniec main");
// Jeśli w działaniu pozostały wyłącznie wątki będące demonami, to proces jest kończony (i te wątki są "zabijane" w dowolnym momencie).
}
}
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