110 likes | 261 Views
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;
E N D
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; • print ”3”} • b = Thread.new { print “4”; Thread.pass; • print “5”; Thread.pass; • print ”6”} • a.join • b.join • Returns: 142536
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
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
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
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
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>
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
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.
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>