[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what is mean by thread scheduler in Ruby?

Vellingiri Arul

9/28/2007 5:53:00 AM

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


Please Any one help me?

by
Vellingiri
--
Posted via http://www.ruby-....

2 Answers

7stud 7stud

9/28/2007 7:09:00 AM

0

Vellingiri Arul wrote:
> 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..."

--
Posted via http://www.ruby-....

Charles Oliver Nutter

9/28/2007 10:57:00 AM

0

7stud -- wrote:
> Vellingiri Arul wrote:
>> 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.

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...

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...

- Charlie