#!/usr/bin/env ruby

require 'pry'

class Test
  def initialize
    @result = []
    threads = []
    10000.times do |number|
      while threads.size > 50
        sleep 0.01
        deleted = []
        threads.delete_if { |thread| deleted << thread  if not thread.alive? }
        deleted.each { |thread| thread.terminate; GC.start }
      end
      binding.pry if File.exist? 'test.pry'
      threads << Thread.new { mah_thread number }
    end
    threads.each { |thread| thread.join }
    binding.pry
  end

  def mah_thread number
    @result[number] = rand(100000)
    sleep rand(10)/10.0
  end
end

Test.new