what is mean by thread scheduler in Ruby?

V

Vellingiri Arul

what is mean by thread scheduler in Ruby?with example?


Please Any one help me?

by
Vellingiri
 
7

7stud --

Vellingiri said:
what is mean by thread scheduler in Ruby?with example?


Please Any one help me?

by
Vellingiri

Threads don't execute at the same time despite what the literature might
say. If you have two threads, for instance your main program and one
thread, then one thread executes for a short time, and then execution
switches to the other thread for a short time, and then execution
switches back. The switching back and forth happens so fast that it
gives the appearance that the threads are executing at the same time.
The thread scheduler determines which thread gets execution time and how
much.

You should note that if both of your threads contain code that has no
dead time or pauses in it, your program will take the same amount of
time to execute without threads as it does with threads. It's only when
code has some pauses in it, like if your program has to wait for a
requested web page to be returned, that threads can reduce the time your
program takes to execute. During the pauses, the thread scheduler will
switch execution to another thread where there is code ready to execute.

You can manually manipulate the thread scheduler using methods like
wait(), join(), stop(), and pass(). All those methods cause the thread
scheduler to alter its normal behavior.

puts "Inside main program."
sleep(2) #execution halts here - no thread yet
puts

t = Thread.new do
puts <<ENDx
Inside thread, but Thread.pass tells thread scheduler to go
elsewhere and look for code that is ready to execute.
ENDx
Thread.pass #Tells thread scheduler to go elsewhere
#and look for code to execute.

puts
puts "Inside thread."
puts
sleep(12) #Because execution halts here, the thread scheduler
#will look elsewhere for code to execute.
puts "Inside thread: thread finished."
end

puts
puts <<ENDy
Back inside main program. Halting main program. Send
execution back to thread and give it all the execution
time it needs until it finshes.
ENDy
t.join()

puts
puts "Inside main, thread finally finished, exiting..."
 
C

Charles Oliver Nutter

7stud said:
Threads don't execute at the same time despite what the literature might
say. If you have two threads, for instance your main program and one
thread, then one thread executes for a short time, and then execution
switches to the other thread for a short time, and then execution
switches back. The switching back and forth happens so fast that it
gives the appearance that the threads are executing at the same time.
The thread scheduler determines which thread gets execution time and how
much.

Some time ago I wrote an article on RubySpec about Ruby's threading
implementation; if there's anything to add to that, please do.

http://headius.com/rubyspec/index.php/Ruby_Threading

There's also an article on JRuby's threading, which is native,
concurrent, and just allows the OS to schedule.

http://headius.com/rubyspec/index.php/JRuby_Threading

- Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,850
Latest member
Brightrs

Latest Threads

Top