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
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
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
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
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
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
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.
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
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
(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)
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