Home
Username:
Password:
Programming With Ruby Tutorials

Threading in Ruby / How to Create & Use Threads




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

In this video we're going to take a look at how to implement a thread in Ruby. Now I have a simple example here called counter dot rb available in the working files. We have a simple class I just called printer. It has one attribute that you can read. That's count. We initialized count to zero when we initialize our class and we have one method call, which is a print method that we give a caller and that caller gets printed out and then when we call that, we actually give it a caller, we print that out and then we increment that count up one. So I'm going to use these two threads to essentially compete with each other. So we go ahead and create a new object so I can go ahead and utilize it. Then I create a thread and this is essentially how you create a thread. You give the call thread dot new and then you give it a process for it to run. And this process is a hundred times it's going to call the method with T2. And this thread down here is the same thing except that the caller has a different name, T1. So to think about it, if we want, let me replace this real quick here. So you can see exactly what's going on here. I'm going to comment these two out for the moment and I'm not going to use an object. What I'm going to actually do is just have it a hundred times Ok. So you can see all that it did was it called that thread and printed that out. Now, we could ditch these two for a second. We could do a very simple thread like that. Run that one. I need to get that output here. Ok. There we go. So all we did was count up and you see it's doubled up and it's because of how I have this, this put here. Its just going to throw them together like that. I can put a space on the end here I guess. Let me see if that makes a difference here. No. Let me put a space in there so at least that makes it different. There we go. So you can see, there we go, it just counts up and it's pretty much I called the one thread and it runs and then the other thread gets run. But that's not an absolute guarantee that that's going to happen all the time and I've managed to get this a few times to show you essentially a hiccup or it switches over and T1 becomes first and I have to run it a few times to get that to happen. It didn't do it that time. No. Doesn't look like it's going to do it. But I've had that happen where it switched that ordering. So you kind of have to be careful with assuming that you can determine which thread's going to run. Something might happen in there that it, it switches its ordering. Something in the OS or something along those lines. So there's more advanced techniques like using mutual exclusion flags and stuff to actually force a particular thread to wait for another thread, but we aren't going to cover here. This is just the basics of how to create a thread and get it running. So we call thread dot new and then give it our block inside there. You could also use the do structure where you have do and then some code and then end all. Or, like I did here, is inside here I actually had a object that I issued a call to and I had a blocker on that. So you can wrap a lot of things inside of each other here. So that's how you implement simple threads in Ruby.

Tutorial Information

Course: Programming With Ruby
Author: Al Anderson
SKU: 33788
ISBN: 1-934743-01-1
Release Date: 2007-08-21
Duration: 8.5 hrs / 113 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available