tmp.py その2

翻訳頼まれたんで翻訳した

from threading import Thread

max = 3
lmax = 50000000

ary = [[0, 0]]

class MyThread(Thread):
    def run(self):
        for _i in range(lmax):
            e = ary[-1]
            if e[0] != self.name:
                e = [self.name, 0]
                ary.append(e)
            e[1] += 1

threads = [MyThread(name=i) for i in "123"]
for th in threads: th.start()
for th in threads: th.join()

for p in ary:
    print "%s\t%d" % (p[0], p[1])

print len(ary)

原文、消えちゃうから転載しちゃっていいよと許可もらった

max = 3
lmax = 50_000_000

# test fairness
ary = [[0, 0]] # [[Tid, num], [Tid, num], ...]
(1..max).map{|i|
  Thread.new(i){|tid|
    lmax.times{
      e = ary[-1]
      if e[0] != tid
        e = [tid, 0]
        ary << e
      end
      e[1] += 1
    }
  }
}.each{|t|
  t.join
}
ary.each{|(t, c)|
  puts "#{t}\t#{c}"
}
puts ary.length