Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
Ruby Thread. why "#Join"?

This may sound dumb question. but Please guide me.

https://apidock.com/ruby/Thread/join

a = Thread.new { print "a"; }
a.join(5)

As we see here, #join method is basically, "Hey OS, Run this code block(thread), 5 seconds from now."

but where this name come from? join . why not just run?
in Java, it seems like slightly different meaning.

EDIT

I found out that actually #join is blocking call. It means that check 5 second later, if the thread has been finished or not. If finished, kill the thread.(x) . join will return Thread object. which is already Dead.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Not quite. join is the traditional name for the operation that waits for a thread to complete. Execution "splits" when you start a thread, and "joins" when a parent becomes aware of a thread's completion.

a.join(5) doesn't say "run this thread 5 seconds from now", it says "wait up to 5 seconds for this thread to finish". It starts running as soon as you create it.

over 4 years ago · Santiago Trujillo Report

0

Thread.new starts the thread. You can do a simple test:

Thread.new { puts "started" };
sleep 1
puts "done"

It will print started immediately, then wait a second, then print done.

Or run Thread.new in irb and see it execute immediately.

You need a.join because without it the main program might exit and kill the threads before the thread is complete. For example...

Thread.new do
  puts "start"

  # Simulate the thread doing some work
  sleep 1

  puts "end"
end
sleep 0.5

This will print start and that's it. The main program will end and kill the thread. The main process needs to wait until the thread is complete with join.

It's called "join" because you are joining the thread back together with the main thread which spawned it.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!