Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Threads and Concurrency
3.
Threads and Platform Dependencies
4.
Lifecycle of a Thread
4.1.
The main thread
4.2.
Threads and unhandled exceptions
5.
Threads and Variables
6.
Thread Scheduling
7.
State of Thread
8.
Frequently Asked Questions
8.1.
What do Ruby threads do?
8.2.
Which editors provide support for Ruby?
8.3.
What does Ruby's concurrency mean?
8.4.
What are the reference best books for ruby?
8.5.
What in Ruby is a mutex?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Threads and Concurrency in Ruby

Introduction

The smallest group of instructions an operating system scheduler can independently manage is called a thread of execution in computer science. A thread is a delicate procedure.

Thread in Ruby

A thread is a delicate procedure. All of its threads share the resources of a process. This is why, in some circumstances, using threads is more cost-effective.

Recommended Topic, procedure call in compiler design

Threads and Concurrency

Computing-intensive software, like image processing software, is called compute-bound. They can only profit from multithreading if several CPUs are available to perform computations concurrently. However, most programmes are not entirely compute-bound. Many often wait for network or file I/O, such as web browsers. These kinds of programmes are referred to as IO-bound. Multithreading can be helpful for IO-bound programmes even when only one CPU is available.

One web browser thread may render an image while another waits for the following image to be downloaded from the network.

Ruby's Thread class makes it simple to create multi-threaded programmes. Just pair a block with a call to Thread.new to begin a new thread.

A new thread will execute the code in the league, and the original thread will immediately return from Thread. New and resume execution with the following statement:

# Thread #1 is running here

    Thread.new 
    {
		# Thread #2 runs this code
    }
                
# Thread #1 runs this code

Threads and Platform Dependencies

Different operating systems handle threads in various ways.

Furthermore, there are multiple ways that different Ruby implementations layer Ruby threads on top of operating system threads. For instance, Ruby 1.8's standard C implementation only employs one native thread, which executes each Ruby thread individually. As a result, even on multicore CPUs, Ruby 1.8 threads are incredibly lightweight but never managed simultaneously.

For each Ruby thread, Ruby 1.9 now allocates a native thread. However, some of the C libraries in this implementation are not thread-safe.

Ruby

Ruby 1.9 is very cautious and permits one of its native threads to be active simultaneously. If it is possible to make the C code thread-safe, this restriction might be eased in subsequent 1.9 releases.

Each Ruby thread is converted into a Java thread by JRuby, the Java implementation of Ruby. However, the performance of the Java virtual machine, in turn, affects how Java threads are implemented and behave. Java threads are typically implemented in modern Java implementations as native threads, enabling accurate parallel processing on multicore CPUs.

Dependencies

Lifecycle of a Thread

Thread.new is used to start new threads, as previously mentioned. The antonyms Thread.start and Thread.fork are also acceptable. After being created, a thread doesn't need to be activated; it starts running when CPU resources are made available. A Thread object is returned by the Thread.new invocation. The Thread class defines numerous methods to query and control the thread while it is active.

After executing the code in the block connected to the call to Thread.new, a thread terminates itself. Calling the value method of the Thread object will return the thread value, which is the value of the final expression in that block.

The deal immediately returns the thread's value if the thread has completed running. If not, the value method becomes blocked and does not release until the thread is finished.

The class method Thread returns the Thread object representing the current thread.current. Threads can now manipulate themselves as a result. The Thread object representing the primary thread—that—is returned by the class method Thread.main.

The main thread

The following method waits until all threads have terminated, excluding the main thread and the current thread (which might be the same thing):

# Wait for all running threads to stop (aside from the current thread and the main thread).
# assumes that no additional threads will be started while you're waiting.

    def join_all
        main = Thread.main # The main thread
        current = Thread.current # The current thread
        all = Thread.list # All threads still running
        
#  Call join on each thread
        all.each {|t| t.join unless t == current or t == main }
    end

Threads and unhandled exceptions

The Ruby interpreter prints a message and quits if an exception is thrown in the main thread but not handled anywhere. Unhandled exceptions in threads other than the main thread result in the thread's termination. However, by default, this has no effect on the interpreter printing a message or quitting. The exception that occurred in thread t is raised in thread s if thread t exits due to an unhandled exception and thread s calls t.join or t.value.

Exceptions

Threads and Variables

The ability of threads to share access to variables is one of their key characteristics. Since blocks define threads, they have access to all variables (local variables, instance variables, global variables, etc.) that fall under the block's purview:

  1. Thread-private variables
  2. Thread-local variables

Thread Scheduling

Ruby interpreters frequently need to run more threads than there are CPUs available to do so. When true parallel processing cannot be achieved, it is simulated by having multiple threads share a single CPU. Thread scheduling refers to the process of dividing a CPU among various threads. Thread scheduling may be handled by the Ruby interpreter or by the underlying operating system, depending on the implementation and platform.

  1. Thread priorities
  2. Thread preemption and Thread.pass

State of Thread

One of five possible states is possible for a Ruby thread. Live threads can be in two states, runnable or sleeping, which is the two most intriguing states. A thread that is currently running or that is prepared and qualified to run the following time there are CPU resources available said to be a runnable thread. A sleeping thread has stopped itself, is waiting for I/O, or is sleeping (see Kernel.sleep) (see Thread.stop below). The runnable and sleeping states of threads frequently alternate.

For threads that are no longer active, there are two thread states. A thread that has ended has either ended usually or, with an exception, has terminated abnormally.

One final transitional state is present. A thread is said to be aborted if it has been killed (see Thread.kill below) but hasn't yet terminated.

Thread and return value

Frequently Asked Questions

What do Ruby threads do?

The most advantageous feature of Ruby is multi-threading, which enables concurrent programming of two or more programme components for maximum CPU efficiency. Thread refers to each element of a programme. In other words, threads are quick processes contained within more extensive processes.

Which editors provide support for Ruby?

The editors that support ruby are Emacs or XEmacs, Vim (version 5.7 or later), Jedit, Nedit, Atom editor, Sublime Text, Visual Studio code, etc. 

What does Ruby's concurrency mean?

Ruby concurrency specifically refers to the ability for two tasks to start, run, and finish concurrently. However, it doesn't necessarily follow that they'll ever be moving forward simultaneously (e.g., multiple threads on a single-core machine).

What are the reference best books for ruby?

The best books for ruby are Programming Ruby: The Pragmatic Programmer’s Guide by David Thomas and Andrew Hunt, The Ruby Programming Language by Matz et al., and Ruby Pocket Reference by O’Reilly

What in Ruby is a mutex?

A class called Mutex implements a straightforward semaphore lock to allow for mutually exclusive access to a shared resource. In other words, only one thread can hold the lock at once.

Conclusion

We have discussed the concept of Threads and Concurrency in Ruby. If you have any doubt, you can ask in a comment, or you may prefer the below-mentioned blogs.

Introduction to Ruby

Ruby is an object-oriented language

Hey Ninjas! We hope this blog helped you better to understand the java Vs ruby concept. Please check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Thank You

Live masterclass