×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Threads in Ruby

By IncludeHelp Last updated : December 02, 2024

Ruby Threads

In Ruby, with the help of threads, you can implement more than one process at the same time or it can be said that Thread supports concurrent programming model. Apart from the main thread, you can create your thread with the help of the "new" keyword.

Syntax

Threads are light in weight and you can create your thread with the help of the following syntax:

Thread_name = Thread.new{#statement}

Creating a Thread

Now, let us see how we can create a thread in Ruby with the help of a supporting example?

Example

=begin
Ruby program to demonstrate threads    
=end

thr = Thread.new{puts "Hello! Message from Includehelp"}
thr.join

Output

Hello! Message from Includehelp

In the above example, we have created a thread named as thr. Thr is an instance of Thread class and inside the thread body, we are calling puts method with the string. thr.join method is used to start the execution of the thread.

Calling User-defined Method inside a Thread

Now, let us create a user-defined method and call it inside the thread in the following way,

Example

=begin
Ruby program to demonstrate threads
=end

def Includehelp1 
a = 0
while a <= 10
    puts "Thread execution: #{a}"

    sleep(1) 

    a = a + 1
    end
end

x = Thread.new{Includehelp1()} 
x.join

Output

Thread execution: 0
Thread execution: 1
Thread execution: 2
Thread execution: 3
Thread execution: 4
Thread execution: 5
Thread execution: 6
Thread execution: 7
Thread execution: 8
Thread execution: 9
Thread execution: 10

In the above code, we are creating an instance of Thread class names as x. Inside the thread body, we are giving a call to the user-defined method Includehelp1. We are printing the loop variable for 10 times. Inside the loop, we have accommodated the sleep method which is halting the execution for 1 second.

Using .join Method

We are starting the execution of thread with the help of the join method

Example

=begin
Ruby program to demonstrate threads    
=end

def myname 
    for i in 1...10
        p = rand(0..90)
        puts "My name is Vaibhav #{p}"
        sleep(2) 
    end
end

x = Thread.new{myname()} 
x.join

Output

My name is Vaibhav 11
My name is Vaibhav 78
My name is Vaibhav 23
My name is Vaibhav 24
My name is Vaibhav 82
My name is Vaibhav 10
My name is Vaibhav 49
My name is Vaibhav 23
My name is Vaibhav 52

In the above code, we have made a thread and printing some values inside it. We are taking help from the rand method and sleep method.

By the end of this tutorial, you must have understood the use of thread class and how we create its objects.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.