Controlling the Thread Scheduler : Synchronization « Threads « Ruby






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) } 
] 

 








Related examples in the same category

1.Multithreads without synchronization
2.This is easy using monitors.
3.Use monitor to lock
4.make specific objects into monitors.
5.Synchronizing Access to an Object