異なる要求のタイムアウトを処理する単純なタイムアウトクラスを実装しようとしています。これは、正常に動作するようです同じコードの2つのバージョンが同じ結果を返さない
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timeout[1] = 41
timeout[2] = 5
timeout[3] = 14
t1 = MyTimer.new
t2 = MyTimer.new
t3 = MyTimer.new
first = Thread.new do
t1.handleTimeout(timeout,1)
end
second = Thread.new do
t2.handleTimeout(timeout,2)
end
third = Thread.new do
t3.handleTimeout(timeout,3)
end
first.join
second.join
third.join
:ここ
は、最初のバージョンです。すべてのタイムアウトは、互いに独立して動作します。
class MyTimer
def handleTimeout mHash, k
while mHash[k] > 0 do
mHash[k] -=1
sleep 1
puts "#{k} : #{mHash[k]}"
end
end
end
MAX = 3
timeout = Hash.new
timers = Array.new(MAX+1)
threads = Array.new(MAX+1)
for i in 0..MAX do
timeout[i] = rand(40)
# To see timeout value
puts "#{i} : #{timeout[i]}"
end
sleep 1
for i in 0..MAX do
timers[i] = MyTimer.new
threads[i] = Thread.new do
timers[i].handleTimeout(timeout, i)
end
end
for i in 0..MAX do
threads[i].join
end
なぜこの出来事である: Screenshot attached
コードの第2のバージョンは、しかしながら、異なる結果を生成しますか?
アレイを使用してこの機能を実装するにはどうすればよいですか?
同じ機能を実装するより良い方法はありますか?
私はこのようなものを使用して、サーバーへの要求のタイムアウトを処理します。それを行う良い方法はありますか? –
@ B.Nabiどのような要求がありますか? HTTP? –
はいHTTPリクエスト。 –