Home »
Ruby programming
Multithreading in Ruby
Ruby Multithreading: In this tutorial, we are going to learn about the multithreading in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on October 03, 2019
Ruby Multithreading
If we talk about a normal program in Ruby, all statements are executed sequentially because it used to have a single thread commonly known as the main thread. But our program may have more than one thread and this concept is known as multithreading. The advantage of multithreading is that our program uses less memory space and share the same address. With the help of this concept, we can perform more than one task at the same instant of time. We know that we can create a new thread with the help of thread.new keyword.
Now, let us see how we create multiple threads with the help of the examples provided below:
=begin
Ruby program to demonstrate multithreading
=end
def Includehelp1
a = 0
while a <= 10
puts "Thread execution: #{a}"
sleep(1)
a = a + 1
end
end
def Includehelp2
b = 0
while b <= 5
puts "Thread2: #{b}"
sleep(2)
b = b + 1
end
end
x = Thread.new{Includehelp1()}
y = Thread.new{Includehelp2()}
x.join
y.join
Output
RUN 1:
Thread execution: 0
Thread2: 0
Thread execution: 1
Thread execution: 2
Thread2: 1
Thread execution: 3
Thread execution: 4
Thread2: 2
Thread execution: 5
Thread execution: 6
Thread2: 3
Thread execution: 7
Thread execution: 8
Thread2: 4
Thread execution: 9
Thread execution: 10
Thread2: 5
RUN 2:
Thread execution: 0
Thread2: 0
Thread execution: 1
Thread2: 1
Thread execution: 2
Thread execution: 3
Thread2: 2
Thread execution: 4
Thread execution: 5
Thread2: 3
Thread execution: 6
Thread execution: 7
Thread2: 4
Thread execution: 8
Thread execution: 9
Thread2: 5
Thread execution: 10
In the above example, you can observe that both the outputs are different even though there is no change in the program. This is because the execution of the thread is carried out based on their priority and by default, all the threads have equal priority. Their execution is dependent on the pointer. You can also observe that both the threads are executing at once.
Threads can also use the same resource in the program, refer to the following example,
=begin
Ruby program to demonstrate multithreading
using same resource
=end
$g_var = 10
def Includehelp1
a = 0
while a <= 10
puts "Thread execution: #{a}"
sleep(1)
a = a + 1
end
puts "Inside Thread 1 ; #{$g_var+19}"
end
def Includehelp2
b = 0
while b <= 5
puts "Thread2: #{b}"
sleep(2)
b = b + 1
end
puts "Inside Thread 2 ; #{$g_var+10}"
end
x = Thread.new{Includehelp1()}
y = Thread.new{Includehelp2()}
x.join
y.join
Output
Thread execution: 0
Thread2: 0
Thread execution: 1
Thread execution: 2
Thread2: 1
Thread execution: 3
Thread2: 2
Thread execution: 4
Thread execution: 5
Thread2: 3
Thread execution: 6
Thread execution: 7
Thread2: 4
Thread execution: 8
Thread execution: 9
Thread2: 5
Thread execution: 10
Inside Thread 1 ; 29
Inside Thread 2 ; 20
In the above code, you can observe that there exists a global variable name as $g_var. Both the threads can access it but when they are manipulating its value, it is not creating any impact on the value manipulated by other threads.