Ruby's multi-threading

Source: Internet
Author: User

#Thread #1 is running Herethread. New {  #Thread #This  code} #Thread # This code

Thread.new is synonymous with Thread.Start and thread.for.

The value of the last expression in the code block is the value of the thread, which can be obtained by invoking the value of the thread object.

The thread.current represents the thread object for the current threads.

Thread.main is the main thread that represents the beginning of the Ruby program

Thread.Join blocking the current thread knows that the join thread has finished executing

Thread.abort_on_exception = True if there are unhandled exceptions exit the interpreter

Threads can store data that is part of a thread

thread.current['count'] = 0puts thread.current['count' ]

Thread Mutex

#!/usr/bin/rubyRequire'Thread'Mutex=Mutex.newcount1= Count2 =0difference=0counter=thread.new Do loop does mutex.synchronize do count1+ = 1Count2+ = 1End Endendspy=thread.new Do loop does mutex.synchronize do difference+ = (Count1-count2). ABS End Endendsleep1mutex.lockputs"count1: #{count1}"puts"count2: #{count2}"puts"difference: #{difference}"

Thread state

Thread State Return value
Runnable Run
Sleeping Sleeping
Aborting Aborting
Terminated normally False
Terminated Width exception Nil

# !/usr/bin/ruby  = thread.new do   #  calling a class method new puts "in second Thread"   raise
   
     "
    Raise exception
    "
    endthr.join   
    #
    
   

Ruby's multi-threading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.