# Ruby threads

**URL:** <https://www.ruby-forum.com/t/ruby-threads/171403>\
**Category:** Ruby\
**Created:** [July 1, 2009, 10:43pm UTC](https://www.ruby-forum.com/t/ruby-threads/171403 "2009-07-01T22:43:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 10:43pm UTC](https://www.ruby-forum.com/t/ruby-threads/171403/1 "2009-07-01T22:43:58Z")

</div>

Hi guys! Hope you`ll find some time to assist with any ideas

i have an array of links and a number of steps that needs to be done to  
each of those links.

there are always the same amount of links in array…so basically i can  
do everything manually

i was wondering how can i create the new thread for processing each of  
those links , maybe there`s a way of doing something like this :

array.each do |link|  
a = Thread.new {…}  
a.join  
end

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/ruby-threads/171403/2 "2009-07-01T23:16:29Z")

</div>

Vlad Smith wrote:

> array.each do |link|  
> a = Thread.new {…}  
> a.join  
> end

That almost works, except it will block on each thread before starting  
the next.

threads = []

array.each do |link|  
threads \<\< Thread.new {…}  
end

threads.each do |t|  
t.join  
end

-Justin

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 11:17pm UTC](https://www.ruby-forum.com/t/ruby-threads/171403/3 "2009-07-01T23:17:04Z")

</div>

On Thu, Jul 02, 2009 at 05:44:00AM +0900, Vlad Smith wrote:

> array.each do |link|  
> a = Thread.new {…}  
> a.join  
> end

Your code won’t run anything in parallel. Thread#join will wait until  
the thread is finished.

Try mapping your array to a list of threads, then iterating over that  
list calling join:

array.map { |link|  
Thread.new(link) { |l| … }  
}.each { |thread| thread.join }

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 3, 2009, 9:45am UTC](https://www.ruby-forum.com/t/ruby-threads/171403/4 "2009-07-03T09:45:11Z")

</div>

thanks everybody, works works just great!
