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/04 10:29] – [Monitorkonzept mit der Klasse Semaphore] martin | api:documentation:threads:start [2025/03/05 10:26] (aktuell) – [synchronized-Block] martin | ||
---|---|---|---|
Zeile 258: | Zeile 258: | ||
< | < | ||
- | <div class=" | + | <div class=" |
<script type=" | <script type=" | ||
- | BlockingQueue queue = new BlockingQueue<> | + | BlockingQueue< |
for (int i = 0; i < 3; i++) { | for (int i = 0; i < 3; i++) { | ||
- | Producer p = new Producer(queue, | + | new Producer(queue, |
- | MyConsumer c = new MyConsumer(queue, | + | new MyConsumer(queue, |
} | } | ||
Zeile 358: | Zeile 358: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | ===== 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.1733308161.txt.gz · Zuletzt geändert: 2024/12/04 10:29 von martin