Thread means lightweight sub-process. It is a separate path of execution. In Ruby, different parts of a program can run at the same time by either splitting tasks within a program using multiple threading or splitting tasks between different programs using multiple process.
Threads are the Ruby implementation for a concurrent programming model.
A normal program has single thread of execution. All the statements in the program are executed sequentially.
A multi thread program has more than one thread of execution in it. It uses less memory space and share same address space. Multithreading is used to perform more than one task at once.
A new thread is created using thread.new call. It is different from the main thread's execution.
To create a new thread Ruby provides three keywords namely, ::new, ::start and ::fork.
To start a new thread, associate a block of code with a call to Thread.new, Thread.start or Thread.fork. Thread will be created. The new thread exits when the block exit.
There are different ways to terminate a thread in Ruby. To exit a given thread, class ::kill is used.
Ruby Thread Example
Once a thread is created, there is no need to start it. It automatically runs when it gets proper CPU resources. The last expression in a block is the value of the thread. If thread has run completely, value method returns the thread value, otherwise value method blocks it and returns when the thread has completed. A number of methods are defined by thread class while running query and manipulate the thread.
By calling a thread's Thread.join method, you can wait for a particular thread to finish.
Thread Exception Handling
Threads may have some exceptions in them. If exception arises in any thread other than main thread, it depends upon abort_on_exception. By default this option is always false. It means unhandled exception will silently terminate the thread. This can be changed by setting either abort_on_exception = true or $DEBUG to true.
To handle exception, you can use class method ::handle_interrupt. It will handle exceptions asynchronously with threads.
Thread Variables and Scope
Threads are created with blocks. A local variable created within a block is accessible to only thread in which this block is present.
Ruby thread class allows thread-local variables to be created and accessed by name. Thread object is treated like a hash, writing elements using = and reading them back using .
Ruby supports scheduling threads by using ::stop and ::pass methods in a program.
The ::stop class method put the current running thread to sleep and schedule the execution of another thread. Once the thread is asleep, instance method wakeup is used to mark thread as eligible for scheduling.
The ::pass class method tries to pass execution to another thread. It depends upon the operating system whether the running thread will switch or not.
Thread priority gives a hint to schedule threads according to their priority. The high priority thread is scheduled first. It also depends upon the operating system. A thread can increase or decrease its own priority as the first action it takes.
Ruby thread exclusion states that, when two threads share the same data and one of the thread modifies that data, then we need to ensure that no thread should see each others data in an inconsistent state. For example, banking server. Where one thread operates money transfer in accounts and other thread is generating monthly report for the customers.
Public Class Methods
Public Instance Methods