mech a nism used to ensure th a t only one thre a d c a n a ccess a sh a red resource or critic a l section a t a time. Wh a t do they gu a r a ntee? • Mutu a l exclusion → only one thre a d holds the lock a t a time • Visibility → ch a nges a re visible when the lock is rele a sed • Explicit control → lock a cquisition a nd rele a se a re progr a mmer-m a n a ged When to use Locks? • When a dv a nced coordin a tion is required beyond synchronized • When locking a nd unlocking must h a ppen in di ff erent scopes
keyword used to ensure th a t only one thre a d a t a time c a n execute a speci f ic piece of code or method. • It uses a lock implicitly Wh a t do they gu a r a ntee? • Mutu a l exclusion → only one thre a d enters the critic a l section • Visibility → ch a nges a re visible when the lock is rele a sed • Ordering → oper a tions occur in a predict a ble sequence When to use synchronized? • Protecting multiple rel a ted v a ri a bles • Enforcing a ll-or-nothing upd a tes • When code cl a rity a nd correctness m a tter more th a n r a w perform a nce
is a v a ri a ble th a t c a n be s a fely modi f ied by multiple thre a ds without using locks or synchronized blocks. Wh a t do they gu a r a ntee? • Atomicity → no interle a ving during upd a tes • Visibility → ch a nges a re immedi a tely visible to other thre a ds • No locks → uses low-level CPU comp a re- a nd-sw a p (CAS) When to use Atomic types? • Sh a red counters, f l a gs, or simple st a te • High-contention scen a rios where locks would slow things down • When only one v a ri a ble must be protected
tion mech a nism th a t controls a ccess to a resource by m a int a ining a f ixed number of permits. Thre a ds must a cquire a permit before proceeding a nd rele a se it when f inished. Wh a t do they gu a r a ntee? • Bounded a ccess → only a limited number of thre a ds m a y enter • Visibility → ch a nges a re visible when permits a re rele a sed • No ownership → permits a re not tied to a speci f ic thre a d When to use Sem a phores? • Limiting a ccess to a f inite resource (connections, printers, c a rts) • Throttling concurrency to a void overlo a d
component th a t m a n a ges a pool of thre a ds a nd executes t a sks a synchronously without m a nu a lly cre a ting or controlling thre a ds. Wh a t do they provide? • Thre a d reuse → a voids costly thre a d cre a tion • Concurrency control → limits how m a ny thre a ds run a t once • T a sk scheduling → queues a nd executes submitted t a sks • Sc a l a bility → a d a pts to multicore systems When to use ExecutorService? • Running m a ny short-lived t a sks • Limiting p a r a llelism to a v a il a ble CPU cores
viergs/CSC364/tree/m a in/08-control Concurrency mech a nisms explored: • ExecutorService (thre a d pools) • Sem a phore (limited resources) • Reentr a ntLock (exclusive a ccess) • synchronized (sh a red st a te protection)
seline. Run the progr a m unch a nged. Observe output order a nd termin a tion. Expl a in wh a t e a ch concurrency mech a nism represents P a rt 1 – Sem a phore Experiments (C a p a city Limits). Modify sem a phore permits (1, 2, 3, 10, 50). Observe concurrency, w a iting, a nd execution time. Expl a in why incre a sing permits eventu a lly stops helping P a rt 2 – Removing Sem a phore Protection. Comment out a cquire() / rele a se() c a lls. Observe loss of resource limits. Expl a in the re a l-world bug being simul a ted P a rt 3 – Lock Misuse (De a dlock). Remove unlock() on a Reentr a ntLock. Observe h a ngs a nd incomplete t a sks. Expl a in why a single missing unlock c a n block the system P a rt 4 – Removing synchronized (R a ce Conditions). Remove synchroniz a tion from sh a red st a te upd a tes. Run multiple times a nd observe inconsistent results. Expl a in r a ce conditions using observed beh a vior P a rt 5 – Thre a ds vs CPU Cores. Detect a v a il a ble CPU cores. Experiment with pool sizes (½×, 1×, 2× cores)
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. 30