Ruby | Thread Class-Public Class Methods

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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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"

    chevron_right

    
    

    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
    
  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.
    Thread.exit
  7. fork : This method is similar to start method.
    Thread.fork{block} -> thread
  8. kill : This method is used to exit the thread.
    Thread.kill(thread)

    Example:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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?       

    chevron_right

    
    

    Output:

    false
    
  9. list: This method return an array of thread objects for all the threads either runnable or stopped.
    Thread.list -> array

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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 }

    chevron_right

    
    

    Output:

    #<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
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby program to print the id 
    # of main thread
       
    # using the main method
    puts Thread.main      

    chevron_right

    
    

    Output:

    #<Thread:0xbf04f18>
  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.
    Thread.new([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.
    Thread.pass
  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.
    Thread.stop

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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

    chevron_right

    
    

    Output:

    geeksgeeksforgeeksgeeksforgeeks
  15. Reference: https://ruby-doc.org/core-2.5.0/Thread.html#class



    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.