Create Thread

6 minutes
Share the link to this page
Copied
  Completed
Learn how to create a thread using a more dynamic class system.

Transcript

Hello, in this video, what we're going to do is extend what we've already got right now. So if you don't have this, feel free to go back to the previous video on initiating a new thread. So we're going to extend this. So we have our own class that allows us to create our own threads manually. This is the new system of doing things. So first of all, we need to import the threading module of the old underscore thread module.

So for the Define time function, we'll make a few modifications to that we need to have the class here. So let's do time from first when I sort of already done so Fred name, delay. Now let's specify cancer as well to make it a bit more dynamic. And what we're going to do while And I haven't seen a flag here just bear with me I'll do it by default will be zero. If you know exit flag, then we're going to do red got tons the delay that for printing it out like so is so fine. The counter, I will do is do because we're doing the opposite we are specifying the function.

So this is going to change as well but before we change that, we need to create our thread class ethics thread Never say Fred in a sense or is it makes me you know, in my head, I'm hearing Fred at our end, you know the name threading and in here, we're gonna define the init method. If any of this is a bit confusing regarding the like init method itself, you would prefer to go to classes videos, and check them. All you do, counter and in here, threading dot thread underscore in it in the school. So self dot thread ID equals red ID In the self dot name, folders keeping track of all of this within the class allows us to you know, have a more dynamic way of keeping track of it, creating more threads and just using them. So it's just a better management classes than what we did in the previous video.

Presumably method one self dot name, so you just print out Fred, we're actually starting with a call a time function from here, the time function. So name and self dot counter, and free. So this is how we talk One to One you could have been five even 10. In the previous video we do yes five executives. And so if we said that we go down we need to mainly get rid of this Can we go out on thread naza we're not going to use a start new thread method. Thread one equals epic for creating a new thread object here and specify one like so.

So this just be the thread. What we're gonna do is now we go to specify. To specify the thread ID you need to specify the name and the counter. So the name is Come on counter. No need to put Jones so far no errors so starting from one form to execution So, this actually created a more dynamic thread class that we can extend over the next couple of videos where we will be synchronizing thread and having multi threaded priority queues. So feel free to go over this again into any questions you have, feel free to reach out and I'll help you.

And as usual, thanks for watching, and I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.