api:documentation:threads:start
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
api:documentation:threads:start [2024/10/28 13:56] – [Synchronized-Methode] martin | api:documentation:threads:start [2025/03/05 10:26] (aktuell) – [synchronized-Block] martin | ||
---|---|---|---|
Zeile 121: | Zeile 121: | ||
{{ : | {{ : | ||
Im rechts stehenden Sequenzdiagramm ist zu sehen, was passiert, wenn zwei Threads gleichzeitig die Methode '' | Im rechts stehenden Sequenzdiagramm ist zu sehen, was passiert, wenn zwei Threads gleichzeitig die Methode '' | ||
- | Man nennt diesen Effekt eine **race condition**. | + | Man nennt diesen Effekt eine **race condition**. |
</ | </ | ||
Zeile 127: | Zeile 127: | ||
<WRAP center round info 80%> | <WRAP center round info 80%> | ||
{{ : | {{ : | ||
- | Setzt man vor die Deklaration der Methode '' | + | Wir brauchen eine Möglichkeit, |
* Vergleichen Sie die Deklaration der Methode '' | * Vergleichen Sie die Deklaration der Methode '' | ||
* Starten Sie das Programm mehrmals. Erklären Sie die Ausgabe! | * Starten Sie das Programm mehrmals. Erklären Sie die Ausgabe! | ||
Zeile 184: | Zeile 184: | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Die Klasse Semaphore ===== | ||
+ | <WRAP center round info 80%> | ||
+ | Ein **Semaphor** ist ein Objekt, das einen Zähler enthält, dessen Wert angibt, von wie vielen Prozessen ein kritischer Abschnitt im Augenblick gerade noch betreten werden darf. \\ (**Bem.:** Eine andere Interpretation besteht darin, dass es $n$ gleichartig Ressourcen gibt. Der Wert des Zählers gibt an, wie viele im Augenblick gerade zur Verfügung stehen.) \\ \\ Der Semaphor besitzt zwei Methoden: | ||
+ | * '' | ||
+ | * Ist dies der Fall, so wird der Zähler um eins erniedrigt und mit der danach folgenden Anweisung fortgefahren. Dem Prozess wird damit das Betreten des kritischen Bereichs gestattet. | ||
+ | * Ist dies **nicht** der Fall, so wird der Prozess in einen Wartezustand versetzt. \\ | ||
+ | * '' | ||
+ | * Falls dies der Fall ist, wir einer davon " | ||
+ | * Ist dies **nicht** der Fall, so wird der Zähler um 1 erhöht. | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | Counter counter = new Counter(); | ||
+ | Semaphore semaphore = new Semaphore(1); | ||
+ | |||
+ | for (int i = 0; i < 3; i++) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | class Counter { | ||
+ | long counter = 0; | ||
+ | |||
+ | | ||
+ | long i = counter; | ||
+ | i++; | ||
+ | counter = i; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | class Incrementer implements Runnable { | ||
+ | |||
+ | | ||
+ | long increment = 0; | ||
+ | int index; | ||
+ | | ||
+ | |||
+ | | ||
+ | this.increment = increment; | ||
+ | this.counter = counter; | ||
+ | this.index = index; | ||
+ | this.semaphore = semaphore; | ||
+ | } | ||
+ | |||
+ | | ||
+ | for (long i = 0; i < increment; i++) { | ||
+ | if(i % (increment / 10) == 0) println(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Monitorkonzept ===== | ||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | BlockingQueue< | ||
+ | |||
+ | for (int i = 0; i < 3; i++) { | ||
+ | new Producer(queue, | ||
+ | new MyConsumer(queue, | ||
+ | } | ||
+ | |||
+ | while (true); | ||
+ | |||
+ | |||
+ | class Producer implements Runnable { | ||
+ | | ||
+ | int count; | ||
+ | int producerIndex; | ||
+ | |||
+ | | ||
+ | this.queue = queue; | ||
+ | this.count = count; | ||
+ | this.producerIndex = producerIndex; | ||
+ | Thread t = new Thread(this); | ||
+ | t.setName(" | ||
+ | t.start(); | ||
+ | } | ||
+ | |||
+ | | ||
+ | while (count > 0) { | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class MyConsumer implements Runnable { | ||
+ | | ||
+ | int count; | ||
+ | int consumerIndex; | ||
+ | |||
+ | | ||
+ | this.queue = queue; | ||
+ | this.count = count; | ||
+ | this.consumerIndex = consumerIndex; | ||
+ | Thread t = new Thread(this); | ||
+ | t.setName(" | ||
+ | t.start(); | ||
+ | } | ||
+ | |||
+ | | ||
+ | while (count > 0) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | <script type=" | ||
+ | public class BlockingQueue < T extends Object > { | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | this.capacity = capacity; | ||
+ | } | ||
+ | |||
+ | | ||
+ | while (queue.size() == capacity) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | queue.add(element); | ||
+ | notify(); // notifyAll() for multiple producer/ | ||
+ | } | ||
+ | |||
+ | | ||
+ | while (queue.isEmpty()) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | T item = queue.remove(); | ||
+ | notify(); // notifyAll() for multiple producer/ | ||
+ | return item; | ||
+ | } | ||
+ | |||
+ | | ||
+ | return queue.size(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== synchronized-Block ===== | ||
+ | < | ||
+ | |||
+ | <div class=" | ||
+ | |||
+ | <script type=" | ||
+ | |||
+ | Counter counter = new Counter(); | ||
+ | |||
+ | for (int i = 0; i < 3; i++) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | class Counter { | ||
+ | long counter = 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | class Incrementer implements Runnable { | ||
+ | |||
+ | | ||
+ | long increment = 0; | ||
+ | int index; | ||
+ | |||
+ | | ||
+ | this.increment = increment; | ||
+ | this.counter = counter; | ||
+ | this.index = index; | ||
+ | } | ||
+ | |||
+ | | ||
+ | for (long i = 0; i < increment; i++) { | ||
+ | if(i % (increment/ | ||
+ | |||
+ | | ||
+ | long i = counter.counter; | ||
+ | i++; | ||
+ | counter.counter = i; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ |
api/documentation/threads/start.1730123793.txt.gz · Zuletzt geändert: 2024/10/28 13:56 von martin