concept thread in category ruby

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.
Again, modules play a key role here, and they’ll get their due in chapter 4. For now, though, we’ll follow the thread of inheritance upward, so to speak, and look at the classes that appear at the top of the inheritance tree of every Ruby object: the Object and BasicObject classes.
Trying to read in some files... Message from thread: Failed on n=1 Finished!You can also stop and start threads and examine their state. A thread can be asleep or awake, and alive or dead. Here’s an example that puts a thread through a few of its paces and illustrates some of the available techniques for examining and manipulating thread state:
t = Thread.new do puts "[Starting thread]" #1 Thread.stop puts "[Resuming thread]" end sleep 1 puts "Status of thread: #{t.status}" #2 puts "Is thread stopped? #{t.stop?}" #3 puts "Is thread alive? #{t.alive?}" #4 puts puts "Waking up thread and joining it..." t.wakeup t.join #5 puts puts "Is thread alive? #{t.alive?}" #6 puts "Inspect string for thread: #{t.inspect}" #7Fibers: a twist on threads
In addition to threads, Ruby has a Fiber class. Fibers are like reentrant code blocks: they can yield back and forth to their calling context multiple times.
Ruby’s threads allow you to do more than one thing at once in your program, through a form of time sharing: one thread executes one or more instructions and then passes control to the next thread, and so forth. Exactly how the simultaneity of threads plays out depends on your system and your Ruby implementation. Ruby will try to use native operating system threading facilities, but if such facilities aren’t available, it will fall back on green threads (threads implemented completely inside the interpreter). We’ll black-box the green-versus-native thread issue here; our concern will be principally with threading techniques and syntax.
True parallelism means multiple processes ensure that two or more threads can always be running simultaneously. Creating threads in Ruby is easy: you instantiate the Thread class. A new thread starts executing immediately, but the execution of the code around the thread doesn’t stop. If the program ends while one or more threads are running, those threads are killed.
Here’s a kind of inside-out example that will get you started with threads by showing you how they behave when a program ends:
Thread.new do puts "Starting the thread" sleep 1 puts "At the end of the thread" end puts "Outside the thread"Thread.new takes a code block, which constitutes the thread’s executable code. In this example, the thread prints a message, sleeps for one second, and then prints another message. But outside of the thread, time marches on: the main body of the program prints a message immediately (it’s not affected by the sleep command inside the thread), and then the program ends—unless printing a message takes more than a second, in which case you need to get your hardware checked! The second message from the thread will never be seen. You’ll only see this:
Starting the thread Outside the threadNow, what if we want to allow the thread to finish executing? To do this, we have to use the instance method join. The easiest way to use join is to save the thread in a variable and call join on the variable. Here’s how you can modify the previous example along these lines:
t = Thread.new do puts "Starting the thread" sleep 1 puts "At the end of the thread" end puts "Outside the thread" t.joinThis version of the program produces the following output, with a one-second pause between the printing of the first message from the thread and the printing of the last message:
Starting the thread Outside the thread #1 At the end of the threadIn addition to joining a thread, you can manipulate it in a variety of other ways, including killing it, putting it to sleep, waking it up, and forcing it to pass control to the next thread scheduled for execution.