__sleep()
click to toggle source
def __sleep
sleep(rand(0.08))
end
_instantiate?()
click to toggle source
def _instantiate?
@enter.push Thread.current[:i]
while false.equal?(@singleton__instance__)
@singleton__mutex__.unlock
sleep 0.08
@singleton__mutex__.lock
end
@leave.push Thread.current[:i]
@singleton__instance__
end
instantiate_all()
click to toggle source
def instantiate_all
@enter = []
@leave = []
1.upto(9) {|i|
Thread.new {
begin
Thread.current[:i] = i
__sleep
instance
rescue RuntimeError => mes
puts mes
end
}
}
puts "Before there were #{num_of_instances(self)}"
sleep 3
puts "Now there is #{num_of_instances(self)}"
puts "#{@enter.join '; '} was the order of threads entering the waiting loop"
puts "#{@leave.join '; '} was the order of threads leaving the waiting loop"
end
new()
click to toggle source
def initialize
self.class.__sleep
puts "initialize called by thread ##{Thread.current[:i]}"
end
new()
click to toggle source
def new
begin
__sleep
raise "boom - thread ##{Thread.current[:i]} failed to create instance"
ensure
class << self
remove_method :new
end
end
end