Sýna aðeins
0:03
S… Speaker 1 (10- Confinement)
Let's see how we can use confinement to prevent race conditions.
0:06
S… Speaker 1 (10- Confinement)
So instead of showing a single status object across multiple
0:10
S… Speaker 1 (10- Confinement)
threads, we're going to have each thread work with its own status object.
0:14
S… Speaker 1 (10- Confinement)
So let's remove this global status object and
0:19
S… Speaker 1 (10- Confinement)
remove it from here as well.
0:21
S… Speaker 1 (10- Confinement)
Now we go into constructor of this class and remove this parameter
0:25
S… Speaker 1 (10- Confinement)
and then set the status field to a new download status
0:29
S… Speaker 1 (10- Confinement)
object.
0:30
S… Speaker 1 (10- Confinement)
Now back to our demo class.
0:33
S… Speaker 1 (10- Confinement)
So we start 10 threads.
0:35
S… Speaker 1 (10- Confinement)
We wait for them to finish.
0:36
S… Speaker 1 (10- Confinement)
At the end,
0:38
S… Speaker 1 (10- Confinement)
we have to ask each thread for its status object.
0:41
S… Speaker 1 (10- Confinement)
Then we'll combine the total bytes across all these status objects.
0:45
S… Speaker 1 (10- Confinement)
But there's a problem here.
0:46
S… Speaker 1 (10- Confinement)
We're creating our download tasks over here.
0:50
S… Speaker 1 (10- Confinement)
So later on,
0:51
S… Speaker 1 (10- Confinement)
we won't have access to them.
0:52
S… Speaker 2 (10- Confinement)
Now,
0:53
S… Speaker 1 (10- Confinement)
to solve this,
0:54
S… Speaker 1 (10- Confinement)
we're going to create a list of download file task objects.
0:57
S… Speaker 1 (10- Confinement)
And every time we create a new download file task,
1:00
S… Speaker 1 (10- Confinement)
we're going to add it to that list.
1:01
S… Speaker 2 (10- Confinement)
So,
1:03
S… Speaker 1 (10- Confinement)
Let's create a list of download file task We
1:08
S… Speaker 1 (10- Confinement)
call it tasks and set it to a new array list now
1:13
S… Speaker 1 (10- Confinement)
in our for loop First we create a task object new download
1:17
S… Speaker 1 (10- Confinement)
file task Then we add it to our list so
1:21
S… Speaker 1 (10- Confinement)
tasks that add task and now we can
1:25
S… Speaker 1 (10- Confinement)
pass that object over here That's better now
1:29
S… Speaker 1 (10- Confinement)
after all these threads are finished We need to
1:33
S… Speaker 1 (10- Confinement)
combine the total bytes across these tasks.
1:36
S… Speaker 2 (10- Confinement)
So
1:37
S… Speaker 1 (10- Confinement)
We go back to our download file task class and create a
1:41
S… Speaker 1 (10- Confinement)
getter for this field so we can read it later on.
1:44
S… Speaker 1 (10- Confinement)
We put the caret on the field name and bring up the generate palette to
1:48
S… Speaker 1 (10- Confinement)
create a getter for this field.
1:50
S… Speaker 1 (10- Confinement)
So here's our getter,
1:53
S… Speaker 1 (10- Confinement)
getStatus,
1:54
S… Speaker 1 (10- Confinement)
which returns the status field.
1:56
S… Speaker 2 (10- Confinement)
Now,
1:57
S… Speaker 1 (10- Confinement)
As a best practice,
1:58
S… Speaker 1 (10- Confinement)
move these getters and setters down the bottom.
2:00
S… Speaker 1 (10- Confinement)
This makes our code cleaner and easier to read.
2:03
S… Speaker 1 (10- Confinement)
So first we list our fields,
2:05
S… Speaker 1 (10- Confinement)
then we have our constructors,
2:07
S… Speaker 1 (10- Confinement)
then we have our public methods,
2:08
S… Speaker 1 (10- Confinement)
and finally we have our getters and setters.
2:11
S… Speaker 2 (10- Confinement)
So,
2:12
S… Speaker 1 (10- Confinement)
let's move this down.
2:13
S… Speaker 2 (10- Confinement)
There you go.
2:16
S… Speaker 1 (10- Confinement)
So, after the run method,
2:19
S… Speaker 1 (10- Confinement)
we have our getter.
2:20
S… Speaker 2 (10- Confinement)
Now,
2:21
S… Speaker 1 (10- Confinement)
back to our demo class.
2:23
S… Speaker 1 (10- Confinement)
Here we can use the streams API to combine the total bytes across
2:27
S… Speaker 1 (10- Confinement)
these tasks.
2:28
S… Speaker 2 (10- Confinement)
So,
2:29
S… Speaker 1 (10- Confinement)
we go to our tasks list,
2:31
S… Speaker 1 (10- Confinement)
get a stream,
2:33
S… Speaker 1 (10- Confinement)
then map each task to the total bytes it
2:37
S… Speaker 1 (10- Confinement)
has downloaded.
2:38
S… Speaker 2 (10- Confinement)
So,
2:39
S… Speaker 1 (10- Confinement)
map,
2:39
S… Speaker 1 (10- Confinement)
we pass lambda,
2:40
S… Speaker 1 (10- Confinement)
t goes to,
2:41
S… Speaker 1 (10- Confinement)
t dot get status,
2:43
S… Speaker 1 (10- Confinement)
and then we get the total bytes.
2:46
S… Speaker 1 (10- Confinement)
With this we get a stream of integers.
2:50
S… Speaker 1 (10- Confinement)
Now we can reduce this integer.
2:51
S… Speaker 1 (10- Confinement)
So we call it reduce.
2:52
S… Speaker 1 (10- Confinement)
We can pass an initial value and then a binary operator.
2:56
S… Speaker 1 (10- Confinement)
For example,
2:58
S… Speaker 1 (10- Confinement)
a and b goes to a plus b.
3:01
S… Speaker 1 (10- Confinement)
Or we can rewrite this as integer
3:05
S… Speaker 2 (10- Confinement)
sum.
3:06
S… Speaker 1 (10- Confinement)
So we use a method reference here.
3:10
S… Speaker 1 (10- Confinement)
With this we get the total bytes.
3:12
S… Speaker 1 (10- Confinement)
So let's store it over here and then print
3:16
S… Speaker 1 (10- Confinement)
it on a terminal.
3:17
S… Speaker 1 (10- Confinement)
As simple as that.
3:19
S… Speaker 2 (10- Confinement)
Take a look.
3:21
S… Speaker 2 (10- Confinement)
So
3:24
S… Speaker 1 (10- Confinement)
we have 100 ,000 bytes.
3:26
S… Speaker 1 (10- Confinement)
If we run this program again,
3:28
S… Speaker 1 (10- Confinement)
we get the exact same result.
3:31
S… Speaker 1 (10- Confinement)
So we no longer have a race condition because these threads are not
3:35
S… Speaker 1 (10- Confinement)
modifying a shared object.

Þessi afritun var búin til af AI (sjálfvirk talgreining). Getur innihaldið villur - staðfesta gegn upprunalegu hljóðinu til mikilvægrar notkunar. AI stefna

❤️ Elska STT.ai? Segðu vinum þínum!
Samantekt
Smelltu á Samantekt til að búa til AI samantekt á þessari afritun.
Samantekt...
Spyrja AI um þessa afritun
Spyrðu eitthvað um þetta afritunarrit - AI mun finna viðeigandi hluta og svara.