1 00:00:05,000 --> 00:00:06,000 Hello. 2 00:00:06,000 --> 00:00:09,000 Yes, students in this class and will create our first multithreaded program. 3 00:00:10,000 --> 00:00:14,000 It is time to put our theoretical knowledge on practice and learn new things. 4 00:00:14,000 --> 00:00:20,000 We'll start from understanding of threat lifecycle and in which states our threat may be and when will 5 00:00:20,000 --> 00:00:22,000 jump to practical examples. 6 00:00:22,000 --> 00:00:24,000 You'll have enough knowledge to understand that. 7 00:00:25,000 --> 00:00:29,000 And this lesson I will show you how to create a threat with five different ways. 8 00:00:29,000 --> 00:00:35,000 We are going to learn such basic laws for multithreaded in general threat and one of the most important 9 00:00:35,000 --> 00:00:36,000 interfaces. 10 00:00:36,000 --> 00:00:37,000 Runnable. 11 00:00:37,000 --> 00:00:43,000 While showing you examples, I will focus your attention on main properties of threat class to describe 12 00:00:43,000 --> 00:00:44,000 each state. 13 00:00:44,000 --> 00:00:50,000 And once we Iran's threats, we will learn with you how we can interact execution of a threat. 14 00:00:50,000 --> 00:00:56,000 We'll discuss methods that are deprecated for a long time already and that you never should use. 15 00:00:56,000 --> 00:00:59,000 And also we'll talk about proper ways of threat interaction. 16 00:01:00,000 --> 00:01:03,000 Today, we're going to have a lot of good examples. 17 00:01:03,000 --> 00:01:09,000 And as we will go over the examples, I will also explain specifics of some methods and behavior of 18 00:01:09,000 --> 00:01:09,000 threats. 19 00:01:10,000 --> 00:01:10,000 Let's start. 20 00:01:11,000 --> 00:01:17,000 And as I said before, let's start from the understanding the whole lifecycle of a threat is considered 21 00:01:17,000 --> 00:01:18,000 to be a new one. 22 00:01:18,000 --> 00:01:20,000 We just created an object of a threat. 23 00:01:21,000 --> 00:01:23,000 And you know that in Java, everything is an object. 24 00:01:24,000 --> 00:01:28,000 That's why threat of execution is also an object to run a threat. 25 00:01:28,000 --> 00:01:35,000 We need to start massive because mass that creates a separate threat of execution within the Java process. 26 00:01:36,000 --> 00:01:38,000 After that threat is in the running state. 27 00:01:38,000 --> 00:01:44,000 In this state, we can make our threat not run by turning our threat into waiting state or block state. 28 00:01:45,000 --> 00:01:50,000 During this course, we will discuss all these scenarios, for example, when threat is blocked and 29 00:01:50,000 --> 00:01:57,000 when it's waiting for now, you need just to understand that running threat may be put on pause and 30 00:01:57,000 --> 00:02:01,000 getting back to running state again as engineers will learn how to do this. 31 00:02:02,000 --> 00:02:06,000 Sometimes threats waiting their chance to update a shared resource. 32 00:02:06,000 --> 00:02:12,000 Sometimes we want to make a threat, wait until other threats will adjust it of our shared resource. 33 00:02:12,000 --> 00:02:16,000 And the last possible state of our threat is a Terminator's state. 34 00:02:16,000 --> 00:02:21,000 This state tells us that threat finished its execution or it was interrupted. 35 00:02:22,000 --> 00:02:24,000 Is it clear now when you're no threat? 36 00:02:24,000 --> 00:02:27,000 Life-cycle it is time to write our first. 37 00:02:27,000 --> 00:02:32,000 Just read an application I open file was named first just writing program. 38 00:02:32,000 --> 00:02:34,000 I have main message here. 39 00:02:34,000 --> 00:02:37,000 Let me go line by line and explain you each role here. 40 00:02:38,000 --> 00:02:44,000 You can see that the first thing that I do here, I declare a variable of type Runnable and initialize 41 00:02:44,000 --> 00:02:46,000 it with new object of type default. 42 00:02:46,000 --> 00:02:47,000 Runnable. 43 00:02:47,000 --> 00:02:52,000 Let me open source code of Runnable type, because you see this time the first time in this course, 44 00:02:53,000 --> 00:02:56,000 as you can see, this is an interface from the Java version. 45 00:02:56,000 --> 00:03:01,000 What does a functional interface was only one abasic mass with name. 46 00:03:01,000 --> 00:03:01,000 Right. 47 00:03:02,000 --> 00:03:06,000 We see that this method returns nassan and return type is void. 48 00:03:06,000 --> 00:03:13,000 This is exactly the method that we need to implement and where we need to put a code that will be executed 49 00:03:13,000 --> 00:03:18,000 in the separate thread to have separate thread, we need to have instructions about what this thread 50 00:03:18,000 --> 00:03:23,000 is supposed to do and these instructions should be written somewhere. 51 00:03:23,000 --> 00:03:26,000 This is exactly the method that will be executed by thread. 52 00:03:27,000 --> 00:03:33,000 Let's now check the source code of a default runnable we see as a default Runnable implements Runnable 53 00:03:33,000 --> 00:03:34,000 interface. 54 00:03:34,000 --> 00:03:41,000 Basically, this is my custom clause that gives implementation to run Macel inside this matter. 55 00:03:41,000 --> 00:03:43,000 I print such text to console. 56 00:03:43,000 --> 00:03:45,000 I'm a new threat. 57 00:03:45,000 --> 00:03:52,000 My name is and here I call static method on class will learn this class in a minute. 58 00:03:52,000 --> 00:03:58,000 But just to finish explanation of this line, current threat is a static method of threat class that 59 00:03:58,000 --> 00:04:00,000 returns the reference to the threat object. 60 00:04:01,000 --> 00:04:08,000 And depending on where this method is invoked, we receive the reference to the object of current threat. 61 00:04:08,000 --> 00:04:13,000 And after that I just call that name method that returns is a name of the threat. 62 00:04:13,000 --> 00:04:18,000 I want to bring this text to console to prove that we have separate threat of execution. 63 00:04:18,000 --> 00:04:20,000 One will run our app. 64 00:04:20,000 --> 00:04:24,000 But before doing that, let's investigate threat glass. 65 00:04:24,000 --> 00:04:26,000 Let me open the source code of this class. 66 00:04:27,000 --> 00:04:31,000 The first thing is that you can notice here is that threat also implements Runnable. 67 00:04:32,000 --> 00:04:33,000 What does this mean? 68 00:04:33,000 --> 00:04:38,000 This means that the object of threat type also may be executed as a separate threat. 69 00:04:38,000 --> 00:04:42,000 And this class also has implementation of around MassArt. 70 00:04:42,000 --> 00:04:43,000 Let's look at it. 71 00:04:43,000 --> 00:04:49,000 As we can see, the first things that object will try to do is to check whether we have target field 72 00:04:49,000 --> 00:04:49,000 initialized. 73 00:04:49,000 --> 00:04:53,000 And if yes, then we'll call run Masset on that object. 74 00:04:54,000 --> 00:04:57,000 And if not, then we'll just do nothing. 75 00:04:57,000 --> 00:05:02,000 Target Field has Runnable type and we can see even Kermanshah above this field. 76 00:05:02,000 --> 00:05:03,000 This is something. 77 00:05:03,000 --> 00:05:08,000 What will be around so as you can understand, we can do two things here. 78 00:05:08,000 --> 00:05:16,000 We can pass Runnable object to the threat glass or we can override run Masad in Third-Class, we can 79 00:05:16,000 --> 00:05:18,000 pass Runnable to this class. 80 00:05:18,000 --> 00:05:19,000 We are constructor. 81 00:05:19,000 --> 00:05:23,000 We have really a lot of different versions of constructor for this type. 82 00:05:23,000 --> 00:05:30,000 And one of the constructor is to accept and initialize target field to store the reference to the Runnable 83 00:05:30,000 --> 00:05:30,000 object. 84 00:05:31,000 --> 00:05:37,000 Interesting thing to notice here is that this constructor invokes another version of constructor. 85 00:05:37,000 --> 00:05:39,000 It passes in reference to the REDgroup. 86 00:05:40,000 --> 00:05:42,000 We'll discuss a little bit later. 87 00:05:42,000 --> 00:05:48,000 What does it reference to our Runnable object name of the thread, which consists of the word thread 88 00:05:48,000 --> 00:05:50,000 dash and the sequential number. 89 00:05:50,000 --> 00:05:53,000 Is it a static variable name thread in it? 90 00:05:53,000 --> 00:06:00,000 No, that is incremented every time when the next threatener method and the last argument is a stack 91 00:06:00,000 --> 00:06:07,000 size zero because according to the recommendation, in case this argument is zero, we have no requirement 92 00:06:07,000 --> 00:06:09,000 for specific text size for this new threat. 93 00:06:10,000 --> 00:06:15,000 And the new constructor Interent calls new constructor that passes to more arguments. 94 00:06:15,000 --> 00:06:23,000 New and true new value is supposed to cover such constructor argument as access control context and 95 00:06:23,000 --> 00:06:29,000 true value is the flag to indicate whether we need to inherit initial values for inheritable threat 96 00:06:29,000 --> 00:06:31,000 logos from the construction threat. 97 00:06:32,000 --> 00:06:34,000 Let me explain what these things meant. 98 00:06:35,000 --> 00:06:42,000 Access control context is a type that is used to create objects that make system access decisions based 99 00:06:42,000 --> 00:06:44,000 on the context it encapsulates. 100 00:06:44,000 --> 00:06:50,000 Check permission Massada that is invoked in the context that access control context encapsulates inside 101 00:06:51,000 --> 00:06:56,000 is allowed to process was access to resources or access control exception. 102 00:06:56,000 --> 00:07:00,000 Anyway, this is not the topic for the first lesson of not just reading. 103 00:07:00,000 --> 00:07:04,000 And this is not what you will use on a daily basis directly while working with me. 104 00:07:04,000 --> 00:07:05,000 Just read it. 105 00:07:05,000 --> 00:07:07,000 But just in order to let you know. 106 00:07:08,000 --> 00:07:10,000 Regarding the second argument inherits read. 107 00:07:10,000 --> 00:07:13,000 Locals do not pay a lot of attention to this one. 108 00:07:13,000 --> 00:07:20,000 And this moment will cover concept of threat locals and inheritable threat local separately, but ensured 109 00:07:20,000 --> 00:07:26,000 this flag is to tell threat whether we need to inherit values from inheritable threat local from the 110 00:07:26,000 --> 00:07:27,000 construction threat. 111 00:07:27,000 --> 00:07:32,000 And basically after that we proceed with construction of our threat of execution. 112 00:07:33,000 --> 00:07:38,000 Besides the construction of Runnable that we have just discussed, we have also different constructors 113 00:07:38,000 --> 00:07:44,000 that initializes other properties of threat object, for example, threat that takes Runnable and access 114 00:07:44,000 --> 00:07:51,000 control context, constructor that takes REDgroup and Runnable also another popular constructor for 115 00:07:51,000 --> 00:07:54,000 the bag and purposes is constructor with Runnable and string. 116 00:07:54,000 --> 00:08:00,000 String represents the name of the threat and other different variations, including constructors with 117 00:08:00,000 --> 00:08:04,000 taxes and boolean flag to indicate the state of the inherited threat. 118 00:08:04,000 --> 00:08:05,000 The locals flag. 119 00:08:06,000 --> 00:08:07,000 What are the properties? 120 00:08:07,000 --> 00:08:08,000 The threat glass half. 121 00:08:09,000 --> 00:08:16,000 If we would scroll to the top of this file, will find properties that we also may use during the configuration 122 00:08:16,000 --> 00:08:16,000 of the threat. 123 00:08:17,000 --> 00:08:19,000 You already know about threatening property. 124 00:08:20,000 --> 00:08:22,000 By the way, here is also new keyword for you. 125 00:08:23,000 --> 00:08:24,000 Volatile will cover it. 126 00:08:24,000 --> 00:08:28,000 Do in the separate lesson we have priority property. 127 00:08:29,000 --> 00:08:35,000 This is a from one to 10 that will be used by threat scheduler to decide what threat is more important 128 00:08:35,000 --> 00:08:36,000 to work with. 129 00:08:36,000 --> 00:08:43,000 So this next, however, we never can rely on the priority property when we have concurrent threat executed. 130 00:08:43,000 --> 00:08:49,000 Let's call this recommendation, but not mandatory direction to follow for threats. 131 00:08:49,000 --> 00:08:50,000 Catala What is a threat? 132 00:08:50,000 --> 00:08:54,000 Skalla It is part of GM that decides which threats should run. 133 00:08:55,000 --> 00:08:58,000 Also, it decides which threats should wait, how it is. 134 00:08:59,000 --> 00:09:03,000 This is a great question, but probably not for the first practical move. 135 00:09:03,000 --> 00:09:06,000 This riding lesson will also cover this topic separately. 136 00:09:07,000 --> 00:09:10,000 The next property here is on by default. 137 00:09:10,000 --> 00:09:11,000 It is false. 138 00:09:12,000 --> 00:09:16,000 We will discuss later in this course what demons threats are interrupted. 139 00:09:16,000 --> 00:09:23,000 Flag is used to indicate where the threat is interrupted and we need to stop its execution or we need 140 00:09:23,000 --> 00:09:25,000 to proceed execution of a threat. 141 00:09:25,000 --> 00:09:26,000 Let in this lesson. 142 00:09:26,000 --> 00:09:30,000 I will also show you how to interact with this property. 143 00:09:31,000 --> 00:09:33,000 Maxfield's here used by Jovian. 144 00:09:33,000 --> 00:09:36,000 We will not focus our attention on them in this lesson. 145 00:09:37,000 --> 00:09:40,000 And probably the one property that we did not talk yet is a threat. 146 00:09:40,000 --> 00:09:48,000 The group this object is used to groups threats to gather in one group support group forms a three and 147 00:09:48,000 --> 00:09:50,000 every threat except a regional threat. 148 00:09:50,000 --> 00:09:51,000 The group has a parent. 149 00:09:52,000 --> 00:09:58,000 There are also some specific self-locking mechanism for threat in that group, but probably will cover 150 00:09:58,000 --> 00:09:59,000 this in a separate lesson. 151 00:10:00,000 --> 00:10:03,000 And it looks like that Tetragon high level of. 152 00:10:03,000 --> 00:10:10,000 You officer red class and its properties definitely as more things to learn here, but we'll do it gradually 153 00:10:10,000 --> 00:10:12,000 and with practical examples, case by case. 154 00:10:13,000 --> 00:10:15,000 Now, let's get back to the demo file here. 155 00:10:15,000 --> 00:10:20,000 You can see that I created a threat object and positive reference to the Runnable object. 156 00:10:20,000 --> 00:10:25,000 Why option was a separate class might be needed in this case. 157 00:10:25,000 --> 00:10:27,000 You can declare some files if needed. 158 00:10:28,000 --> 00:10:34,000 Also, you can declare constructors and pass external resources to the new objects if needed, because 159 00:10:34,000 --> 00:10:38,000 by default, ranma, it doesn't take anything as Masset argument. 160 00:10:38,000 --> 00:10:43,000 And here's just an example of constructor initialization of the state of this object. 161 00:10:44,000 --> 00:10:51,000 If we want, we can pass any external object to the constructor and use the state of our runnable object 162 00:10:51,000 --> 00:10:53,000 in one method if needed. 163 00:10:53,000 --> 00:10:58,000 In such a way you can just share the resource that we shared with this thread. 164 00:10:59,000 --> 00:11:05,000 This is basically the first way to create a threat object that is ready to be executed and that is in 165 00:11:05,000 --> 00:11:06,000 your state. 166 00:11:06,000 --> 00:11:09,000 The next way is another variation of the first one. 167 00:11:10,000 --> 00:11:15,000 Instead of creating type that implements Runnable, we can just pass instance of anonymous class. 168 00:11:16,000 --> 00:11:19,000 You can see here that I have similar method implemented. 169 00:11:20,000 --> 00:11:22,000 Basically nothing special to highlight here. 170 00:11:23,000 --> 00:11:27,000 And to be honest, this way of creating threat is not very popular. 171 00:11:28,000 --> 00:11:34,000 The next way to great threat is to extend a new class from the threat and all around Massett, if you 172 00:11:34,000 --> 00:11:41,000 remember when we were you in threat object, we saw that there is nothing to execute by default if we 173 00:11:41,000 --> 00:11:44,000 didn't pass the reference to the Runnable object. 174 00:11:44,000 --> 00:11:48,000 That's why so radically we can just override one method. 175 00:11:49,000 --> 00:11:54,000 You can see that the current class extends threat clause and we overwrite one method here. 176 00:11:55,000 --> 00:12:00,000 And after that, the main method is simply create the object of the first sorting program type. 177 00:12:01,000 --> 00:12:01,000 That's it. 178 00:12:02,000 --> 00:12:08,000 While this option often described in the literature, don't recommend it to you this way. 179 00:12:08,000 --> 00:12:08,000 Why? 180 00:12:09,000 --> 00:12:13,000 Because he extends that class just to overwrite one method. 181 00:12:13,000 --> 00:12:18,000 Probably it is not the best approach from the single responsibility principle point of view. 182 00:12:19,000 --> 00:12:25,000 For such purposes we have Runnable type and we can use it to give implementation to run Macit. 183 00:12:25,000 --> 00:12:32,000 The fourth way to prepare a threat for execution is to use lambda expression to implement around Macit. 184 00:12:32,000 --> 00:12:34,000 She can see that I created lambda function. 185 00:12:34,000 --> 00:12:37,000 That takes no arguments and returns nassan. 186 00:12:38,000 --> 00:12:44,000 Basically I just print the same text and the last, but not least for today, is the way preparing A 187 00:12:45,000 --> 00:12:46,000 was MassArt reference. 188 00:12:46,000 --> 00:12:53,000 I have a static method declared in this class with name execute that takes no parameters and that brings 189 00:12:53,000 --> 00:12:55,000 the same text to cancel. 190 00:12:55,000 --> 00:13:00,000 I just passing method reference here that meets the definition of Runnable interface. 191 00:13:00,000 --> 00:13:06,000 It doesn't take any arguments and it returns nassan the same as around Massett. 192 00:13:06,000 --> 00:13:12,000 In case you are not familiar with lambda functions and method references in Java, but you want to learn 193 00:13:12,000 --> 00:13:12,000 that. 194 00:13:13,000 --> 00:13:16,000 Check my functional programming course for Java InGenius. 195 00:13:16,000 --> 00:13:22,000 We have really a lot of examples that the next thing that you see in this line and a sprinkling of the 196 00:13:22,000 --> 00:13:27,000 current threat to console and for the sake of example, call run Nassib. 197 00:13:27,000 --> 00:13:36,000 Remember, Run Masset doesn't start as a threat of execution, only start MassArt starts separate thread. 198 00:13:36,000 --> 00:13:42,000 But if you would call directly around MassArt, you would just execute run MassArt synchronously. 199 00:13:43,000 --> 00:13:47,000 And after that I call starts MassArt on each thread that we created here. 200 00:13:47,000 --> 00:13:53,000 Let's run the program to see what we have got in the console in the first line with the printed text 201 00:13:53,000 --> 00:14:02,000 from my main threat here you can see the text current threatening is and its name is Main Man is the 202 00:14:02,000 --> 00:14:09,000 name of the first read that we have in our Java application is the next line is a result of invocation 203 00:14:09,000 --> 00:14:10,000 of FRAND method. 204 00:14:10,000 --> 00:14:13,000 And as you can see, threatening is still man. 205 00:14:13,000 --> 00:14:19,000 That proves that invocation of method doesn't actually start a new threat. 206 00:14:20,000 --> 00:14:27,000 And to start a new threat, we need to start Macit after you can see multiple lines with different names 207 00:14:27,000 --> 00:14:28,000 of a threat. 208 00:14:29,000 --> 00:14:34,000 That is exactly what is returned by getting the same method on the current threat involved from the 209 00:14:34,000 --> 00:14:35,000 threat. 210 00:14:35,000 --> 00:14:42,000 And you remember that by default, naming convention for each threat is threat word and sequential number 211 00:14:42,000 --> 00:14:42,000 of a threat. 212 00:14:43,000 --> 00:14:47,000 Can you understand now how to run your threat in real life? 213 00:14:47,000 --> 00:14:50,000 You often use executor's to run and manage. 214 00:14:50,000 --> 00:14:53,000 Your threats will learn executor's later in this course. 215 00:14:54,000 --> 00:15:00,000 But what we have learned in this lesson, this is basics that you need to understand before moving further. 216 00:15:01,000 --> 00:15:02,000 Now, what do we know how? 217 00:15:03,000 --> 00:15:08,000 To start the threat, let's learn how to interact and stop its execution. 218 00:15:08,000 --> 00:15:16,000 I have separate file for that was name interrupt them is interesting app that will help you to understand 219 00:15:16,000 --> 00:15:17,000 how threat interception works. 220 00:15:18,000 --> 00:15:20,000 Let's start from the main mast. 221 00:15:20,000 --> 00:15:24,000 The first thing that I do here, create a task for execution. 222 00:15:24,000 --> 00:15:31,000 I implement it runnable interface in this class for the sake of this demo and not having multiple classes 223 00:15:31,000 --> 00:15:31,000 in this example. 224 00:15:32,000 --> 00:15:35,000 I will show you around Massud in the minute after this. 225 00:15:35,000 --> 00:15:41,000 I create a threat object and start a threat and I won't let the main threat sleep for two seconds and 226 00:15:41,000 --> 00:15:44,000 interrupt my task after two seconds. 227 00:15:44,000 --> 00:15:45,000 How I can do this? 228 00:15:46,000 --> 00:15:49,000 I invoke sleep masset on the threat object. 229 00:15:49,000 --> 00:15:53,000 This method takes a number of milliseconds as Masset argument. 230 00:15:54,000 --> 00:15:57,000 Two seconds are equal to two thousand milliseconds. 231 00:15:57,000 --> 00:16:00,000 Sleep method is our law that in threat class. 232 00:16:01,000 --> 00:16:02,000 Let's look at the threat clause. 233 00:16:02,000 --> 00:16:03,000 Source code. 234 00:16:03,000 --> 00:16:05,000 We have to sleep massas here. 235 00:16:05,000 --> 00:16:12,000 One was milliseconds only and another one takes two arguments, milliseconds and nanoseconds. 236 00:16:12,000 --> 00:16:14,000 To be honest, I never used sleep mass. 237 00:16:14,000 --> 00:16:19,000 It was nanoseconds, but still it is good to know that such method exists. 238 00:16:19,000 --> 00:16:26,000 Let's get back to our demo file, another really user friendly interface of making sure that sleep is 239 00:16:26,000 --> 00:16:28,000 to use time unit in from Javitz. 240 00:16:28,000 --> 00:16:29,000 You can current package. 241 00:16:30,000 --> 00:16:36,000 This enum represents time duration at the given unit of granularity and provides methods to convert 242 00:16:36,000 --> 00:16:40,000 across units and perform timing and delay operations. 243 00:16:40,000 --> 00:16:47,000 In these units, for example, you may perform such operations as making Meinzer slip, and the interface 244 00:16:47,000 --> 00:16:52,000 allows us to not calculate milliseconds and convert them between seconds and other time units. 245 00:16:52,000 --> 00:16:53,000 One unit. 246 00:16:53,000 --> 00:16:59,000 But we can just take time units that we need and parse the value of the specific time unit that we need. 247 00:17:00,000 --> 00:17:03,000 For example, here is a line similar to the one above. 248 00:17:04,000 --> 00:17:10,000 It also uses milliseconds time unit and here is a line that use a second time unit. 249 00:17:10,000 --> 00:17:12,000 And I'm just saying two seconds. 250 00:17:12,000 --> 00:17:16,000 To be honest, in my opinion, using this API is more preferable. 251 00:17:17,000 --> 00:17:24,000 And after this line, my main threat is in on the running state, in wait in state and the one threat 252 00:17:24,000 --> 00:17:26,000 is awaiting as a threat may interrupt it. 253 00:17:27,000 --> 00:17:31,000 That's why Slakey method maestro so called interrupted exception. 254 00:17:32,000 --> 00:17:39,000 And that's why Compiler makes us handle this exception case to describe how a system should behave in 255 00:17:39,000 --> 00:17:41,000 case somebody will interrupt this thread. 256 00:17:41,000 --> 00:17:46,000 In this example, nobody will interrupt main threat but still interrupted. 257 00:17:46,000 --> 00:17:47,000 Exception is checked. 258 00:17:47,000 --> 00:17:53,000 That's why we need Searls declaration here in Maine method because for the sake of this demo, I don't 259 00:17:53,000 --> 00:17:55,000 want to create try catch block here. 260 00:17:55,000 --> 00:17:59,000 After that I print a console that I interact as a threat. 261 00:18:00,000 --> 00:18:01,000 To interrupt as a threat. 262 00:18:01,000 --> 00:18:07,000 We should call interrupt method interrupt method interruption flag inside the threat object. 263 00:18:08,000 --> 00:18:12,000 That flag basically has two possible values, true or false. 264 00:18:12,000 --> 00:18:19,000 With the help of this method, we can change the state of the interaction flag inside the threat and 265 00:18:19,000 --> 00:18:23,000 the last line and my main threat is print and leave in the console. 266 00:18:23,000 --> 00:18:27,000 Let's now look at our task and understand how interrupt. 267 00:18:27,000 --> 00:18:30,000 Masset interrupts execution of the threat. 268 00:18:30,000 --> 00:18:32,000 I open one method. 269 00:18:32,000 --> 00:18:39,000 I print text that indicates that I entered one method and the threat is about to execute Vork method. 270 00:18:40,000 --> 00:18:41,000 I invoke Vork method. 271 00:18:42,000 --> 00:18:43,000 Let's look at it now. 272 00:18:44,000 --> 00:18:47,000 Work method is that was the infinite loop. 273 00:18:47,000 --> 00:18:49,000 After that we just print one word. 274 00:18:50,000 --> 00:18:52,000 Verkin in f close. 275 00:18:52,000 --> 00:18:55,000 I check if threat is interrupted is interrupted. 276 00:18:55,000 --> 00:19:02,000 Matthew Chance is a state of the interaction flat and in case somebody from as a threat called MassArt 277 00:19:02,000 --> 00:19:07,000 interrupt like we did in our main threat, then this method will return. 278 00:19:07,000 --> 00:19:07,000 True. 279 00:19:08,000 --> 00:19:13,000 Right now you already know what is done by interrupt and by is interrupted masses. 280 00:19:14,000 --> 00:19:16,000 But why do we need interrupted? 281 00:19:16,000 --> 00:19:24,000 Macit interrupted Massata returns also boolean value and return state of the interaction flac but it 282 00:19:24,000 --> 00:19:28,000 also clears it and return it back to false. 283 00:19:28,000 --> 00:19:35,000 So in case what uncommon this line and will executed interruption flag will be turned back to false 284 00:19:35,000 --> 00:19:36,000 again. 285 00:19:36,000 --> 00:19:42,000 To interrupt this thread I will keep this line commanded and we'll just leave it as an example for you. 286 00:19:42,000 --> 00:19:49,000 By the way, I encourage you to download this service code example from the GitHub and run it locally 287 00:19:49,000 --> 00:19:49,000 on your computer. 288 00:19:50,000 --> 00:19:55,000 By doing this, you can play with values and comment and comment the lines that you are interested in 289 00:19:55,000 --> 00:20:00,000 and as always, service code examples in attachments to this lesson. 290 00:20:00,000 --> 00:20:02,000 OK, so now I believe. 291 00:20:03,000 --> 00:20:08,000 The difference between these three methods interact is interrupted and interrupted. 292 00:20:08,000 --> 00:20:14,000 Now pay attention that threat is moved to the white state because and will sleep mass at here. 293 00:20:14,000 --> 00:20:21,000 And by the way, no matter how long sleep will be, I put one millisecond here for sleep. 294 00:20:21,000 --> 00:20:24,000 But still, it is enough to interrupt the threat. 295 00:20:24,000 --> 00:20:25,000 What will happen? 296 00:20:25,000 --> 00:20:28,000 One threat will be a sleep take into account. 297 00:20:28,000 --> 00:20:35,000 I'm in this safe block because interrupted FLAC is equal to true and the threat is invading state. 298 00:20:35,000 --> 00:20:38,000 My threat will be interrupted and interrupted. 299 00:20:38,000 --> 00:20:40,000 Exceptions will be thrown. 300 00:20:40,000 --> 00:20:48,000 This line will not be executed never because interrupted exception will be thrown this mass Atheros 301 00:20:48,000 --> 00:20:52,000 interrupted exception without any handlin to the upper level. 302 00:20:53,000 --> 00:20:58,000 And here we should print interrupted in the work method and return from around Macit. 303 00:20:58,000 --> 00:21:05,000 As you can see here, I have tried Kedge Block because sleep mass my throat checked exception and work 304 00:21:05,000 --> 00:21:08,000 method doesn't handle this potential exception inside. 305 00:21:08,000 --> 00:21:11,000 Let's run this program now for two seconds. 306 00:21:11,000 --> 00:21:18,000 Program will bring to console working until threat will be interrupted and once it is interrupted, 307 00:21:18,000 --> 00:21:19,000 let's see what we have. 308 00:21:19,000 --> 00:21:26,000 The first thing that happened is threat interruption and even after that you can see that I printed 309 00:21:26,000 --> 00:21:27,000 reconvert. 310 00:21:27,000 --> 00:21:34,000 That is because I just changed interaction flac and I still need to wait until the threat will be in 311 00:21:34,000 --> 00:21:34,000 a waiting mode. 312 00:21:35,000 --> 00:21:43,000 This text in the main living tells us that Main Street finished its execution and after that we see 313 00:21:43,000 --> 00:21:44,000 that we enter it if block. 314 00:21:44,000 --> 00:21:47,000 And here is a status of interrupted flac. 315 00:21:47,000 --> 00:21:49,000 It is equal to true. 316 00:21:49,000 --> 00:21:56,000 The next step in our execution is entering the catch block and print and that threat was interrupted 317 00:21:56,000 --> 00:21:56,000 in the work. 318 00:21:56,000 --> 00:21:57,000 MassArt. 319 00:21:57,000 --> 00:21:58,000 That's it. 320 00:21:59,000 --> 00:22:05,000 So the rule of thumb, when you're creating all rain app, think about what is a logical point in your 321 00:22:05,000 --> 00:22:08,000 thread to allow other threats to interrupt it. 322 00:22:08,000 --> 00:22:12,000 In case you have such possibility to introduce this point, do it. 323 00:22:13,000 --> 00:22:16,000 Usually you would let your threat fall asleep. 324 00:22:16,000 --> 00:22:21,000 After all, resources are adjusted and state of the system is consistent. 325 00:22:21,000 --> 00:22:24,000 In this case, there is no harm to interrupt the threat. 326 00:22:25,000 --> 00:22:31,000 There are some methods that you should never use and that are marked as deprecated but not removed from 327 00:22:31,000 --> 00:22:34,000 Janicki only because of the backwards compatibility. 328 00:22:34,000 --> 00:22:38,000 Ziya, stop, suspend and resume. 329 00:22:39,000 --> 00:22:46,000 These are dangerous masses in terms they are stop, suspend or resume threat execution instantly without 330 00:22:46,000 --> 00:22:47,000 bothering. 331 00:22:47,000 --> 00:22:53,000 What process is happening right now inside the threat, taking into account not all operations at ATOMIC 332 00:22:53,000 --> 00:22:57,000 and performed with one single operation of updating data in the memory. 333 00:22:58,000 --> 00:23:01,000 You can start somewhere in the middle of data processing in your threat. 334 00:23:02,000 --> 00:23:05,000 At the meantime, resource will be updated as a threat. 335 00:23:05,000 --> 00:23:11,000 And when you will resume execution, you may end up with not consistent state of your objects. 336 00:23:11,000 --> 00:23:18,000 We will talk about atomic operations in adolescence, but I believe you understood that stopping a suspension 337 00:23:18,000 --> 00:23:25,000 and after that resume and execution of the threat in case it is made some calculation process is not 338 00:23:25,000 --> 00:23:26,000 the best idea. 339 00:23:26,000 --> 00:23:29,000 And one more interesting and lasting for today's lesson. 340 00:23:30,000 --> 00:23:34,000 Probably you saw one comment line here where I invoke June Masset. 341 00:23:35,000 --> 00:23:36,000 What is it? 342 00:23:36,000 --> 00:23:40,000 Let me uncommented first and run up one more time. 343 00:23:40,000 --> 00:23:44,000 What's changed now in this version line? 344 00:23:44,000 --> 00:23:47,000 Zatarain's in the main Leesville will always be printed. 345 00:23:47,000 --> 00:23:48,000 Lost. 346 00:23:48,000 --> 00:23:49,000 Why? 347 00:23:49,000 --> 00:23:56,000 Because Join Masset also put our main threat in great state until this threat will be completely finished. 348 00:23:56,000 --> 00:23:59,000 So I gave a command with this line to join. 349 00:23:59,000 --> 00:24:01,000 This thread does a current one. 350 00:24:02,000 --> 00:24:08,000 That's why my app will wait until Perry's threat will be completely executed during Masad. 351 00:24:08,000 --> 00:24:15,000 Also, Masroor interrupted exception, taking into account it puts current threat wait in state and 352 00:24:15,000 --> 00:24:20,000 other threats might interrupt this threat while it is innervate state. 353 00:24:20,000 --> 00:24:21,000 Is it clear? 354 00:24:21,000 --> 00:24:25,000 Basically, that's all what I wanted to share with you in this lesson. 355 00:24:26,000 --> 00:24:28,000 Let's recap what we have learned today. 356 00:24:28,000 --> 00:24:34,000 In this lesson, we learned the life cycle of the threat and how state of the threat is changed from 357 00:24:34,000 --> 00:24:36,000 the new disseminated. 358 00:24:36,000 --> 00:24:39,000 We learned five ways how to run a threat. 359 00:24:39,000 --> 00:24:43,000 Also in this lesson, we performed over a year of threat class. 360 00:24:43,000 --> 00:24:48,000 Now, you know, main properties of this type, we learned a radical interface. 361 00:24:48,000 --> 00:24:51,000 Now you know how to use it to execute a separate threat. 362 00:24:52,000 --> 00:24:55,000 And at the end of the lesson, we learned how to interrupt threat. 363 00:24:56,000 --> 00:24:59,000 Hope you enjoyed the lesson and you like the examples. 364 00:24:59,000 --> 00:25:02,000 We still have a lot of other things to learn, but. 365 00:25:02,000 --> 00:25:08,000 I think that they will have enough information to remember, make sure you understand this lesson before 366 00:25:08,000 --> 00:25:12,000 moving further, because multithreaded is not the easiest topic. 367 00:25:12,000 --> 00:25:18,000 And we need to be very careful with understanding each concept in sequential order, because it would 368 00:25:18,000 --> 00:25:22,000 be hard to understand one thing without understanding the previous one. 369 00:25:22,000 --> 00:25:23,000 Thanks a lot for your attention. 370 00:25:24,000 --> 00:25:27,000 Have a great day and see you in the next lesson.