Join allows a thread to wait for another thread.
The main thread may need to wait for the background thread to finish and join allows a thread to wait
for another.
In this lesson, you will learn when and how to use the join method.
First, open up the folder for this lesson by following this path in your course resources.
We need to create a function that generates a number as close as possible to the targets with a precision
of however many decimal places that is.
So right here, we'll create a function.
Public static returns a double.
Called Generate No.
OK, and inside the function, we need to generate a number as close as possible to the target.
So first, the number that we get.
Is going to be from the random.
And now what I want to do is get the difference between the target and the number that gets generated,
so what I'll do is I'm going to cut another function public static.
Double call difference.
This function is going to receive a number.
Double, no.
And it's going to return the difference between the target and the number that gets passed on target.
Minus no.
Now, if the no happens to be higher than the target, we don't want to return a negative value.
So what I'll do is I'll take the absolute value so that the difference returned is always positive.
OK.
And now I can say double difference.
Is equal to the return value from difference.
And now a critter while loop.
While the difference is bigger than the precision.
Then we need to generate another random number.
And recalculate the difference.
And so until that difference is less than zero point zero zero zero zero zero one.
This loop is going to keep running.
We seem to have an error.
Uh, this should be capitalized.
And once the while loop breaks, we know that our number is as close as it's going to get to the target.
We'll return it.
This function should run for a pretty long time because such a precision is going to be hard to get.
Back in Maine, I'm going to call generate number here, I'm going to say double result is equal to
generate no.
And then I'm going to calculate the actual precision right here.
Double precision is equal to the difference between the resulting number and the targets.
Will fill these placeholders inappropriately.
And this is taking way too long to produce a number.
And wow, that's pretty impressive, it eventually generated a value to a precision of 10 decimal places.
That's pretty good.
But it took way too long to produce that number.
This is considered a time intensive task.
Remember that time intensive tasks should not run on the main thread.
They should run on another thread.
So what I'm going to do for now is is set this equal to zero because we cannot call generate number
on the main thread.
Now, inside may not Java, I'm going to create a new object of the thread class.
This thread will execute one runnable task.
And now we have to create a runnable object, and we can do that using a lambda expression.
Runnable is equal to and when Runnable Run eventually gets called, it's going to pass and no arguments,
so it follows that our lambda expression is going to receive no parameters.
The arrow key is going to point to a block of code that's going to perform a task.
And that task is generate.
No, we're creating an object of a class that we haven't even defined that implements the runnable interface.
Anyways, when this task gets executed, it's going to return a number.
Until we can see that result.
Equal to the return value.
Now we get an error, and the issue is that we cannot use a local variable inside the expression unless
it's final.
That's not something that we can do, so the trick is to just define the variable at the class level
so that it's not local.
But now you cannot access a non static variable inside of a static method.
So we have to make this static as well.
And that's really it.
Now we can call.
The red dots start.
Which behind the scenes is going to call a runnable run, which performs our long running task on a
different thread.
And if we try it out.
I hope you can see what the problem is.
The long task is running on one thread while our other code is running on the main thread, the main
thread executes its code a lot faster.
It doesn't wait for thread to to finish.
The main thread is running this code, not waiting for Thread two to finish and to return the results
so after a few seconds.
Thread two eventually finishes updates the results, but we were supposed to use that result in the
main thread before.
But the main thread finished executing a long time ago, and this is where the join method comes into
play.
Join Method waits for a thread to die.
Here, if I say.
Threat to join.
Which throws an interrupted exception, so we'll try to run the code.
Catch the exception if the code fails.
If we do that, the main threat is waiting for a threat to to die in after it dies.
Prince, the values.
We're making progress on more than one task at a time.
The long running task is running in the background in this line of code is telling the main thread OK,
we still need to wait for thread to die before you can run these lines of code because they depend on
the updated results.
So if I were to have some scanner related code right here, scanner scan is equal to a new object of
the scanner class that receives input from the system.
And now if I were to say.
Please enter a name.
To generate a number.
Scanned up close.
So here, as the user is entering their name, this task is running in the background once they finally
enter their name or background tasks have already finished a long time ago and our values get printed
out right away.
But we still need the thread to join as a precaution because you cannot guarantee that the thread is
going to finish running before the user enters their name.
In any case, take home messages.
The long running task is running in the background thread, which leaves us to do whatever we want in
the main thread.
Let's recap calling thread join waits for a threat to die time intensive tasks should not block the
main thread.
Time intensive tasks should run on another thread.
Use join when code from one thread depends on the result of another thread.
In other words, when a thread must wait for another threat to finish.