Ruby | Thread Class-Public Class Methods Last Updated : 18 Sep, 2018 Suggest changes Share Like Article Like Report In Ruby, threads are used to implement concurrent programming module. Programs that required multiple threads, use the Thread class to create threads. Thread class contains a wide range of methods that perform some specified tasks. Public Class Methods abort_on_exception : This method returns the status of the global "abort on exception" condition. The default value of this method is false. If the value of this method is set to true, then it aborts all the threads in which an exception is raised. Thread.abort_on_exception -> true or false abort_on_exception= : This method returns the new state. When the value of this method is set to be true, then it aborts the threads in which exception arises. The return type of this method is boolean. Thread.abort_on_exception= bool -> true or false Ruby # Ruby program to illustrate # abort_on_exception Method Thread.abort_on_exception = true x = Thread.new do puts "Welcome to new thread" raise "Exception is raised in thread" end sleep(0.5) puts "Not Found" Output: Welcome to new thread test.rb:9: Exception is raised in thread (RuntimeError) from test.rb:6:in `initialize' from test.rb:6:in `new' from test.rb:6 critical : This method returns the global "thread critical" condition. Thread.critical -> true or false critical= : This method is used to set the status of global "thread critical" and return it. When the value of this method is set to be true, then it prohibits scheduling of any existing thread and it does not block the new thread from being created and run. Some thread operations like killing or stopping a thread, sleeping in the current thread, or raising an exception may cause a thread scheduled in a critical section. This method primarily supports folks writing threading libraries. The return type of this method is boolean. Thread.critical= bool -> true or false current : This method returns the current execution of the thread. Thread.current -> thread exit : This method is used to terminate the currently running threads and schedule another thread to be run. If this thread marked to be killed, then it returns the thread and if this is the main thread or last thread then it exits the process. Thread.exit fork : This method is similar to start method. Thread.fork{block} -> thread kill : This method is used to exit the thread. Thread.kill(thread) Example: Ruby # Ruby program to illustrate # kill Method counter = 0 # creating new thread x = Thread.new { loop { counter += 1 } } # using sleep method sleep(0.4) # exits the thread using kill method Thread.kill(x) # give it time to die! sleep(0.5) # return false x.alive? Output: false list: This method return an array of thread objects for all the threads either runnable or stopped. Thread.list -> array Example: Ruby # Ruby program to illustrate # list Method # first thread Thread.new { sleep(100) } # second thread Thread.new { 10000.times {|z| z*z } } # third thread Thread.new { Thread.stop } # using list method Thread.list.each {|thr| p thr } Output: #<Thread:0x8795838 sleep> #<Thread:0x87958e0 run> #<Thread:0x8795958 sleep> #<Thread:0x87a4f10 run> main : This method returns the main thread of the process. The program will always return different id's for every run. Thread.main -> thread Ruby # Ruby program to print the id # of main thread # using the main method puts Thread.main Output: #<Thread:0xbf04f18> new : This method is used to create and run a new thread to execute the instruction given in the block. Any argument passed to this method are passed in the block. Thread.new([arguments]*){|arguments|block} -> thread pass : This method attempts to pass execution to another thread, but the switching of the execution is depends upon the operating system. Thread.pass start : This method is similar to the new method. If the Thread class is subclassed then calling start from subclass will not invoke the subclass's initialize method. Thread.start([arguments]*){|arguments|block} -> thread stop : This method is used to stop the execution of the current running thread by putting it to sleep and schedule the execution of another thread, reset the critical condition to false. Thread.stop Example: Ruby # Ruby program to illustrate # stop Method x = Thread.new { print "geeks"; Thread.stop; print "geeksforgeeks" } # using pass method Thread.pass print "geeksforgeeks" x.run x.join Output: geeksgeeksforgeeksgeeksforgeeks Reference: https://ruby-doc.org/core-2.5.0/Thread.html#class A ankita_saini Follow 0 Article Tags : Misc Ruby Ruby-Multithreading Ruby-Built-in-class Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like