use System.Threading class X const numThreads = 8 const numReps = 20 const minSleepMS = 1 const maxSleepMS = 20 shared var _rand = Random() var state = 0 def delay Thread.sleep(_rand.next(.minSleepMS, .maxSleepMS)) def work for n in .numReps, .readAndWriteState var locker = Object() def readAndWriteState # Change state from 0 to 1 and back to 0 # with assertions along the way. # The delays allow other threads to modify the shared state # if a proper lock is not in place by whatever means. lock .locker .delay assert .state == 0 .state = 1 .delay assert .state == 1 .state = 0 .delay assert .state == 0 def main threads = for i in .numThreads get Thread(ThreadStart(ref .work)) for t in threads, t.start