Mega Code Archive

 
Categories / Ruby / Threads
 

Controlling the Thread Scheduler

class MyData      attr_reader :count      def initialize(name)          @name = name          @count = 0      end      def chase(other)          while @count < 5              while @count < other                 count > 1                  Thread.pass              end              @count += 1              print "#@name: #{count}\n"          end      end  end  c1 = MyData.new("A")  c2 = MyData.new("B")  threads = [  Thread.new { Thread.stop; c1.chase(c2) },  Thread.new { Thread.stop; c2.chase(c1) }  ]