Chapter 33: one lock over one record, built
In Java the shared tally is a synchronized method or an AtomicLong. Here the tally is a record, the record sits under one lock inside a class, and the only way in is a key - a function that runs while the lock is held. Eight workers each make 250 deposits. The IDE writes the constants, the record and its defaults, the Ledger with its lock and the two methods' shapes, and the abstract work function; you type the keys and the workers. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-shared-tally.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module book.concurrency.tally defines constant workerCount <- 8 depositsPerWorker <- 250 defines record Tally deposits <- 0 total <- 0 default operator defines class Ledger lockedTally <- MutexLock(Tally()) deposit() -> amount as Integer depositor <- (amount) is MutexKey of Tally as function lockedItem.deposits += 1 lockedItem.total += amount require lockedTally.enter(depositor) snapshot() <- rtn as Tally: Tally() reader <- (rtn) is MutexKey of Tally as function rtn :=: lockedItem require lockedTally.enter(reader) default operator ? defines function work() as abstract <- rtn as Integer? defines program SharedTally() stdout <- Stdout() ledger <- Ledger() workers <- List() of work for id in 1 ... workerCount worker <- (ledger, id) is work as function for n in 1 ... depositsPerWorker ledger.deposit(id) rtn: id workers += worker finished <- cat workers | async | sort | collect as List of Integer stdout.println(`workers finished: ${finished}`) stdout.println(`tally: ${ledger.snapshot()}`)
Step 1: Constants
Choose Code > New Construct > Constant…
workerCount is the constant's name and 8 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows workerCount about to be written. Choose Apply.
Step 2: Constants
Choose Code > New Construct > Constant…
depositsPerWorker is the constant's name and 250 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.
The preview shows depositsPerWorker about to be written. Choose Apply.
Step 3: Tally — the record the lock protects
Choose Code > New Construct > Record…
Tally is the record's name. The Fields table holds deposits of type Integer, starting at 0; total of type Integer, starting at 0. Choose Preview.
The preview shows Tally about to be written - and default operator ? with them: a record that holds fields must say when it counts as set. Choose Apply.
Select the Tally line and, above it, type the blank line the book opens its first record with. Saves when typed.
Select the two fields and type them the way the book does: EK9 infers each type from its value, so deposits <- 0 is an Integer. Saves when typed.
Right-click the Tally line and choose Add Operator > default operator (all missing).
Select the line the first field brought, default operator ?: the bare default operator below it covers every operator, ? included, so it goes. Saves when typed.
Set the operators apart from the fields: above default operator, type a blank line. Saves when typed.
Step 4: Ledger — one lock over one record
Choose Code > New Construct > Class…
Ledger is the class's name. Choose Preview.
The preview shows Ledger about to be written. Choose Apply.
Select the Ledger line and, above it, type the blank line the book opens its first class with. Saves when typed.
Right-click the Ledger line and choose Add Field…
Type lockedTally <- MutexLock(Tally()) - the field and the value it starts with. Choose OK.
The preview shows lockedTally added - and default operator ? with it, Ledger's first field. Choose Apply.
Right-click the Ledger line and choose Add Method…
deposit is the method's name. Add one parameter, amount of type Integer. Choose Apply.
Select the stub and type deposit's key: a dynamic function that implements MutexKey of Tally and captures amount, handed to enter. While enter runs it the lock is held, the key sees the record as lockedItem, and when it ends - however it ends - enter releases. There is no unlock to forget. Saves when typed.
Right-click the Ledger line and choose Add Method…
snapshot is the method's name. Set Return type to Tally. Choose Apply.
Select the stub and type snapshot's key: it copies the locked record out with :=: - a copy is a new object, so nothing outside the lock holds a reference to the locked one. Saves when typed.
Step 5: work — what a worker is
Choose Code > New Construct > Function…
work is the function's name. Set Returns to Integer and tick as abstract - a function type: a contract with no body. Choose Preview.
The preview shows work about to be written. Choose Apply.
Select the work() as abstract line and, above it, type the blank line the book opens its first function with. Saves when typed.
Step 6: The program
Choose Code > New Construct > Program…
SharedTally is the program's name. Choose Preview.
The preview shows SharedTally about to be written. Choose Apply.
Select the SharedTally() line and, above it, type the blank line the book opens the program with. Saves when typed.
Select the stub, from ready <- true down to its //AI line, and type SharedTally's body exactly as the chapter shows it. Saves when typed.
Step 7: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
workers finished: [1, 2, 3, 4, 5, 6, 7, 8], then tally: Tally(deposits=2000, total=9000). Choose Close.
The result
SharedTally.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. lockedTally is a field with an initialiser - the only place a MutexLock may be built - and the only way to the Tally inside is a key. A race would have shown as a smaller total.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 213. - For AI assistants - the MCP tool
ek9_new_construct,ek9_add_operator,ek9_add_field,ek9_add_method,ek9_add_program.







































