İçeriğe geç

Join'siz thread (ARCH6041)

Anahtar ruby.async.thread-without-join · Önem warning · Kategori async

Bir thread başlatılıp join edilmezse, onu oluşturan kapsam thread’in işi bitmeden çıkabilir. Bu durumda thread içindeki istisnalar yutulur ve sonuçlar kaybolur.

def process_batch(items)
items.each do |item|
Thread.new { upload(item) }
end
# method returns; uploads may not finish, errors lost
end
def process_batch(items)
threads = items.map do |item|
Thread.new { upload(item) }
end
threads.each(&:join)
end

Oluşturduğunuz her thread’i bir değişkende tutun ve kapsamdan çıkmadan önce join çağırın.