1 / 10

Ruby - MultiThreading

Ruby - MultiThreading. Kennon Flint Chandler Programming languages. Thread.pass. The Thread.pass method invokes the thread scheduler to pass execution to another thread. a = Thread.new { print “ 1 ” ; Thread.pass; print ” 2 ” ; Thread.pass;

xuxa
Download Presentation

Ruby - MultiThreading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby - MultiThreading Kennon Flint Chandler Programming languages

  2. Thread.pass • The Thread.pass method invokes the thread scheduler to pass execution to another thread. • a = Thread.new { print “1”; Thread.pass; • print ”2”; Thread.pass; • print ”3”} • b = Thread.new { print “4”; Thread.pass; • print “5”; Thread.pass; • print ”6”} • a.join • b.join • Returns: 142536

  3. Thread.stop & thread_name.run • The Thread.Stop, puts the current thread into a “sleep” state • The run method starts the method it is passed to , back up, also known as “wakeup” • a = Thread.new { print “1”; Thread.stop; print “3” } • Thread.pass • print ”2” • a.run • a.join • Returns: abc

  4. Thread_name.join • The calling thread will suspend execution and run thread • The “a” thread will quit automatically, due to timing out • Any threads not joined will be killed when the main program exits • a = Thread.new { print “a”; sleep(10); print “b”; print “c” } • x = Thread.new { print “x”; Thread.pass; print “y”; print “z” } • x.join • Returns: axyz

  5. Thread_name.priority • Sets the priority of a thread to run, the higher the priority the more it runs, while the lower priority will still be performed • It may be ignored on some platform. • a = Thread.new do • loop { count1 += 1 } • end • a.priority = 1 • b = Thread.new do • loop { count2 += 1 } • end • b.priority = 3

  6. Thread_name.alive? • Returns true if thr is running or sleeping. • thr = Thread.new { } • thr.join • Thread.current.alive? Returns: true • thr.alive? Returns: false

  7. Thread.current • Returns the currently running thread and can be used to chain call methods instead of having to specify a thread. • Thread.current • Returns the assigned thread name, EX: #<Thread:0x24bb6086>

  8. Thread_name.raise • Raise is an error checking feature, if combined with an if statement. If used solely it will just raise an error, such as a string. • Using raise does throw an error in the runtime environment; however, the program will continue to run. • a = 5 • b = 10 • If a < b • thread_name.raise(“a is less than b”) • end

  9. Kill • The kill method can be called 2 different ways • Thread.kill(Thread_Name) • thread_name.kill • Terminates thread_name and schedules another thread to be run. • If this thread is already marked to be killed, exit returns the Thread.

  10. Thread_name.inspect • Returns the name, id, and status of thread_name to a string. • This is mainly used to check the status of a specific thread. • EX: #<Thread:0x24bb6086run>

More Related