Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:20:51+00:00 2026-05-16T06:20:51+00:00

Calling Thread.join blocks the current (main) thread. However not calling join results in all

  • 0

Calling Thread.join blocks the current (main) thread. However not calling join results in all spawned threads to be killed when the main thread exits. How does one spawn persistent children threads in Ruby without blocking the main thread?

Here’s a typical use of join.

for i in 1..100 do
  puts "Creating thread #{i}"
  t = Thread.new(i) do |j|
    sleep 1
    puts "Thread #{j} done"
  end
  t.join
end
puts "#{Thread.list.size} threads"

This gives

     Creating thread 1  
     Thread 1 done  
     Creating thread 2  
     Thread 2 done  
     ...  
     1 threads  

but I’m looking for how to get this

    Creating thread 1  
    Creating thread 2  
    ...  
    101 threads  
    Thread 1 done  
    Thread 2 done  
    ...  

The code gives the same output in both Ruby 1.8.7 and 1.9.2

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:20:51+00:00Added an answer on May 16, 2026 at 6:20 am

    You simply accumulate the threads in another container, then join them one-by-one after they’ve all been created:

    my_threads = []
    for i in 1..100 do
        puts "Creating thread #{i}"
        my_threads << Thread.new(i) do |j|
            sleep 1
            puts "Thread #{j} done"
        end
    end
    puts "#{Thread.list.size} threads"
    
    my_threads.each do |t|
        t.join
    end
    

    You also can’t bind the thread to the i variable because i gets constantly overwritten, and your output will be 100 lines of “Thread 100 done”; instead, you have to bind it to a copy of i, which I have cleverly named j.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's one way. I don't really know if it's the… May 16, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer I would suggest Wx. It is a meta-toolkit designed for… May 16, 2026 at 3:21 pm
  • Editorial Team
    Editorial Team added an answer When using a panedwindow, you should be able to switch… May 16, 2026 at 3:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In my program, I am creating several threads in the main() method. The last
What is the significance of the Thread.Join method in C#? MSDN says that it
EDIT: It is not a listbox. My mistake. It is a list view. I
I've got this script, that uploads some files, connects via ssh and does some
I'm aware, and use, the xxx.Dispatcher.Invoke() method to get the background thread to manipulate
The following code demonstrates my dilemma. The code creates a background thread which processes
Let's say I define the sequence of all natural numbers in the following way:
I'm trying to obtain a good understanding of multi-threading in C# and I'm a
I've had a test, and there was a question I lost some points on,
Many times i am using code like this: public static void ExecuteMethod1InThread(string msg) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.