Tutorial by Examples

A new thread separate from the main thread's execution, can be created using Thread.new. thr = Thread.new { sleep 1 # 1 second sleep of sub thread puts "Whats the big deal" } This will automatically start the execution of the new thread. To freeze execution of the main Thread, ...
Use a mutex to synchronise access to a variable which is accessed from multiple threads: counter = 0 counter_mutex = Mutex.new # Start three parallel threads and increment counter 3.times.map do |index| Thread.new do counter_mutex.synchronize { counter += 1 } end end.each(&:joi...
You call use Thread.kill or Thread.terminate: thr = Thread.new { ... } Thread.kill(thr)
A thread terminates if it reaches the end of its code block. The best way to terminate a thread early is to convince it to reach the end of its code block. This way, the thread can run cleanup code before dying. This thread runs a loop while the instance variable continue is true. Set this variable...

Page 1 of 1