1 00:00:00,450 --> 00:00:04,110 This lesson is going to be really short, I'm sure at some point in the future you're going to come 2 00:00:04,110 --> 00:00:07,530 across atomic types, and this is a perfect time to explain what it is. 3 00:00:08,039 --> 00:00:12,240 Atomic types let you safely update variables across multiple threads. 4 00:00:12,660 --> 00:00:15,090 They're also cleaner and faster than locks. 5 00:00:15,420 --> 00:00:21,030 So if you're a variable is being updated by many threads, then simply declaring it as an atomic can 6 00:00:21,030 --> 00:00:22,290 make your code thread safe. 7 00:00:22,830 --> 00:00:25,080 Some now declare this as an atomic integer. 8 00:00:27,130 --> 00:00:30,070 Which is equal to a new atomic integer. 9 00:00:31,630 --> 00:00:32,920 With the value of zero. 10 00:00:34,300 --> 00:00:37,150 And now this is not a primitive type anymore, it's a class type. 11 00:00:37,450 --> 00:00:43,150 So instead of saying counter plus plus, we're going to say counter and we're going to call the ad and 12 00:00:43,150 --> 00:00:46,060 get method from the atomic energy class. 13 00:00:46,540 --> 00:00:51,190 And so what the ad part is going to do is during its run, it's going to add one. 14 00:00:51,700 --> 00:00:55,330 And what Dick Gephardt does is it returns the updated value. 15 00:00:57,820 --> 00:00:59,560 And now we don't have to use locks anymore. 16 00:01:01,280 --> 00:01:02,630 If we rerun the code. 17 00:01:06,560 --> 00:01:10,280 Even across multiple threads, our calendar gets updated correctly. 18 00:01:10,940 --> 00:01:13,220 And so why were we able to remove the lock? 19 00:01:13,670 --> 00:01:18,890 Well, atomic variables are special in the sense that when this atomic variable is being accessed and 20 00:01:18,890 --> 00:01:24,080 updated by one thread, and let's say another thread tries to join in, it's going to say, Whoa, whoa, 21 00:01:24,080 --> 00:01:25,280 whoa, wait your turn. 22 00:01:25,670 --> 00:01:27,550 And so they're pretty special in that sense. 23 00:01:27,560 --> 00:01:32,570 So to keep it short, whenever possible, they were using an atomic type over a lock. 24 00:01:32,600 --> 00:01:37,690 It's cleaner and typically faster, so we can remove this lock parameter right here. 25 00:01:37,700 --> 00:01:39,020 We don't need it anymore. 26 00:01:41,640 --> 00:01:45,960 I'm here as well, and you can remove the re-entering clock declaration.