Commit 6d087373 by Patryk Czarnik

Przykład yz podstaw wątków

parent a58a8b2c
package p28_watki.a_postawy;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicReferenceArray;
public class Priorytety {
private final int N;
private final int T;
private volatile boolean koniec = false;
private final AtomicLongArray tablica;
private final AtomicReferenceArray<Thread> watki;
public Priorytety(int n, int t) {
this.N = n;
this.T = t;
this.tablica = new AtomicLongArray(n);
this.watki = new AtomicReferenceArray<>(n);
}
private class Robotnik implements Runnable {
private final int numer;
Robotnik(int numer) {
this.numer = numer;
}
public void run() {
while(!koniec) {
tablica.incrementAndGet(numer);
}
}
}
private void run() {
final int polowa = N / 2;
for(int i = 0; i < N; i++) {
Thread th = new Thread(new Robotnik(i));
if(i < polowa) {
th.setPriority(Thread.MAX_PRIORITY);
} else {
th.setPriority(Thread.MIN_PRIORITY);
}
watki.set(i, th);
}
for(int i = 0; i < N; i++) {
watki.get(i).start();
}
try {
Thread.sleep(1000 * T);
koniec = true;
for(int i = 0; i < N; i++) {
watki.get(i).join();
}
} catch (InterruptedException e) {
System.err.println(e);
}
for(int i = 0; i < N; i++) {
if(i == polowa) {
System.out.println("================================");
}
long razy = tablica.get(i);
System.out.printf("%2d : %15d razy\n", i, razy);
}
}
public static void main(String[] args) {
// domyślne wartości parametrów: ilość wątków, czas trwania testu
int N = 32;
int T = 10;
// inne można przekazać w cmd-line
if(args.length >= 1) {
N = Integer.parseInt(args[0]);
}
if(args.length >= 2) {
T = Integer.parseInt(args[1]);
}
System.out.printf("Startujemy %d wątków, czekaj %d sekund...\n", N, T);
new Priorytety(N, T).run();
System.out.println("Koniec");
}
}
package p28_watki.a_postawy;
import java.io.IOException;
public class Przerwanie {
public static void main(String[] args) {
System.out.println("main: początek programu");
System.out.println("Mój obiekt Thread: " + Thread.currentThread());
System.out.println("Moje id: " + Thread.currentThread().getId());
Thread th = new Thread(() -> {
System.out.println("Start");
try {
Thread.sleep(5000);
System.out.println("sleep normalnie zakończony");
} catch (InterruptedException e) {
System.out.println("sleep przerwany " + e);
}
System.out.println("interrupted() ? " + Thread.interrupted());
System.out.println("Koniec");
});
System.out.println("main: robię th.start()");
th.start();
System.out.println("Wystartowałem wątek. Naciśnij enter, aby przerwać.");
try {
System.in.read();
// czekamy na ENTER i gdy zostanie naciśnięty, wywołamy interrupt na wątku
th.interrupt();
} catch (IOException e) {
e.printStackTrace();
};
System.out.println("Koniec main");
}
}
package p28_watki.a_postawy;
public class TworzenieWatkow {
public static void main(String[] args) {
System.out.println("Początek main, id wątku = " + Thread.currentThread().getId());
// Jak tworzyć nowe wątki?
// 1) dziedziczenie z klasy Thread:
class Watek0 extends Thread {
}
class Watek1 extends Thread {
// nadpisując metodę run podajemy treść wątku - instrukcje, które będzie on wykonywał
@Override
public void run() {
System.out.println("Watek1 początek, id = " + Thread.currentThread().getId());
for(int i = 1; i <= 10; i++) {
System.out.println("Watek1 działa, id = " + Thread.currentThread().getId() + ", i = " + i);
}
System.out.println("Watek1 koniec, id = " + Thread.currentThread().getId());
}
}
Watek1 watek1a = new Watek1();
// wątek jeszcze nie wystartował
// aby wątek zaczął działać, należy wywołać jego metodę
watek1a.start();
// gdybym wpisał watek1a.run(), to treść run też by się wykonała,
// ale w jednym wątku main ("synchronicznie")
Watek1 watek1b = new Watek1();
watek1b.start();
// 2) Implementacja interfejsu Runnable i utworzenie obiektu Thread na jej podstawie
class Watek2 implements Runnable {
public void run() {
System.out.println("Watek2 początek, id = " + Thread.currentThread().getId());
for(int i = 1; i <= 10; i++) {
System.out.println("Watek2 działa, id = " + Thread.currentThread().getId() + ", i = " + i);
}
System.out.println("Watek2 koniec, id = " + Thread.currentThread().getId());
}
}
Watek2 watek2_instancja_runnable = new Watek2();
// to nie jest jeszcze wątek - bo nie dziedziliśmy z Thread
// watek2_instancja_runnable.start();
Thread watek2a = new Thread(watek2_instancja_runnable);
// wiele wątków opartych o ten sam obiekt Runnable? - OK
Thread watek2b = new Thread(watek2_instancja_runnable);
watek2a.start();
watek2b.start();
Thread watek2c = new Thread(new Watek2());
watek2c.start();
// Zwięzłe zapisy: klasa anonimowa, wyrażenie lambda
Thread watek3a = new Thread(new Runnable() {
public void run() {
System.out.println("wątek 3 a");
}
});
watek3a.start();
Thread watek3b = new Thread(() -> {
System.out.println("wątek 3 b");
});
watek3b.start();
new Thread(() -> {
System.out.println("wątek 3 c");
}).start();
System.out.println("Koniec main");
}
}
......@@ -23,7 +23,7 @@ public class Watki2_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
// jeśli one się skończyły wcześniej, to od razu przechodzi dalej
try {
th1.join();
th2.join();
......
package p28_watki.a_postawy;
class WatekAA extends Thread {
@Override
public void run() {
System.out.println("AA");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
System.out.println("AA");
throw new RuntimeException("masakra");
//throw new Error("masakra"); // tak samo
}
}
class WatekBB extends Thread {
@Override
public void run() {
for(int i =1; i <= 15; i++) {
System.out.println("BB");
try {
Thread.sleep(333);
} catch (InterruptedException e) {
}
}
}
}
public class WyjatekWWatku {
public static void main(String[] args) {
WatekAA watekB = new WatekAA();
WatekBB watekC = new WatekBB();
watekB.start();
watekC.start();
System.out.println("main: uruchomilem watki");
for(int i =1; i <= 10; i++) {
System.out.println("MM");
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
}
}
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