Uitsluitend tonen
0:03
S… Speaker 2 (16- Adders)
So atomic objects are great for implementing counters,
0:06
S… Speaker 2 (16- Adders)
but if you have multiple threads updating a value frequently,
0:10
S… Speaker 2 (16- Adders)
it's better to use one of the other classes in Java.
0:12
S… Speaker 2 (16- Adders)
They're faster than atomic types.
0:14
S… Speaker 2 (16- Adders)
So we have long adder for adding long values
0:18
S… Speaker 2 (16- Adders)
and double adder.
0:20
S… Speaker 2 (16- Adders)
Let me show you how they work.
0:22
S… Speaker 2 (16- Adders)
So we have the same code as before.
0:25
S… Speaker 2 (16- Adders)
This is the code that has a race condition.
0:27
S… Speaker 2 (16- Adders)
So when we run this program,
0:30
S… Speaker 2 (16- Adders)
we see this weird number over here.
0:32
S… Speaker 2 (16- Adders)
Let's solve this problem using an adder class.
0:35
S… Speaker 1 (16- Adders)
So in the last video,
0:36
S… Speaker 2 (16- Adders)
we replaced this integer field with an atomic adder.
0:40
S… Speaker 2 (16- Adders)
Now we're going to replace this with a long adder.
0:44
S… Speaker 2 (16- Adders)
And then we set this to a new long adder.
0:48
S… Speaker 1 (16- Adders)
All right.
0:49
S… Speaker 2 (16- Adders)
Now in our getter,
0:51
S… Speaker 2 (16- Adders)
we call the int value method.
0:54
S… Speaker 2 (16- Adders)
This will return the result as an integer.
0:56
S… Speaker 2 (16- Adders)
We also have byte value,
0:58
S… Speaker 1 (16- Adders)
short value,
0:59
S… Speaker 1 (16- Adders)
double value,
1:00
S… Speaker 1 (16- Adders)
and so on.
1:01
S… Speaker 2 (16- Adders)
Internally, this long other object keeps an array of counters that can grow
1:06
S… Speaker 1 (16- Adders)
on demand.
1:06
S… Speaker 2 (16- Adders)
So we don't have a single place in memory where our value is stored.
1:10
S… Speaker 2 (16- Adders)
We have a bunch of array cells each holding a counter value.
1:14
S… Speaker 2 (16- Adders)
So different threads can modify this counter variables concurrently.
1:18
S… Speaker 2 (16- Adders)
That's why these other classes are faster than atomic types because
1:22
S… Speaker 1 (16- Adders)
they allow more throughput.
1:23
S… Speaker 2 (16- Adders)
So here we can call
1:25
S… Speaker 2 (16- Adders)
int value method but internally this method is going to call another
1:29
S… Speaker 2 (16- Adders)
method called sum which is going to add up all these counter values
1:34
S… Speaker 1 (16- Adders)
and return the result.
1:35
S… Speaker 2 (16- Adders)
Now the int value will call this method and then convert the result to
1:39
S… Speaker 1 (16- Adders)
an integer.
1:39
S… Speaker 2 (16- Adders)
Now in our increment method we call increment.
1:44
S… Speaker 2 (16- Adders)
We also have add for adding a long value.
1:47
S… Speaker 2 (16- Adders)
We have decrement,
1:49
S… Speaker 2 (16- Adders)
we have reset,
1:50
S… Speaker 1 (16- Adders)
and so on.
1:51
S… Speaker 2 (16- Adders)
So let's call the increment method.
1:53
S… Speaker 1 (16- Adders)
That's all we had to do.
1:54
S… Speaker 2 (16- Adders)
Let's run our program.
1:56
S… Speaker 2 (16- Adders)
The race condition is gone.
1:59
S… Speaker 2 (16- Adders)
So if you have multiple threads updating a value frequently,
2:03
S… Speaker 2 (16- Adders)
prefer the adder classes to atomic types.

Dit transcript werd gegenereerd door AI (automatische spraakherkenning). Kan fouten bevatten..verifieer tegen de oorspronkelijke audio voor kritisch gebruik. AI-beleid

❤️ Hou je van STT.ai? Vertel het je vrienden!
Samenvatting
Klik op Summarize om een AI samenvatting van dit transcript te genereren.
Samengevat...
Vraag AI over dit Transcript
Vraag maar iets over dit transcript De AI zal relevante secties en antwoord vinden.