Hello, in this Java programming video, we are going to take a look at using Fred. So Java is a multi threaded programming language. Awesome. I'm going to implement it using a runnable interface. So this is so this is great if you want to create a class that when executed, it's run, you know a thread. So to do that, not to audit also it's going to create an example class.
So let's create a new one. Let me create a new package First, I'll call it so now I'm going to create a new class. And in here, I'm going to name this thread runnable is going to be using the runnable interface. Okay, there we go. So that's pretty cool. Cuz it's an interface is done it implements, implements runnable.
Add unimplemented methods, there are certain methods that we haven't implemented yet. And run is, you know, the main one. Okay, so we'll actually implement them momentarily. The first thing we're going to do is create a couple of variables on the thread. thr. The other one I'm going to create is going to be a thread named just call it name like so.
And we are going to create a constructor and we're going to pass in a string like so and we are going to assign this name to the Once that we just mentioned this dot name, equals name like so, then we'll just do a quick system, System dot out dot print ln. And in here, we'll just say the Fred will put name like so. And now we can implement the run method. So in here, we are just going to do something kind of similar to this. If we copy that, instead of saying the Fred cord has been created, and it is being run Now we're going to do a try catch block to try to look at x in here in here yet because I haven't done anything yet, I mean the Troy block book that will disappear in seconds the system the system they'll say, so say name Fred.
Interrupted. Okay, so now let's actually implement this I will just do a for loop for our for greater than zero on Monday morning, so we're going to tick them. And then we are going to system dot print ln. And we are going to thread pay. So we'll show that and then we'll make use of red dot sleep so we can see that there's a bit of a time difference and we'll talk 100 milliseconds so September 2, not long at all. Wonderful nothing in here.
We are just gonna do a system dum dum to say that the Fred is not stopping. So we're gonna say name plus and now we just need to create a method for start so public void. And in here we are going to the system dot out. dot print ln. Like so. I was gonna say no.
So if the Fred doesn't exist, then we want to create a new Fred So then we want to do th o equals new bread. I'm gonna pass in there like so. That's only if the new if they even afraid it already, then you find that we don't need to actually do something though we know we need to start the fight again basically. So if I saved up I know in my solar system, the Java file want to do import so no dot read runnable in here, I'm going to create the thread runnable and I'm going to call it thr one is going to be equal to new thread run the wall And the name of the thread will be awesome. Okay, so this Okay, there we go. And then we just do th o one.
And what we'll do create one more fret just to show you more than one of the most likely going to need what I'm going to say. So if we run this, you won't get Okay, so what happened was they said the Fred chord. Awesome. Fred is being created. That's cool. So it's from this constructor.
Start in the Fred chord. Awesome. Fred from this method, different called crazy credit being started. So this one, and he started this thread. And he said the Fred core awesome Fred is being run, Fred, awesome, Fred for the Fred chord crazy Fred is being run. And as you can see it was alternating between them because we had a sleep.
So that way you can like set a priority. So if something in the application isn't as important, then you could have that running, you know, at a higher time step. And that way, you aren't using valuable resources for something significant and then vice versa. As an extra task, I want you to either modify the constructor or create a new method that sets the milliseconds. So it's not always at the default value of 100. So that's it for this tutorial.
Thanks for watching, and I look forward to seeing you in the next awesome Java tutorial.