Open In App
Related Articles

Ruby | Thread Class-Public Class Methods

Like Article
Save Article
Report issue
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

  1. 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
  2. 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 program to illustrate 
    # abort_on_exception Method
    Thread.abort_on_exception = true
    x = do
    puts "Welcome to new thread"
    raise "Exception is raised in thread"
    puts "Not Found"
    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
  3. critical : This method returns the global “thread critical” condition.
    Thread.critical -> true or false
  4. 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
  5. current : This method returns the current execution of the thread.
    Thread.current -> thread
  6. 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.
  7. fork : This method is similar to start method.
    Thread.fork{block} -> thread
  8. kill : This method is used to exit the thread.
    # Ruby program to illustrate 
    # kill Method
    counter = 0
    # creating new thread
    x = { loop { counter += 1 } }
    # using sleep method
    # exits the thread using kill method
    # give it time to die!
    # return false
  9. list: This method return an array of thread objects for all the threads either runnable or stopped.
    Thread.list -> array
    # Ruby program to illustrate 
    # list Method
    # first thread { sleep(100) }
    # second thread { 10000.times {|z| z*z } }
    # third thread { Thread.stop }
    # using list method
    Thread.list.each {|thr| p thr }
    #<Thread:0x8795838 sleep>
    #<Thread:0x87958e0 run>
    #<Thread:0x8795958 sleep>
    #<Thread:0x87a4f10 run>
  10. 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 program to print the id 
    # of main thread
    # using the main method
    puts Thread.main      
  11. 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.[arguments]*){|arguments|block} -> thread
  12. pass : This method attempts to pass execution to another thread, but the switching of the execution is depends upon the operating system.
  13. 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
  14. 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.
    # Ruby program to illustrate 
    # stop Method
    x = { print "geeks"; Thread.stop; print "geeksforgeeks" }
    # using pass method
    print "geeksforgeeks"
  15. Reference:

    Last Updated : 18 Sep, 2018
    Like Article
    Save Article
    Share your thoughts in the comments
Similar Reads