1 00:00:05,000 --> 00:00:10,000 Hello there, students, in this lesson, we are going to learn with your fork join framework, we'll 2 00:00:10,000 --> 00:00:16,000 start from the high level overview of your framework and understanding of its basic principles. 3 00:00:17,000 --> 00:00:20,000 One will understand the theory of Forkin and Journey. 4 00:00:20,000 --> 00:00:24,000 We would jump straight to the source code to learn first joint pool class. 5 00:00:24,000 --> 00:00:30,000 I've explained what works to an algorithm is after that we are going to have practical code examples 6 00:00:30,000 --> 00:00:36,000 and I will show you how to submit tasks for execution with the help of furniture and pool will review 7 00:00:36,000 --> 00:00:39,000 recursive action and recursive type tasks. 8 00:00:40,000 --> 00:00:45,000 And at the end of the lesson we are going to discuss drawbacks of Thorbjoern framework that we need 9 00:00:45,000 --> 00:00:49,000 to be aware of for making a decision about its usage. 10 00:00:49,000 --> 00:00:53,000 Let's start from understanding what Furbies during the framework is. 11 00:00:53,000 --> 00:00:56,000 The first time it was presented in Java version seven. 12 00:00:57,000 --> 00:00:58,000 Why it was needed. 13 00:00:58,000 --> 00:01:03,000 The framework helps to speed up parallel processing by using all available course. 14 00:01:03,000 --> 00:01:08,000 It is designed for work that can be broken into smaller pieces recursively. 15 00:01:09,000 --> 00:01:14,000 The goal is to use all the available processing power to enhance the performance of your application. 16 00:01:15,000 --> 00:01:20,000 Why it is called for joins and it is named after the way how it works. 17 00:01:20,000 --> 00:01:27,000 The Framework X recursively breaking the task into smaller independent subtasks until they're simple 18 00:01:27,000 --> 00:01:29,000 enough to be executed asynchronously. 19 00:01:30,000 --> 00:01:35,000 And after that result of all subtasks, I recursively journey into a single result. 20 00:01:36,000 --> 00:01:37,000 This is due in part. 21 00:01:38,000 --> 00:01:40,000 That's why the framework is called for. 22 00:01:40,000 --> 00:01:42,000 Join the Fork. 23 00:01:42,000 --> 00:01:45,000 Join Framework distributes tasks to various threads in the thread. 24 00:01:45,000 --> 00:01:50,000 Pull workers that are sleeping or waiting, not actually sleeping and waiting. 25 00:01:50,000 --> 00:01:57,000 They're working on other smaller tasks for joint users work stealing algorithm workers. 26 00:01:57,000 --> 00:02:03,000 Threads that run out of things to do can steal tasks from other threats that are still busy. 27 00:02:03,000 --> 00:02:08,000 For example, doesn't create a separate thread for every single subtask. 28 00:02:08,000 --> 00:02:12,000 Instead, each threatened ZAPU has its own double ended. 29 00:02:12,000 --> 00:02:15,000 Q Modak, which stauss tasks. 30 00:02:16,000 --> 00:02:17,000 Let's talk a little bit more about work. 31 00:02:17,000 --> 00:02:19,000 Stulen algorithm. 32 00:02:19,000 --> 00:02:26,000 If I could add a simple thread that if we try to steal work from decks of business threats by default, 33 00:02:26,000 --> 00:02:30,000 every threat gets tasks from the head of its own deck. 34 00:02:30,000 --> 00:02:37,000 When it is empty, the threat takes that task from the tail of the deck of another business threat or 35 00:02:37,000 --> 00:02:39,000 from the global entry queue. 36 00:02:39,000 --> 00:02:45,000 Since this is who has the biggest pieces of work are likely to be located today, we're going to review 37 00:02:45,000 --> 00:02:48,000 a lot of JDK source code and code examples. 38 00:02:48,000 --> 00:02:50,000 So let me start screen sharing. 39 00:02:51,000 --> 00:02:58,000 The center of Fergin framework is, for example, Class I open source code of this class similar to 40 00:02:58,000 --> 00:02:59,000 Oza Executor's. 41 00:02:59,000 --> 00:03:07,000 It extends abasic executive service class that in turn implements executer service allsorts in the CPU 42 00:03:07,000 --> 00:03:14,000 attempt to find and execute tasks submitted to the CPU and or created by other active tasks that that 43 00:03:15,000 --> 00:03:16,000 a blocked or waiting. 44 00:03:16,000 --> 00:03:17,000 At the moment. 45 00:03:17,000 --> 00:03:24,000 This enables efficient processing when most tasks spawn other subtasks, as well as when many small 46 00:03:24,000 --> 00:03:27,000 tasks are submitted to the CPU from external clients. 47 00:03:27,000 --> 00:03:29,000 Let me open now. 48 00:03:29,000 --> 00:03:32,000 For example, demo class I created IT. 49 00:03:32,000 --> 00:03:38,000 Infrastructure and package done for you will be able to find the reference to the source code in attachments 50 00:03:38,000 --> 00:03:39,000 to the lesson. 51 00:03:39,000 --> 00:03:42,000 I am going to walk you through each line in this demo file. 52 00:03:43,000 --> 00:03:47,000 The first thing that we do here is create an offer of joint pull object. 53 00:03:48,000 --> 00:03:50,000 We can do this in different ways. 54 00:03:50,000 --> 00:03:53,000 The first one is to use Static Masset Common Pool. 55 00:03:53,000 --> 00:03:56,000 This will provide a reference to the common pool. 56 00:03:56,000 --> 00:04:03,000 This pool and any ongoing process are automatically terminated upon program termination in case we'll 57 00:04:03,000 --> 00:04:05,000 call X method of system class, for example. 58 00:04:06,000 --> 00:04:11,000 Another way to create an instance of, for example, is just call upon the constructor. 59 00:04:11,000 --> 00:04:14,000 The int parameter describes parallelism level. 60 00:04:14,000 --> 00:04:21,000 This level indicates how many threads or CPUs you want to work concurrently on tasks past the function 61 00:04:21,000 --> 00:04:22,000 pool. 62 00:04:22,000 --> 00:04:25,000 In this case we have parallel is level of four. 63 00:04:26,000 --> 00:04:29,000 We can create four adjourned without passing in value. 64 00:04:29,000 --> 00:04:35,000 In this case, parallelism is equal to the end value that is returned by available processers. 65 00:04:35,000 --> 00:04:42,000 Masset they remember in previous lesson I showed you a verbal processor that we can call on the runtime 66 00:04:42,000 --> 00:04:46,000 object to define the number of processing, of course, we have on the machine. 67 00:04:47,000 --> 00:04:53,000 Basically, if you don't have any specific reasons to set specific level of parallelism, you may use 68 00:04:53,000 --> 00:04:54,000 default constructor. 69 00:04:54,000 --> 00:05:00,000 Also, there is another constructor that allows it to pass to the constructor, sweat factory and error 70 00:05:00,000 --> 00:05:00,000 handler. 71 00:05:01,000 --> 00:05:03,000 Well, would you review similar example? 72 00:05:03,000 --> 00:05:09,000 Also, sweat factory in previous lesson and another argument of type, unquote, exception handler is 73 00:05:09,000 --> 00:05:15,000 a functional interface that requires you to implement logic of handling exceptional cases during the 74 00:05:15,000 --> 00:05:16,000 execution. 75 00:05:16,000 --> 00:05:19,000 It will depend on how you would like to address the exception. 76 00:05:20,000 --> 00:05:27,000 Now we can proceed with submitting a task to our executer, similar to Oprah as examples with Runnable 77 00:05:27,000 --> 00:05:32,000 and Kobel, we can submit two types of tasks actions and tasks. 78 00:05:32,000 --> 00:05:33,000 Action. 79 00:05:33,000 --> 00:05:37,000 This is something one doesn't return any value after execution. 80 00:05:37,000 --> 00:05:42,000 Just set of instructions that needs to be executed in a separate sweat and task. 81 00:05:43,000 --> 00:05:47,000 It is a type that will change the result of the execution similar to callable type. 82 00:05:48,000 --> 00:05:54,000 Recursive action is a type that will not return as a result of the execution and objects of recursive 83 00:05:54,000 --> 00:05:56,000 task type will return result of execution. 84 00:05:57,000 --> 00:06:03,000 Most of them has an absolute mass compute in which the task logic is defined also. 85 00:06:03,000 --> 00:06:06,000 Most of these types extends for the joint task. 86 00:06:06,000 --> 00:06:09,000 Let's review examples with each of these types. 87 00:06:10,000 --> 00:06:15,000 But before we jump to review code example, let me share a basic template for our tasks. 88 00:06:16,000 --> 00:06:16,000 On the slide. 89 00:06:16,000 --> 00:06:18,000 You can see pseudocode. 90 00:06:19,000 --> 00:06:27,000 If my portion of the work is small enough, does it work directly or else split my work into two pieces 91 00:06:27,000 --> 00:06:30,000 in one of the two pieces and wait for the results? 92 00:06:30,000 --> 00:06:33,000 That is how we should implement compute method. 93 00:06:34,000 --> 00:06:39,000 There should be some conditions that will tell us whether we can proceed with the execution directly 94 00:06:39,000 --> 00:06:40,000 or we should create some. 95 00:06:41,000 --> 00:06:43,000 Now let's look at the good example. 96 00:06:44,000 --> 00:06:50,000 So as you may already understand, recursive action is just a set of instructions that are needed to 97 00:06:50,000 --> 00:06:50,000 be done. 98 00:06:51,000 --> 00:06:57,000 And recursive action may still need to break up its work into smaller chunks, which can be executed 99 00:06:57,000 --> 00:06:59,000 by independent threads or CPU's. 100 00:07:00,000 --> 00:07:04,000 Recursive action is an absolute class that extends for joint task. 101 00:07:04,000 --> 00:07:11,000 So to create an object of this type, we need to have concrete implementation of this type in a separate 102 00:07:11,000 --> 00:07:16,000 file of default recursive action clause that extends recursive action. 103 00:07:16,000 --> 00:07:22,000 This is not a very meaningful example, but I decided to keep it super simple in order you would understand 104 00:07:22,000 --> 00:07:22,000 how it works. 105 00:07:23,000 --> 00:07:26,000 We have private in the field with named workload. 106 00:07:26,000 --> 00:07:32,000 We would initialize it with some value during the default recursive action object instantiation. 107 00:07:32,000 --> 00:07:38,000 And here is implementation of our compute method like you saw in template implementation. 108 00:07:38,000 --> 00:07:38,000 On the slide. 109 00:07:39,000 --> 00:07:44,000 We start with definition of a condition that should describe what we are going to proceed with. 110 00:07:44,000 --> 00:07:44,000 Action. 111 00:07:44,000 --> 00:07:49,000 Execution was a threat and one we should split our action into multiple subtasks. 112 00:07:50,000 --> 00:07:57,000 In this case, we check if the workload value is less than 18, 18 in this case is a random threshold. 113 00:07:58,000 --> 00:08:05,000 Just for the sake of example, when workload is less than 18, we just pursued action execution in the 114 00:08:05,000 --> 00:08:05,000 same thread. 115 00:08:06,000 --> 00:08:13,000 But in case it is equal to itchin or more, then we jump to our section where we create subtasks and 116 00:08:13,000 --> 00:08:14,000 call for massive. 117 00:08:14,000 --> 00:08:19,000 Here you can see two alternative ways how to achieve the same result. 118 00:08:19,000 --> 00:08:25,000 We can create subtasks and call for method for each subtask like you can see in the foreach loop. 119 00:08:25,000 --> 00:08:33,000 Or alternatively, we can use for a joint task class and call invoke all method and pass all subtasks 120 00:08:33,000 --> 00:08:35,000 that the result would be the same. 121 00:08:36,000 --> 00:08:41,000 I decided to keep for each loop here because the visualization is better in this case. 122 00:08:41,000 --> 00:08:46,000 Let's understand how we create subtasks basically for our template. 123 00:08:46,000 --> 00:08:50,000 We split my work into two pieces here. 124 00:08:50,000 --> 00:08:57,000 You can see that I create two objects of the same action type and in constructor I pass workload divided 125 00:08:57,000 --> 00:08:57,000 by two. 126 00:08:58,000 --> 00:09:02,000 I add all subtasks to the release and return it from the method. 127 00:09:02,000 --> 00:09:09,000 We use this list and for each loop or in invoke all Masset to submit this action for execution. 128 00:09:09,000 --> 00:09:11,000 Let's get back to the, for example, demo class. 129 00:09:12,000 --> 00:09:18,000 I just create an instance of my default recursive action and I pass it to the constructor. 130 00:09:18,000 --> 00:09:22,000 I call invoke method and pass the reference to my action object. 131 00:09:23,000 --> 00:09:28,000 Let's run this program and you can see that I split the workload and created two subtasks. 132 00:09:29,000 --> 00:09:34,000 To sum it up, in your action task, you need to find a way to reflect the total amount of work. 133 00:09:35,000 --> 00:09:40,000 Also, you need to define suitable thresholds that will be used to split action in subtasks. 134 00:09:40,000 --> 00:09:41,000 Like another example. 135 00:09:41,000 --> 00:09:45,000 This can be separate Masset that knows how to divide the work. 136 00:09:46,000 --> 00:09:51,000 And in implementation of compute method, you should describe the conditions that would define what 137 00:09:51,000 --> 00:09:55,000 it is time to split work into subtasks or proceed with execution. 138 00:09:56,000 --> 00:09:58,000 Hope that it is clear. 139 00:09:58,000 --> 00:10:02,000 Now let's look at the second type of tasks that we can submit into. 140 00:10:02,000 --> 00:10:10,000 For example, now we are going to recursive task, recursive task is also APSA class that extends for 141 00:10:10,000 --> 00:10:11,000 a joint task. 142 00:10:11,000 --> 00:10:16,000 We want to submit a recursive task when we want to get the result of the calculation and the response. 143 00:10:16,000 --> 00:10:22,000 The logic of interaction with objects of this type is similar was on the one difference. 144 00:10:22,000 --> 00:10:26,000 We have to join the results of computation from different set tasks. 145 00:10:26,000 --> 00:10:33,000 Let me open default recursive task class that extends recursive tasks and that implements compute method. 146 00:10:33,000 --> 00:10:39,000 As you can see, we have similar constructor and the same field as we had in recursive action. 147 00:10:39,000 --> 00:10:46,000 In case our condition tells us that we shouldn't split activity in different tasks, we just return 148 00:10:46,000 --> 00:10:46,000 the result. 149 00:10:47,000 --> 00:10:52,000 In this example, we just want to get values that twice the original value in the case. 150 00:10:52,000 --> 00:10:54,000 We need to split our work into subtasks. 151 00:10:55,000 --> 00:11:00,000 We do this, but also we need to join the results of all subtasks. 152 00:11:00,000 --> 00:11:06,000 I call Gern Masset on each subtask that's supposed to return the result of computation. 153 00:11:06,000 --> 00:11:12,000 I aggregate result in one variable and after that I return the result to them. 154 00:11:12,000 --> 00:11:15,000 This case I have, for example, demo two class. 155 00:11:15,000 --> 00:11:20,000 I create an instance of, for example, and call invoke method. 156 00:11:20,000 --> 00:11:25,000 I pass an instance of the false recursive task with workload 40. 157 00:11:25,000 --> 00:11:28,000 I run the program and Incans so output. 158 00:11:28,000 --> 00:11:31,000 You can see that I get 80 as a result. 159 00:11:32,000 --> 00:11:34,000 That is exactly what my task is supposed to do. 160 00:11:35,000 --> 00:11:35,000 Great. 161 00:11:36,000 --> 00:11:39,000 Is it clear for you how the recursive action and recursive task work? 162 00:11:40,000 --> 00:11:45,000 If yes, then we are good to proceed in case the things that are still not clear. 163 00:11:45,000 --> 00:11:49,000 Please leave your question under the switch and I will be happy to answer. 164 00:11:50,000 --> 00:11:56,000 Now we are going to learn more about such factory method from executor's class as new for stealing pool 165 00:11:56,000 --> 00:11:56,000 pull. 166 00:11:57,000 --> 00:12:04,000 This method is overloaded and has versions within parameter and without parameters in the source code 167 00:12:04,000 --> 00:12:05,000 of executor's class. 168 00:12:05,000 --> 00:12:11,000 You can see that this is nothing more than, for example, instantiate that with a specific level of 169 00:12:11,000 --> 00:12:18,000 parallelism or with the default level of parallelism that is equal to the number of available processors 170 00:12:18,000 --> 00:12:19,000 and how to work with. 171 00:12:19,000 --> 00:12:25,000 For example, you already know and we reviewed examples, I believe that you already understood potential 172 00:12:25,000 --> 00:12:32,000 benefits that we expect to get from, for example, effective usage of all course, by splitting tasks 173 00:12:32,000 --> 00:12:35,000 into subtasks, supposed to improve performance. 174 00:12:35,000 --> 00:12:38,000 But we would help us in all cases. 175 00:12:38,000 --> 00:12:44,000 Let's talk about disadvantages of, for example, probably after this lesson, you got an impression 176 00:12:44,000 --> 00:12:50,000 that Fork Join framework is something extremely cool, efficient and super fast. 177 00:12:50,000 --> 00:12:54,000 Probably even started to think that you have to use it. 178 00:12:54,000 --> 00:12:55,000 Most often. 179 00:12:55,000 --> 00:12:59,000 The evolution of Fergin pool vary between different engineers. 180 00:13:00,000 --> 00:13:03,000 Not everyone agreed and shared benefits of this type. 181 00:13:04,000 --> 00:13:09,000 If you would search the Internet pros and cons of, for example, you would discover that a group of 182 00:13:09,000 --> 00:13:15,000 people who doesn't like, for example, because of different reasons on the slide, you can see the 183 00:13:15,000 --> 00:13:20,000 list of the issues and drawbacks of, for example, based on different people opinion. 184 00:13:21,000 --> 00:13:25,000 So it makes sense in some degree and in my opinion, worse to share with you. 185 00:13:26,000 --> 00:13:30,000 So the drawbacks of, for example, are a faulty task manager. 186 00:13:31,000 --> 00:13:36,000 The foraging technique splits the work into fragments and joins the results together. 187 00:13:37,000 --> 00:13:44,000 And some practice that allows an intermediate joint for each fork can only work successfully in a controlled 188 00:13:44,000 --> 00:13:44,000 environment. 189 00:13:45,000 --> 00:13:52,000 A fatal flaw with Fergin framework by opinion of some group of people is that it is trying to manage 190 00:13:52,000 --> 00:13:53,000 and intimidate. 191 00:13:53,000 --> 00:13:56,000 Join in a task outside of a controlled environment. 192 00:13:56,000 --> 00:14:04,000 Only the operating system as c the operating system can manage and intimidate join in the task and usually 193 00:14:04,000 --> 00:14:09,000 there is a problem of errors, exceptions in subsequently FEG tasks. 194 00:14:09,000 --> 00:14:14,000 And the answer to most of the cases that may happen in real life is uncertainty. 195 00:14:15,000 --> 00:14:21,000 But this is not why people choose drama when we want to create robust application within the tools and 196 00:14:21,000 --> 00:14:23,000 techniques to deal with uncertainties. 197 00:14:24,000 --> 00:14:31,000 Inefficient, since all four tasks go into the same work as Red Deck, this team in various threads 198 00:14:31,000 --> 00:14:35,000 will fight each other at the top of the deck over the four tasks. 199 00:14:36,000 --> 00:14:42,000 This framework can never function efficiently in high performance environment, special purpose. 200 00:14:43,000 --> 00:14:46,000 There are some restrictions to follow to make sure we can benefit from using. 201 00:14:46,000 --> 00:14:51,000 For example, recommended restrictions must be plain. 202 00:14:51,000 --> 00:14:58,000 That is mean between one hundred and ten thousand basic computational steps in the compute masset compute 203 00:14:58,000 --> 00:14:59,000 intensive code only. 204 00:15:00,000 --> 00:15:02,000 No blockin, no input. 205 00:15:02,000 --> 00:15:10,000 Output, no synchronization, slow and unscalable, only works well on a small number of processors, 206 00:15:10,000 --> 00:15:14,000 this design can never scale to hundreds or thousands of processors. 207 00:15:15,000 --> 00:15:23,000 The overhead and reservation would nullify the benefits of additional processors exceedingly complex. 208 00:15:23,000 --> 00:15:26,000 You can spend more time if you wish to investigate. 209 00:15:26,000 --> 00:15:32,000 The source code of Thorbjoern framework was all Nasta types and how it is connected with the rest of 210 00:15:32,000 --> 00:15:34,000 concurrent tools and JDK. 211 00:15:34,000 --> 00:15:41,000 And you would agree that this is not the easiest and clear for understanding piece of JDK lacking in 212 00:15:41,000 --> 00:15:42,000 professional attributes. 213 00:15:42,000 --> 00:15:48,000 As a professional software engineer, as we always striving for excellence and following the best practices 214 00:15:48,000 --> 00:15:53,000 some of them are, programs fail usually as a worst possible time. 215 00:15:53,000 --> 00:15:57,000 Consequently, error recovery is paramount. 216 00:15:57,000 --> 00:16:00,000 Software needs Deunan balancing of resources. 217 00:16:01,000 --> 00:16:08,000 Administrators sometimes need classification, also called alerting logs and necessary. 218 00:16:08,000 --> 00:16:12,000 The software should be useful across a wide range of computer needs. 219 00:16:13,000 --> 00:16:18,000 The Fork Join Framework has none of this inadequate in scope. 220 00:16:18,000 --> 00:16:25,000 The forging framework is only designed to work in a trivial segment of the computing world on Java standards 221 00:16:25,000 --> 00:16:28,000 addition only on high performance workstations. 222 00:16:28,000 --> 00:16:36,000 Six plus CPUs without high persuading do not compute, only work with no task input output, no Intertrust 223 00:16:36,000 --> 00:16:38,000 concurrency and no networking. 224 00:16:39,000 --> 00:16:43,000 So this is just some of the criticism against for of joint pool. 225 00:16:44,000 --> 00:16:48,000 At the end of the day, it will be up to you how often and when to use it. 226 00:16:48,000 --> 00:16:52,000 And now you have all necessary information to make this decision. 227 00:16:52,000 --> 00:16:55,000 Let's recap what we have learned in this lesson. 228 00:16:55,000 --> 00:17:01,000 In this lesson we learned what Fork Join framework is now, you know, the main masses of, for example, 229 00:17:01,000 --> 00:17:03,000 class in the lesson. 230 00:17:03,000 --> 00:17:10,000 We had some examples where the recursive action and recursive task, we executed these types of tasks 231 00:17:10,000 --> 00:17:12,000 with the help of our gene pool. 232 00:17:12,000 --> 00:17:16,000 And at the end of the lesson, we have discussed Fergin framework drawbacks. 233 00:17:17,000 --> 00:17:18,000 That's it for this lesson. 234 00:17:18,000 --> 00:17:20,000 Thanks a lot for your attention. 235 00:17:20,000 --> 00:17:23,000 Have a great day and see you in the next lesson.