Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC364 Lecture 08

Avatar for Javier Gonzalez-Sanchez Javier Gonzalez-Sanchez PRO
February 02, 2026
120

CSC364 Lecture 08

Introduction to Networked, Distributed, and Parallel Computing
Concurrency Patterns
(202602)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

February 02, 2026

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227

    CSC 364 Introduction to Networked, Distributed, and Parallel Computing Lecture 08. Concurrency Design Patterns 1
  2. 2 De f inition A Concurrency Design P a ttern

    is a reus a ble solution to a common coordin a tion problem between multiple thre a ds. They describe: • How thre a ds inter a ct • How sh a red d a t a is a ccessed • How execution is coordin a ted s a fely P a tterns a re a bout structure a nd intent
  3. 3 Why Concurrency Patterns? Concurrency mech a nisms (sem a

    phore, lock, synchronize, pool, a tomic) tell us how to synchronize. P a tterns expl a in when a nd why. With p a tterns: • Code is e a sier to re a son a bout • Beh a vior is predict a ble • Systems sc a le more s a fely
  4. 4 Patterns 1. Producer–Consumer: Producers gener a te d a

    t a ; consumers t a ke d a t a using a sh a red bu ff er or queue. 2. Re a ders–Writers: Multiple re a ders c a n re a d simult a neously, but writers require exclusive a ccess.
  5. 6 Features • The Producer–Consumer p a ttern sep a

    r a tes the gener a tion of work from the processing of work. • One or more thre a ds produce t a sks. • One or more thre a ds consume t a sks. • They oper a te a t di ff erent speeds. • It provide decoupling between producers a nd consumers • S a fe sh a ring of a work bu ff er
  6. 7 When to Use Producer–Consumer? Use when: • Work a

    rrives continuously • Processing is a synchronous • Producers a nd consumers should not block e a ch other unnecess a rily Ex a mples: • T a sk queues • Network mess a ge h a ndling
  7. 3. Producer releases the lock and signals Semaphore B Producer

    Consumer 1. 2. 3. 4. 5. Buffer 4 1 📦
  8. 15 6. Consumer releases the lock and signals Semaphore A

    Producer Consumer 1. 2. 3. 4. 5. Buffer 5 0 📦
  9. 26 In general Producer Consumer 1. 2. 3. 4. 5.

    Buffer 5 0 Producer Producer Consumer Consumer Consumer
  10. 28 Features • Multiple thre a ds to re a

    d simult a neously • Re a ds do not block other re a ds. • Only one thre a d to write exclusively • Writes block everyone. • High perform a nce for re a d-he a vy worklo a ds • D a t a consistency during writes • Controlled a ccess b a sed on oper a tion type
  11. 29 When to Use Readers–Writers? • Re a ds a

    re much more frequent th a n writes • Sh a red d a t a must rem a in consistent • Exclusive write a ccess is required Ex a mples: • Course C a t a log in a University System • Web server c a che • G a me st a tes • IDE - compiler
  12. 30 Java ReadWriteLock A Re a dWriteLock is a lock

    with two di ff erent modes of a ccess: • Re a d lock → sh a red • Write lock → exclusive Why do we need it? • With a norm a l lock (synchronized or Reentr a ntLock) only one thre a d c a n a ccess the d a t a a t a time • Re a ders block other re a ders, even though they don’t modify a nything. Blocking re a ders from e a ch other is unnecess a ry a nd slow.
  13. 33 1. Writer acquires the buffer lock and write Writer

    Reader 1. 2. 3. 4. 5. Buffer 📄 📜 📄 📄 📄 📄
  14. 35 3. Reader acquires the buffer lock and read Writer

    Reader 1. 2. 3. 4. 5. Buffer 📜 📄 📄 📄 📄
  15. 36 Additional Readers are Allowed to Read Writer Reader 1.

    2. 3. 4. 5. Buffer 📜 📄 📄 📄 📄
  16. 47 In general Writer 1. 2. 3. 4. 5. Buffer

    📜 📄 📄 📄 📄 Reader Reader Reader Reader Writer Writer Reader Reader Reader
  17. 49 Concurrent Team Ball Chase • Two te a ms

    move on a grid: Te a m A a nd Te a m B. There is one sh a red b a ll, a nd only one pl a yer owns the b a ll a t a time. • The b a ll c a rrier tries to re a ch the opponent’s go a l. Other pl a yers move concurrently: te a mm a tes try to block ne a rby enemies or support the b a ll c a rrier, while enemies try to collide with the b a ll c a rrier to ste a l the b a ll.
  18. 54 Game Rules 1. Pl a yers m a y

    move one cell per turn in a ny of the 8 directions. 2. Other pl a yers a ct a s moving obst a cles. 3. No two pl a yers m a y occupy the s a me cell unless the move is a v a lid collision with the opposing b a ll c a rrier. 4. If a n enemy collides with the b a ll c a rrier, possession ch a nges. 5. When possession ch a nges, the te a m with the b a ll becomes the a tt a cking te a m. 6. If the b a ll c a rrier re a ches the opponent’s go a l, th a t te a m scores. 7. After a go a l, a ll pl a yers return to their origin a l st a rting positions, a nd the te a m th a t conceded the go a l st a rts with possession of the b a ll.
  19. 55 Concurrent Behavior • Mode 1 — Turn-B a sed

    Concurrent Execution. E a ch pl a yer is upd a ted a s a t a sk once per turn. The g a me submits one t a sk per pl a yer, w a its for a ll t a sks to f inish, resolves moves, a nd then redr a ws the grid. • Mode 2 — Independent Pl a yer Thre a ds. E a ch pl a yer runs a s a n independent thre a d. Pl a yers m a y move a t di ff erent speeds, so some pl a yers m a y move more often th a n others. • Atomic / Lock / Sem a phore: protect b a ll ownership, pl a yer positions, a nd limited- a ccess a re a s. • Producer–Consumer: pl a yer t a sks produce movement events; the UI consumes them a nd redr a ws the grid. • Re a ders–Writers: m a ny pl a yers m a y re a d the grid st a te concurrently, but upd a tes to positions or possession require exclusive a ccess.
  20. 56 Important Bugs to Watch For • If b a

    ll possession is not locked, two pl a yers m a y ste a l the b a ll a t the s a me time. • If position upd a tes a re not protected, two pl a yers m a y move into the s a me cell. • If movement locks a re a cquired incorrectly, pl a yers m a y block e a ch other forever.
  21. 57 Console UI is f ine. Score A: 0 |

    B: 0 Ball: A1 Turn 12 G . . . . . . . G . A2 . . B3 . . . . . . A* . . B1 . . . A3 . . B2 . . G . . . . . . . G A* moves from (3,2) to (4,2) B2 moves from (5,3) to (4,2) <Messages> XX steals the ball or Team XX scores
  22. Lab

  23. 60 Concurrent BFS Chase with Replanning . . . .

    . . . . X . # . X . . . . # . . . . # . T . # . . . . . . X . T = target (data) X = enemy (thread) # = wall (data) . = empty cell (data) Each enemy uses BFS to compute the shortest path to the target, moves one step along that path if the next cell is free, and then recomputes the path on its next turn. So, each enemy repeats this cycle independently: 1. Read the current grid 2. Run BFS from my position to T 3. Get the shortest path 4. Move only one step along that path 5. Sleep briefly 6. Repeat
  24. CSC 364 Introduction to Introduction to Networked, Distributed, and Parallel

    Computing Javier Gonzalez-Sanchez, Ph.D. [email protected] Winter 2026 Copyright. These slides can only be used as study material for the class CSC 364 at Cal Poly. They cannot be distributed or used for another purpose. 61