10 timesRepeat: [ | collection threadCount semaphore safeguard | collection := SortedCollection new. safeguard := Semaphore forMutualExclusion. threadCount := 50. semaphore := Semaphore new. semaphore signal. (1 to: threadCount) do: [:each | Transcript show: each asString;cr. semaphore signal. [1 to: 250000 do: [:x | safeguard critical: [collection add: x; remove: x]]] fork. semaphore wait]. semaphore wait]