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/12/02 19:57] – [Monitorkonzept mit der Klasse Semaphore] martin | api:documentation:threads:start [2025/03/05 10:26] (aktuell) – [synchronized-Block] martin | ||
---|---|---|---|
Zeile 186: | Zeile 186: | ||
- | ===== Monitorkonzept mit der Klasse Semaphore ===== | + | ===== Die Klasse Semaphore ===== |
<WRAP center round info 80%> | <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: | 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: | ||
Zeile 253: | Zeile 253: | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== 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.1733169422.txt.gz · Zuletzt geändert: 2024/12/02 19:57 von martin