Updated March 31, 2023
Introduction to Ruby Thread
Thread in Ruby allows the developer to write codes where they can be able to write and execute many programs at once, in technical words suppose we are going to perform two operations addition and multiplication at the same time, it is not possible with normal way because it will execute either addition first or multiplication first, but with the help of Thread concept in the Ruby, It allows our code to run both addition and multiplication at the same time, the main benefit for these kinds of approach it allows to use maximum available resources and concurrency within the process.
How Thread works in Ruby?
A thread is a concept where two statement runs in parallel. But threads in the Ruby is different as compare with threads in case of Java, as in Ruby all the Threads are implemented inside the interpreter, which means Ruby threads are completely independent of the operating system and we know if it is independent of the operating system it will be portable(basically work for any operating systems). Working of thread in The Ruby is based on the Life cycle methods, we can explain the life cycle methods of the Ruby in the below steps.
- We use new to create a new thread in Ruby, we also have options like Thread.start and Thread.fork.
- Thread in ruby starts automatically, which means we are not required to start it manually, once there are available resources from the CPU.
- If we write Thread.new(code block), here thread will run the code in its code block and then stop it. In Ruby, it gives many methods to handle running thread to manipulate and get information about the thread.
- There are some important methods for the Thread in Ruby like the current( it contains the object of the current thread and on calling Thread.current it returns this object ). Another method is main in the Thread which contains the object which represents the main thread on calling the Thread.main method.
- Thread in Ruby contains a very beautiful feature which is, We can put wait for any specific thread to complete by calling a method called to join, so if we make a call join it will block until this thread completes.
How to initialize Thread in Ruby?
To initialize any thread in Ruby we just need to call Thread.new and any associated block within this Thread.new will start. See the below syntax for the initialization of the thread in Ruby.
Thread.new {
# Here the block of code for running as the thread
}
How to terminate Thread in Ruby?
We can use the general formula to terminate any Thread, which is Thread.kill(thread). In case of any exceptions happen when the code is running in that case Ruby also gives us a way to handle it, we can terminate the Thread or operation with the help of method abort_on_exception. We just have to assign the value true to it to stop the execution of the Thread. For example Thread.abort_on_exception = true. There are other ways to stop and exit also.
Examples of Thread in Ruby
Here are the following examples as mentioned below.
Example #1
In the below example we are showing the running of two functions at the same time, we can explain the below example in the following ways.
- We have defined two functions school1 and school2 and these functions contain the logic for the increments.
- We used Thread.new(school1) and Thread.new(school2) and on the execution of these, it will keep printing the time of class starts.
- We can see in the output that many prints contain the same time as class started. It means that there were two executions happening at the same time.
Please follow the below example along with the screen of output.
Code:
# This example explains to us how we are able to run multiple threads in the ruby
def school1
counter1 = 0
while counter1<=2
puts "Class started at: #{Time.now}"
sleep(2)
counter1 = counter1+1
end
end
def school2
counter2 = 0
while counter2<=2
puts "Class started at: #{Time.now}"
sleep(1)
counter2 = counter2+1
end
end
puts "Common class started at #{Time.now}"
school1Thread = Thread.new{school1()}
school2Thread = Thread.new{school2()}
school1Thread.join
school2Thread.join
puts "Finally classes end at #{Time.now}"
Output:
Example #2
In this example, we are adding numbers at the same time for the two variables and subtracting for them. So we can explain the below example in the following steps.
- We have imported the thread module here.
- We have defined a variable subtraction and assigned the initial value for it as 0.
- We have also defined two variables a and b as these two variables will play the role for the addition.
- We have created two threads and inside one thread we are performing the addition or increment activity and inside the other thread, we are performing the subtraction activity.
Code:
# importing the thread module
require 'thread'
a = b = 0
subtraction = 0
Thread.new do
loop do
a += 1
b += 1
end
end
Thread.new do
loop do
subtraction += (a - b).abs
end
end
sleep 1
puts "The value of a is : #{a}"
puts "The value of b is : #{b}"
puts "subtraction of the a and b is : #{subtraction}"
Output:
Example #3
In the Below example, we are printing the value from 1 to 6 for both the counter which is counter1 and counter2. In the below output screen we can see counter1 and counter2 both are printing parallel, some time sequences may not be in order and it depends on the allocation of execution spaces to statements from the CPU.
Code:
def function1
counter1 = 0
while counter1 <= 6
puts "value of counter1 is : #{counter1}"
sleep(1)
counter1 = counter1 + 1
end
end
def function2
counter2 = 0
while counter2 <= 6
puts "The value of counter2 is : #{counter2}"
sleep(0.5)
counter2 = counter2 + 1
end
end
function1Thread = Thread.new{function1()}
function2Thread= Thread.new{function2()}
function1Thread.join
function2Thread.join
puts "End of the execution"
Output:
Conclusion
From these tutorials we learned the basic concept of Thread in Ruby, we learned about the working, initialization, and termination of the Ruby threads. Most importantly we learned about the real case uses of the Thread with the help of some examples and their outputs.
Recommended Articles
We hope that this EDUCBA information on “Thread in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.