Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:10
What would you like to do?
thread_started_at =
thread_finished_at = nil
for i in 1..4 do
Thread.start(i) do |t|
puts "started thread #{t}"
sleep 1
puts "finished thread #{t}"
thread_finished_at = if t == 4
loop_started_at =
for i in 1..4 do
puts "started loop #{i}"
sleep 1
puts "finished loop #{i}"
loop_finished_at =
puts "Thread finished in #{thread_finished_at - thread_started_at}"
puts "Loop finished in #{loop_finished_at - loop_started_at}"
[23] pry(2.1.5-p273)> edit
started thread 4
started thread 2
started thread 3
started loop 1
started thread 1
finished thread 4finished thread 2
finished loop 1
started loop 2
finished thread 3
finished thread 1
finished loop 2
started loop 3
finished loop 3
started loop 4
finished loop 4
Thread finished in 1.001928
Loop finished in 4.003844
Copy link

SES のメール送信処理とかで時間がかかるやつは CPU に待ち時間が発生しているのでスレッドにする方がよさそう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment