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

CSC364 Lecture 09

Avatar for Javier Gonzalez-Sanchez Javier Gonzalez-Sanchez PRO
February 04, 2026
17

CSC364 Lecture 09

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

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

February 04, 2026
Tweet

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 09. 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. 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
  5. 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
  6. 15 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
  7. 16 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
  8. 17 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.
  9. 24 De f inition • An Immut a ble Object

    c a nnot be modi f ied a fter cre a tion. • Once constructed, its st a te never ch a nges. Wh a t does this p a ttern gu a r a ntee? • Thre a d s a fety by design • No synchroniz a tion required • No r a ce conditions When to Use Immut a ble Objects? • D a t a is sh a red a cross thre a ds • St a te does not need to ch a nge • Simplicity a nd s a fety a re priorities
  10. 26 Code • final on a class prevents extension. •

    final on fields prevents mutation. • Immutability requires both — plus no setters.
  11. 28 Features The Gu a rded Suspension p a ttern

    forces a thre a d to w a it until a condition becomes true. Execution is suspended until it is s a fe to continue. Wh a t does this p a ttern gu a r a ntee? • Correct ordering of oper a tions • No busy w a iting • S a fe coordin a tion using conditions When to Use Gu a rded Suspension? • A thre a d depends on a condition • D a t a m a y not be re a dy yet • W a iting must be e ff icient
  12. 35 Path f inder 1 2 3 4 5 6

    11 12 13 14 15 7 8 9 10
  13. 36 Technical Speci f ication • Observer P a ttern

    (PropertyCh a ngeSupport a nd PropertyCh a ngeListener) • J a v a Swing for GUI • Allow a Big number of cells • Perform a nce is import a nt - cre a te multiple Thre a ds • It is a bout f inding the best (shortest dist a nce) p a th not just a ny p a th • Te a m of 2 or 3 (m a ximum 3)
  14. 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. 37