A countdown that makes it easy to wait for multiple threats.
I should say we had eight threads, and each one is executing a runnable task.
Now you wouldn't want to call thread one to join thread to thread three, thread for it to join, so
on and so forth.
That just seems so redundant.
What if instead we could just say large, shorter wait.
And then our main thread just knows to wait for every task to finish.
Well, that's why in this lesson, you're going to use a countdown that's that awaits worker threats.
A countdown latch allows a parent thread to await the completion of worker threads.
And so before we start, let's take a look at the dark and grim world where we don't have countdown
clocks.
Imagine I actually need to run this task four times, so I would copy and paste this.
I would call this thread three, thread four.
Then I would have to say, throw three dots, start again.
We should be using a thread pool.
We'll do that later.
Thread four starts.
Then I would have to call Thread joined four times, this is already getting so repetitive.
And now, if you were to run our code.
I mean, everything works, but this is just not great, it's not good at all.
What if we needed the task to run a thousand times or, you know, call a thousand joint methods?
Well, I mean, in any case, we should be using a thread pool, but we'll get to that eventually.
What I'm going to do here is delete all of these join calls.
And I'm going to create a countdown that's where we expect to run four tasks ceremonies say countdown
much.
Latch.
Is equal to a new countdown latch that counts down from four.
Now, from within the try catch block, I'm going to call Larch .08.
And now is are allowed to go on to magically wait until all of our tasks have been completed.
Let's find out.
Definitely not.
This seems like it's going to run forever.
The Latch opens after it countdown to zero.
In this case, a relapse starts at four, and when we call it a wait, we're basically closing the latch.
This latch is never going to open until it counts down to zero.
So after we perform each task, we need to call a large dark countdown.
So we're going to do is run a patch now pass, not patch.
We're going to pass our large countdown much into the task method.
And now after we finish each task, we're going to count down large dark countdown.
For every task that executes are allowed is going to count down one.
And so after four tasks, it's going to count down to zero, causing our alarge to open and continue
the code inside men.
So if we run our code.
Oh, I'm going to have to close the previous latch and try this again.
Everything works out.
As our background threads are running large, that a way to close as the latch on the main thread.
The latter will never open until every single task completes and counts down.
And once it counts down to zero hour latch opens and whatever needed to wait inside, Maine can now
get executed.
That being said, you should favor reusing threads over creating new ones.
In other words, use a thread pool that retains an active number of threads.
And so based on our formula for CPU intensive tasks, I'm going to get the number of available course.
So we can say it's number of threads is equal to runtime.
Died at one time, the ones available to our up.
Then I can create a new threat pool using the executor service class.
Executor.
Is equal to executor's dot new fixed thread pool.
And now, instead of creating four different threads, what we're going to do is directly submit each
task.
OK.
And as soon as you submit a task, it already starts running in the background, so we don't need these.
You're on the application.
Oh, we forgot to shut our thread pool down, so finally.
We'll say executor does shutdown.
Perfect.
Now, let's just say you were running 11 tasks you would have to start counting, down from 11.
If you were running 55 tasks, you would have to start counting down from 55.
Whatever it is, make sure your countdown clock is consistent with the amount of tasks that you're executing.
And you know what?
Let's just say we do want to perform 11 tasks.
All we would have to do is say for index is smaller than 11 executor does submit.
Make sure you start counting down from 11.
Beautiful.
55 55.
Nice.
Our threat pool is reusing threads to perform 55 different tasks are our large starts counting, down
from 55.
And every time a task is performed, it counts down one once it gets to zero.
Our lunch opens and whatever needs to be executed in Maine executes.
To recap, a countdown match allows a parent thread to await the completion of worker threads.