A Detailed Guide on How to Use Ruby Threads

A Detailed Guide on How to Use Ruby Threads


Ruby is a language of marvels; other than Ruby gems (for automated functionalities) and Ruby metaprogramming (for enhanced code efficiency), it unravels its Ruby threads to facilitate rapid development practices.

The threads in Ruby make your program do multiple things at the same time, enhancing its capabilities to rapidly cover more ground.

However, Ruby threads might turn out to be agonizing if not used correctly. 

So, using Ruby threads examples, let’s thoroughly understand this methodology and see what it is, how it works, why it’s useful, and how to use it.

What is a Thread in Programming Terminology?

Before we move ahead to the concept of Ruby threads, let’s first understand what a thread is.

A thread is the smallest set of instructions designed to be scheduled and executed by the CPU cores independently of the parent process.

For instance, an open thread waits for a specific event to take place or runs a different process, allowing the main program to perform other tasks.

Threading is used to run code sequences and other data-based structures in parallel, such as opened resources, memory maps, stacks, and so on, to make programming easier. 

It takes advantage of multiple CPU cores to run multiple processes or multiple threads within one process.

Related Post: The Best Ruby Frameworks for Web Development and Business Growth in 2023

Single Thread Vs Multithread

The processes of single threads contain the executions of instructions in a single sequence, or, to pitch it a bit differently, one command process at a time.

Whereas multithreading allows different parts of the program to run concurrently, these are lightweight and available in the process.

Related Post: Ruby on Rails VS Node JS: An in-depth comparison 

Ruby Threads

Ruby threads are units of execution, they allow developers to create codebases that could potentially write codes and execute multiple programs at once. 

For instance, if we were to perform addition and subtraction tasks in a program at the same time using traditional methods, then it’s not possible since a program and an interpreter could only handle one task at a time.

However, with the use of Ruby threads, we can perform such tasks and execute multiple results at the same time.

This gives us the benefit of maximizing available resources and concurrency within the process.

Related Post: Top Reasons to Migrate from Monolithic Apps to Ruby on Rails Microservices

Ruby’s Multi-threading Nature

Multithreading is one of the most appealing properties of Ruby. It allows concurrent programming of multiple parts of the program to maximize the number of CPU cores and their utilization.

Here each program is known as the “Ruby thread,” or, in other words, they are lightweight processes within the process.

Unlike traditional threads, Ruby multi-threading consumes less memory, which makes it efficient at executing tasks. 

If a program contains a single thread, then it’s bound to execute its instructions sequentially, but if it contains multiple threads, then every thread within each thread will execute instructions sequentially, and the thread itself will execute concurrently on the multi-core processor.

However, there are other approaches—referred to as Ruby fibers—that function in a manner similar to that of Ruby threads.

Tip: Make the best Ruby on Rails Development Company your technology partner to build seamless applications.

The Concept of Ruby Fibers

In Ruby There Are Three Ways To Implement Concurrency

  1. Processes
  2. Threads
  3. Fibers

The processes are a bit harsh; they require more memory as compared to threads and fibers, so putting them against these two won’t be a wise decision.

Also, we have already seen the basics of Ruby threads, which leaves us with the fibers.

Similar to threads, fibers are units of execution.

They run code bases and track their progress. Similar to threads, they are a concurrency mechanism but differ in nature when compared with threads. 

It’s possible that the control flow of a thread can get interrupted at any instance and another thread can take that place.

With fiber, the control only switches when we explicitly command it.

Related Post: 5 Best Ruby IDEs for Ruby on Rails Development in 2023

Ruby Threads Vs Fibers

  • >  The fibers are lightweight and consume less power compared to threads.
  • > The initialization and passing of the threads at a given time are decided by operating but with fibers, we command when to pause, and resume manually.
  • >  Threads can do their job in the background, but whenever you initiate a fiber, it becomes the main program before you try to interrupt it.

Related Post: Ruby on Rails vs Laravel: Which Framework is Better for Web Development

Why use Ruby Threads?

As we said earlier, Ruby threads are useful to execute more than one task at the same time; for instance,

  • > Interpreting multiple files
  • > Handling more than one web request
  • > Creating multiple API connections

Above all, with the help of the Ruby threads, the developers can reduce the time required to build, test, and deploy the Ruby on Rails applications.

How Do Ruby Threads Work?

The thread is a concept where two statements function in parallel, and the Ruby threads are much different than Java’s because they operate inside their interpreter.

This means Ruby threads can work independently without depending on OS resources, making it portable for running on any other operating system.

The work function of Ruby is solely based on the life cycle method, so let’s get a thorough understanding of each step.

  1. Create a thread using Thread.start or Thread.fork or new thread
  2. This thread can now run automatically since the resources are made available by the CPU
  3. Using the code block Thread.new, the thread can run the code in the block and then stop it. There are multiple ways to handle, manipulate and get information out of running threads.
  4. Ruby also offers some crucial techniques for the Thread in Ruby like the current( it contains the current thread’s objects and when called it returns its objects ). Other than this there is another method known as main in the Thread which includes the object that represents the main thread when called Thread.main method.
  5. The beauty of Ruby lies in a feature where we can put a wait on a thread to complete by calling a method called “join.” If we were to make a call join then it will block until this thread completes.

