Танҳо нишон додан
0:03
S… Speaker 1 (15- Atomic Objects)
Another way to achieve threat safety is by using the atomic classes in Java.
0:07
S… Speaker 1 (15- Atomic Objects)
So in this package,
0:10
S… Speaker 1 (15- Atomic Objects)
we have a bunch of atomic classes like atomic boolean,
0:13
S… Speaker 1 (15- Atomic Objects)
atomic integer,
0:14
S… Speaker 1 (15- Atomic Objects)
atomic long and so on.
0:15
S… Speaker 1 (15- Atomic Objects)
With this atomic classes,
0:17
S… Speaker 1 (15- Atomic Objects)
we can perform atomic operations.
0:19
S… Speaker 2 (15- Atomic Objects)
For example,
0:20
S… Speaker 1 (15- Atomic Objects)
earlier you learned that incrementing a variable in Java is not atomic
0:24
S… Speaker 1 (15- Atomic Objects)
because it involves three instructions,
0:27
S… Speaker 2 (15- Atomic Objects)
get,
0:27
S… Speaker 1 (15- Atomic Objects)
increment,
0:28
S… Speaker 1 (15- Atomic Objects)
and write.
0:29
S… Speaker 1 (15- Atomic Objects)
With these atomic classes,
0:30
S… Speaker 1 (15- Atomic Objects)
we can increment or decrement a value in an atomic way.
0:34
S… Speaker 2 (15- Atomic Objects)
Let me show you.
0:35
S… Speaker 1 (15- Atomic Objects)
So in our demo class,
0:37
S… Speaker 1 (15- Atomic Objects)
we have the same code as before.
0:38
S… Speaker 1 (15- Atomic Objects)
We have a single status object that we're sharing across many different
0:43
S… Speaker 1 (15- Atomic Objects)
threads.
0:43
S… Speaker 1 (15- Atomic Objects)
Over here,
0:44
S… Speaker 1 (15- Atomic Objects)
we're creating 10 download threads.
0:46
S… Speaker 1 (15- Atomic Objects)
Each thread downloads 10 ,000 bytes.
0:48
S… Speaker 1 (15- Atomic Objects)
So once we join with all these threads and print the total
0:53
S… Speaker 1 (15- Atomic Objects)
bytes, we expect to see 100 ,000 on the terminal.
0:56
S… Speaker 2 (15- Atomic Objects)
Take a look.
0:59
S… Speaker 1 (15- Atomic Objects)
We don't see that.
1:00
S… Speaker 1 (15- Atomic Objects)
So we have a race condition now to solve
1:04
S… Speaker 1 (15- Atomic Objects)
this problem.
1:04
S… Speaker 1 (15- Atomic Objects)
We go to our download status class.
1:07
S… Speaker 1 (15- Atomic Objects)
So here we are not using synchronization.
1:09
S… Speaker 1 (15- Atomic Objects)
We are not using locks.
1:10
S… Speaker 1 (15- Atomic Objects)
We have a very simple class now to solve this problem.
1:14
S… Speaker 1 (15- Atomic Objects)
We need to change the type of this field from end to
1:18
S… Speaker 1 (15- Atomic Objects)
atomic integer and then
1:23
S… Speaker 1 (15- Atomic Objects)
we set this to a new atomic integer.
1:25
S… Speaker 1 (15- Atomic Objects)
Now in our getter
1:28
S… Speaker 1 (15- Atomic Objects)
we should return total bytes that get this will return the
1:32
S… Speaker 1 (15- Atomic Objects)
actual value and in our increment method instead of the increment
1:36
S… Speaker 1 (15- Atomic Objects)
operator we call increment and get we
1:40
S… Speaker 1 (15- Atomic Objects)
also have get an increment which returns the value first and then it will
1:44
S… Speaker 1 (15- Atomic Objects)
increment it so more accurately increment and get is equivalent
1:49
S… Speaker 1 (15- Atomic Objects)
to plus plus a where a is our variable and
1:53
S… Speaker 1 (15- Atomic Objects)
get an increment
1:57
S… Speaker 1 (15- Atomic Objects)
equivalent to a plus plus.
1:59
S… Speaker 1 (15- Atomic Objects)
Now in this case we don't want to return this value,
2:02
S… Speaker 1 (15- Atomic Objects)
we just want to increment it.
2:03
S… Speaker 1 (15- Atomic Objects)
So it doesn't really matter which method we use.
2:06
S… Speaker 1 (15- Atomic Objects)
So I would prefer to call increment
2:10
S… Speaker 1 (15- Atomic Objects)
and get because the intention is more clear.
2:13
S… Speaker 1 (15- Atomic Objects)
So that's it,
2:14
S… Speaker 1 (15- Atomic Objects)
that's all we had to do.
2:15
S… Speaker 1 (15- Atomic Objects)
Now multiple threads can modify this field concurrently without
2:20
S… Speaker 1 (15- Atomic Objects)
waiting for each other.
2:21
S… Speaker 1 (15- Atomic Objects)
Let's run the program and verify this.
2:23
S… Speaker 2 (15- Atomic Objects)
So take a look.
2:25
S… Speaker 2 (15- Atomic Objects)
There you go.
2:26
S… Speaker 1 (15- Atomic Objects)
You got 100 ,000.
2:28
S… Speaker 1 (15- Atomic Objects)
Now if you're curious how atomic types work,
2:30
S… Speaker 1 (15- Atomic Objects)
they actually use a technique called compare and swap,
2:34
S… Speaker 1 (15- Atomic Objects)
which is supported by most CPUs.
2:36
S… Speaker 1 (15- Atomic Objects)
So most CPUs can execute this operation as a single uninterruptible
2:41
S… Speaker 1 (15- Atomic Objects)
operation.
2:41
S… Speaker 1 (15- Atomic Objects)
So when we call the increment and get method,
2:45
S… Speaker 1 (15- Atomic Objects)
this atomic type is going to compare the current value with the expected
2:49
S… Speaker 1 (15- Atomic Objects)
value.
2:49
S… Speaker 1 (15- Atomic Objects)
If they're not equal,
2:51
S… Speaker 1 (15- Atomic Objects)
it's going to swap them.
2:52
S… Speaker 2 (15- Atomic Objects)
For example,
2:53
S… Speaker 1 (15- Atomic Objects)
let's say the current value of total bytes is zero and
2:57
S… Speaker 1 (15- Atomic Objects)
we want to increment it.
2:58
S… Speaker 1 (15- Atomic Objects)
So the expected value is one.
3:01
S… Speaker 1 (15- Atomic Objects)
Now this atomic type is going to compare these values because they're different,
3:06
S… Speaker 1 (15- Atomic Objects)
it's going to swap them.
3:07
S… Speaker 1 (15- Atomic Objects)
So one and zero.
3:08
S… Speaker 1 (15- Atomic Objects)
Now this entire operation is natively supported by most CPUs
3:13
S… Speaker 1 (15- Atomic Objects)
as an atomic operation.
3:14
S… Speaker 1 (15- Atomic Objects)
So if you're dealing with counter variables
3:18
S… Speaker 1 (15- Atomic Objects)
prefer atomic types to synchronization because they're faster and easier
3:22
S… Speaker 1 (15- Atomic Objects)
to use.

Ин нусхаи навишташуда бо AI (муайянкунии худкори сухан) эҷод шудааст. Мумкин аст хатогиҳо дошта бошад - барои истифодаи муҳим бо аудиои аслӣ тафтиш кунед. Сиёсати AI

❤️ Шумо STT.ai- ро дӯст медоред? Ба дӯстонатон бигӯед!
Ҷамъбаст
Барои эҷоди ҷамъбасти AI- и ин нусхаи навиштаҷот, тугмаи ҷамъбастро пахш кунед.
Иттилоот...
Дар бораи ин нусхаи навиштаҷот аз AI пурсед
Дар ин ҷо ба шумо дар бораи ин раванд маълумот дода мешавад: Интихоби дурусти раванди эҷодӣ ва раванди таҳия.