1 00:00:00,630 --> 00:00:03,810 Join allows a thread to wait for another thread. 2 00:00:05,830 --> 00:00:11,860 The main thread may need to wait for the background thread to finish and join allows a thread to wait 3 00:00:11,860 --> 00:00:12,550 for another. 4 00:00:16,540 --> 00:00:19,780 In this lesson, you will learn when and how to use the join method. 5 00:00:23,750 --> 00:00:27,980 First, open up the folder for this lesson by following this path in your course resources. 6 00:00:33,540 --> 00:00:39,420 We need to create a function that generates a number as close as possible to the targets with a precision 7 00:00:39,420 --> 00:00:41,910 of however many decimal places that is. 8 00:00:43,560 --> 00:00:45,450 So right here, we'll create a function. 9 00:00:46,230 --> 00:00:48,810 Public static returns a double. 10 00:00:50,940 --> 00:00:52,740 Called Generate No. 11 00:00:59,610 --> 00:01:04,680 OK, and inside the function, we need to generate a number as close as possible to the target. 12 00:01:05,160 --> 00:01:07,320 So first, the number that we get. 13 00:01:08,360 --> 00:01:09,980 Is going to be from the random. 14 00:01:13,240 --> 00:01:19,210 And now what I want to do is get the difference between the target and the number that gets generated, 15 00:01:19,600 --> 00:01:23,770 so what I'll do is I'm going to cut another function public static. 16 00:01:24,600 --> 00:01:26,970 Double call difference. 17 00:01:29,540 --> 00:01:31,730 This function is going to receive a number. 18 00:01:33,400 --> 00:01:34,310 Double, no. 19 00:01:37,670 --> 00:01:43,910 And it's going to return the difference between the target and the number that gets passed on target. 20 00:01:44,950 --> 00:01:46,240 Minus no. 21 00:01:47,420 --> 00:01:52,400 Now, if the no happens to be higher than the target, we don't want to return a negative value. 22 00:01:53,240 --> 00:01:57,740 So what I'll do is I'll take the absolute value so that the difference returned is always positive. 23 00:01:59,280 --> 00:01:59,720 OK. 24 00:02:00,060 --> 00:02:02,490 And now I can say double difference. 25 00:02:04,490 --> 00:02:06,650 Is equal to the return value from difference. 26 00:02:08,690 --> 00:02:09,990 And now a critter while loop. 27 00:02:11,310 --> 00:02:14,910 While the difference is bigger than the precision. 28 00:02:16,460 --> 00:02:18,680 Then we need to generate another random number. 29 00:02:21,800 --> 00:02:23,480 And recalculate the difference. 30 00:02:27,900 --> 00:02:33,030 And so until that difference is less than zero point zero zero zero zero zero one. 31 00:02:33,450 --> 00:02:34,980 This loop is going to keep running. 32 00:02:35,310 --> 00:02:37,050 We seem to have an error. 33 00:02:37,740 --> 00:02:39,570 Uh, this should be capitalized. 34 00:02:42,800 --> 00:02:47,870 And once the while loop breaks, we know that our number is as close as it's going to get to the target. 35 00:02:48,590 --> 00:02:49,580 We'll return it. 36 00:02:56,650 --> 00:03:02,590 This function should run for a pretty long time because such a precision is going to be hard to get. 37 00:03:03,740 --> 00:03:10,160 Back in Maine, I'm going to call generate number here, I'm going to say double result is equal to 38 00:03:10,190 --> 00:03:11,300 generate no. 39 00:03:14,590 --> 00:03:17,500 And then I'm going to calculate the actual precision right here. 40 00:03:17,770 --> 00:03:24,250 Double precision is equal to the difference between the resulting number and the targets. 41 00:03:27,530 --> 00:03:29,840 Will fill these placeholders inappropriately. 42 00:03:43,380 --> 00:03:46,290 And this is taking way too long to produce a number. 43 00:03:49,200 --> 00:03:55,210 And wow, that's pretty impressive, it eventually generated a value to a precision of 10 decimal places. 44 00:03:55,230 --> 00:03:56,070 That's pretty good. 45 00:03:56,820 --> 00:03:59,670 But it took way too long to produce that number. 46 00:04:00,210 --> 00:04:03,360 This is considered a time intensive task. 47 00:04:04,230 --> 00:04:07,740 Remember that time intensive tasks should not run on the main thread. 48 00:04:08,160 --> 00:04:09,930 They should run on another thread. 49 00:04:13,980 --> 00:04:18,870 So what I'm going to do for now is is set this equal to zero because we cannot call generate number 50 00:04:18,870 --> 00:04:19,769 on the main thread. 51 00:04:21,610 --> 00:04:26,080 Now, inside may not Java, I'm going to create a new object of the thread class. 52 00:04:31,140 --> 00:04:33,570 This thread will execute one runnable task. 53 00:04:34,470 --> 00:04:38,370 And now we have to create a runnable object, and we can do that using a lambda expression. 54 00:04:42,250 --> 00:04:49,120 Runnable is equal to and when Runnable Run eventually gets called, it's going to pass and no arguments, 55 00:04:49,120 --> 00:04:53,290 so it follows that our lambda expression is going to receive no parameters. 56 00:04:53,650 --> 00:04:57,730 The arrow key is going to point to a block of code that's going to perform a task. 57 00:04:58,000 --> 00:04:59,920 And that task is generate. 58 00:04:59,920 --> 00:05:06,370 No, we're creating an object of a class that we haven't even defined that implements the runnable interface. 59 00:05:07,060 --> 00:05:10,570 Anyways, when this task gets executed, it's going to return a number. 60 00:05:11,580 --> 00:05:13,140 Until we can see that result. 61 00:05:14,510 --> 00:05:15,770 Equal to the return value. 62 00:05:17,450 --> 00:05:23,570 Now we get an error, and the issue is that we cannot use a local variable inside the expression unless 63 00:05:23,570 --> 00:05:24,260 it's final. 64 00:05:24,480 --> 00:05:29,330 That's not something that we can do, so the trick is to just define the variable at the class level 65 00:05:29,330 --> 00:05:30,470 so that it's not local. 66 00:05:31,900 --> 00:05:36,370 But now you cannot access a non static variable inside of a static method. 67 00:05:36,610 --> 00:05:38,410 So we have to make this static as well. 68 00:05:40,620 --> 00:05:41,910 And that's really it. 69 00:05:42,120 --> 00:05:43,140 Now we can call. 70 00:05:44,600 --> 00:05:45,980 The red dots start. 71 00:05:48,870 --> 00:05:53,880 Which behind the scenes is going to call a runnable run, which performs our long running task on a 72 00:05:53,880 --> 00:05:54,660 different thread. 73 00:05:55,350 --> 00:05:56,460 And if we try it out. 74 00:05:59,830 --> 00:06:01,410 I hope you can see what the problem is. 75 00:06:02,190 --> 00:06:07,890 The long task is running on one thread while our other code is running on the main thread, the main 76 00:06:07,890 --> 00:06:10,170 thread executes its code a lot faster. 77 00:06:10,500 --> 00:06:13,200 It doesn't wait for thread to to finish. 78 00:06:13,680 --> 00:06:19,080 The main thread is running this code, not waiting for Thread two to finish and to return the results 79 00:06:19,740 --> 00:06:21,030 so after a few seconds. 80 00:06:21,120 --> 00:06:26,490 Thread two eventually finishes updates the results, but we were supposed to use that result in the 81 00:06:26,490 --> 00:06:27,570 main thread before. 82 00:06:27,810 --> 00:06:33,060 But the main thread finished executing a long time ago, and this is where the join method comes into 83 00:06:33,060 --> 00:06:33,390 play. 84 00:06:34,170 --> 00:06:36,630 Join Method waits for a thread to die. 85 00:06:38,620 --> 00:06:40,120 Here, if I say. 86 00:06:42,080 --> 00:06:43,670 Threat to join. 87 00:06:45,650 --> 00:06:48,830 Which throws an interrupted exception, so we'll try to run the code. 88 00:06:50,780 --> 00:06:52,900 Catch the exception if the code fails. 89 00:07:02,360 --> 00:07:06,950 If we do that, the main threat is waiting for a threat to to die in after it dies. 90 00:07:08,490 --> 00:07:09,510 Prince, the values. 91 00:07:11,410 --> 00:07:13,930 We're making progress on more than one task at a time. 92 00:07:14,230 --> 00:07:18,430 The long running task is running in the background in this line of code is telling the main thread OK, 93 00:07:18,610 --> 00:07:24,220 we still need to wait for thread to die before you can run these lines of code because they depend on 94 00:07:24,220 --> 00:07:25,420 the updated results. 95 00:07:28,660 --> 00:07:35,590 So if I were to have some scanner related code right here, scanner scan is equal to a new object of 96 00:07:35,590 --> 00:07:38,110 the scanner class that receives input from the system. 97 00:07:39,130 --> 00:07:40,390 And now if I were to say. 98 00:07:44,580 --> 00:07:45,870 Please enter a name. 99 00:07:47,840 --> 00:07:49,130 To generate a number. 100 00:07:58,150 --> 00:07:59,410 Scanned up close. 101 00:08:03,030 --> 00:08:09,450 So here, as the user is entering their name, this task is running in the background once they finally 102 00:08:09,450 --> 00:08:15,450 enter their name or background tasks have already finished a long time ago and our values get printed 103 00:08:15,450 --> 00:08:16,170 out right away. 104 00:08:16,710 --> 00:08:21,360 But we still need the thread to join as a precaution because you cannot guarantee that the thread is 105 00:08:21,360 --> 00:08:23,910 going to finish running before the user enters their name. 106 00:08:24,360 --> 00:08:26,730 In any case, take home messages. 107 00:08:27,090 --> 00:08:32,159 The long running task is running in the background thread, which leaves us to do whatever we want in 108 00:08:32,159 --> 00:08:32,880 the main thread. 109 00:08:35,919 --> 00:08:41,890 Let's recap calling thread join waits for a threat to die time intensive tasks should not block the 110 00:08:41,890 --> 00:08:42,460 main thread. 111 00:08:42,909 --> 00:08:45,730 Time intensive tasks should run on another thread. 112 00:08:46,660 --> 00:08:50,830 Use join when code from one thread depends on the result of another thread. 113 00:08:51,340 --> 00:08:54,850 In other words, when a thread must wait for another threat to finish.