When are Ruby Threads Helpful?

Ruby threads are extremely helpful since they allow you to save a lot of computer memory on processes, especially if they are multiple threads that share a single instance of Rails applications.

Multiple processes require their own copies. If you want to run 20 threads and the application uses over 100 MB of memory, then the threads will save you more than 800 MB or more of memory space, which is really great.

In Case You Are Using MRI/CRuby Interpreter-

There are some considerations. If you are using the former Ruby interpreter MRI/CRuby, then you might not get the opportunity to get hands-on experience with Ruby multithreading since it uses the GIL (Global Interpreter Lock).

The presence of GIL when using threads simply means that no matter how many CPUs and cores you have, at the time, there will only be one core and one thread that will run within your Ruby processes.

In other words, if you have a task that requires a lot of computation, such as a large set of calculations, you can’t get the results by dividing the calculation parts into different threads. It will work like you programmed that calculation task to run in a single thread. This is a downside of the former Ruby interpreter. 

A detailed guide on how to use Ruby threads- single thread task execution

However, if you have a long-running task waiting on any external resources, such as a database, things operate differently.

For the moment, suppose you have to run an N number of SQL queries. In Ruby, whenever a thread is on hold/ waiting for a response from the database, it passes or yields its control over to another thread.

This thread now assembles and performs a bit different query and again waits for the responses and between that time the first thread receives a response from the database and continues on its path.

This is a perfect example for speeding up execution using Ruby threads. With the use of Ruby multi-threading, we can allot one thread for working while another can wait for the response from the database.

Is Ruby Multithreaded or Single Threaded? The Question Arises

As we saw above, Ruby almost fails to perform multithreading. It’s natural that you will wonder whether it’s multithreaded or single-threaded. 

Ruby is almost multithreaded, but not fully. And the following points will answer this question.

In System Kernel,

The GIL is a lock; unless it is released, no other process than the main can be processed.

But things change miraculously in the system kernel. If the MRI or Cruby needs to perform I/O, it communicates directly with and calls the kernel for that part, and GIL does not apply in this kernel space. This means that once the thread request is sent to the kernel, Ruby’s GIL is released and another thread can process.

So if you are planning only I/O operations, then MRI/CRuby might not affect Ruby multi-threading.

In Case You Are Using Rubinius or JRuby Interpreter

It’s a win-win situation if you use Rubinius or JRuby interpreter for your Ruby on Rails development since they don’t have GIL built in. Thus, multi-threading can be broadly useful here, unlike in MR/CRuby where you only get to work with multi-threading and Ruby classes in performing I/O in the kernel. 

How to Use Ruby Threads

Now let’s jump on to the main point, how you can use Ruby threads to drive efficiency and speed in your Rails application.

Creating and Initializing Ruby Threads

First off, let’s create a Ruby threads example using a new thread and then initialize it with Thread.new 

A detailed guide on how to use Ruby threads- Ruby threads example

And then you might notice in the following code there is no output from that particular thread,

A detailed guide on how to use Ruby threads- Ruby threads example

The reason for this is that Ruby waits for the threads to finish.

However, you can call the join method on the thread to fix the above code.

You can also create multiple threads by putting them inside an array and calling join on every thread.

A detailed guide on how to use Ruby threads- Ruby threads example

Ruby Threads and Exceptions

If the exception happens inside a thread, it dies without stopping your program or showing any sort of error.

Take this for example

A detailed guide on how to use Ruby threads- Ruby thread example

For debugging, if something bad happens, you may want to stop your program. For that you can set the following flag on Thread to true:

Ruby Thread Pools

Let’s take a Ruby thread pool example. Suppose you have hundreds of items to process. Enabling a thread for each of them may destroy your system’s resources. 

It looks like this.

If this is done, then you will be unleashing a number of connections against the server, which is not something you should do.

However, one solution is Ruby thread pools.

It allows you to control the number of active threads at any given time.

You might think of creating your own pool, but we highly suggest not doing so. Instead, here we have used the Celluloid gem to give you an example, although it’s now unmaintained, the general idea of workers still remains the same.

A detailed guide on how to use Ruby threads-Ruby threads example

This time only 5 threads will rub, and as they finish the first, they will pick the next item.

This is how you can use Ruby threads to bring more efficiency and speed to your Ruby on Rails program.


Implementing concurrency is crucial in Ruby to execute multiple tasks at the same time and facilitate rapid development.

Ruby Threads are among these methods, which follow the concurrency mechanism. And Ruby threads’ performance goes beyond that of regular processes, as it handles multiple tasks at once and utilizes the full capacity of CPU cores.

You can rely on Rubinius and JRuby for full-fledged Ruby multithreading, as MRI/CRuby are only compatible when you have I/O processes.

However, it’s important to understand when to use Ruby threads and bring ease to that block of the program. This article does the same thing with the Ruby threads example to help you understand this methodology from the ground up.



Subscribe for newsletter

Trusted By

Our platform focuses on making the remote hiring process easier by providing top quality vetted developers from around the world. Through our service many well-known companies have scaled their product development team.

Related Article

Ready to scale your team with remote engineers?