I have a thread doing things in background and I want to suspend it whenever my program has a real thing to do. Once that is finished I want the background thread to continue. E.g.
ta = Thread.new { loop { print 'a'; sleep 0.2 } }
sleep 0.5
# put ta to sleep
5.times { print 'b'; sleep 0.2 }
# let ta resume
sleep 0.8
puts
The output should be something like aaabbbbbaaaaa
. Currently it is aaabababababaaaaa
.
Although a thread can stop itself through Thread.stop
, I found no method to stop a thread from "outside". Is there a simple solution?