0
0
Rubyprogramming~15 mins

Thread creation and execution in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Thread creation and execution
📖 Scenario: You are building a simple Ruby program that uses threads to run tasks at the same time. This helps your program do multiple things without waiting for one to finish before starting another.
🎯 Goal: Create and run threads in Ruby to print messages concurrently.
📋 What You'll Learn
Create a thread with a block of code
Start the thread
Use Thread.new to create threads
Use join to wait for threads to finish
Print messages from threads
💡 Why This Matters
🌍 Real World
Threads let programs do many things at once, like downloading files while showing progress or handling multiple users in a chat app.
💼 Career
Understanding threads is important for jobs involving concurrent programming, improving app performance, and working with real-time systems.
Progress0 / 4 steps
1
Create a thread that prints a message
Create a thread called thread1 using Thread.new that prints the message "Hello from thread 1" inside the thread block.
Ruby
Need a hint?

Use Thread.new do ... end to create a thread and put puts inside the block.

2
Create a second thread with a different message
Create a second thread called thread2 using Thread.new that prints the message "Hello from thread 2" inside the thread block.
Ruby
Need a hint?

Use the same pattern as thread1 but change the variable name and message.

3
Wait for both threads to finish
Use the join method on both thread1 and thread2 to make sure the main program waits for both threads to complete.
Ruby
Need a hint?

Call join on each thread variable to wait for them.

4
Print a final message after threads complete
Add a puts statement after joining the threads to print "Both threads have finished".
Ruby
Need a hint?

Use puts "Both threads have finished" after the join calls.