Chapter 33: workers that share nothing, built
The safest concurrent program shares nothing. Eight workers each sum into a variable that only they can see, and the pipeline collects the results - there is no lock to forget, because there is nothing to lock. work is a function type with no body; each worker is a dynamic function that fills it in, capturing its own id. The IDE writes the two constants, the abstract function's signature and the program's shell; you type the workers and the pipeline. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-worker-totals.
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.totals defines constant workerCount <- 8 depositsPerWorker <- 250 defines function work() as abstract <- rtn as Integer? defines program WorkerTotals() stdout <- Stdout() workers <- List() of work for id in 1 ... workerCount worker <- (id) is work as function subtotal <- 0 for n in 1 ... depositsPerWorker subtotal += id rtn: subtotal workers += worker subtotals <- cat workers | async | sort | collect as List of Integer total <- Integer() cat subtotals > total stdout.println(`subtotals: ${subtotals}`) stdout.println(`total: ${total}`)
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.
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 2: work — a function
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 3: The program
Choose Code > New Construct > Program…
WorkerTotals is the program's name. Choose Preview.
The preview shows WorkerTotals about to be written. Choose Apply.
Select the WorkerTotals() 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 WorkerTotals's body exactly as the chapter shows it. Saves when typed.
Step 4: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
subtotals: [250, 500, 750, 1000, 1250, 1500, 1750, 2000], then total: 9000 - the same every time. Choose Close.
The result
WorkerTotals.ek9 now reads exactly as chapter 33 of EK9 in Depth shows it. Each worker's subtotal is its own; cat ... | async ran the eight together, sort put the answers in order whatever order they finished in, and cat subtotals > total added them.
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_program.


















