1 00:00:00,390 --> 00:00:03,900 We can use collarbones and future task to fetch a final result. 2 00:00:07,740 --> 00:00:10,530 Runnable is not capable of returning a final result. 3 00:00:14,270 --> 00:00:16,730 But Callable can hold on to the final result. 4 00:00:21,040 --> 00:00:25,420 So the goal of this lesson is to use callable and future task to fetch a final value. 5 00:00:30,860 --> 00:00:36,230 Have another look at the runnable interface, the run method is void, which means whatever lambda expression 6 00:00:36,230 --> 00:00:38,960 that stands in for the run method will not return a value. 7 00:00:39,900 --> 00:00:42,900 The lambda expression here stands in for the run method. 8 00:00:43,290 --> 00:00:45,360 So if I were to remove the variable. 9 00:00:46,390 --> 00:00:50,650 The value that generate no returns gets swallowed into void. 10 00:00:51,250 --> 00:00:57,610 So the workaround was to basically inject a class level variable inside the expression and set it equal 11 00:00:57,610 --> 00:01:01,300 to the result of generate number so that it doesn't get lost in the void. 12 00:01:01,810 --> 00:01:02,950 But that's a really dirty. 13 00:01:02,950 --> 00:01:04,060 I don't like this one bit. 14 00:01:04,540 --> 00:01:07,510 If your task returns a result, use a callable. 15 00:01:09,500 --> 00:01:14,630 The callable interface has one method named Karl, which returns a value, and this generic gives us 16 00:01:14,630 --> 00:01:19,730 flexibility in the sense that if my callable is of type double, then the call method returns a double. 17 00:01:23,380 --> 00:01:27,010 If Michael LaBelle is of type integer, then the call method returns an integer. 18 00:01:30,310 --> 00:01:32,500 So here you have to specify the callable type. 19 00:01:35,370 --> 00:01:39,060 And whatever lambda expression you put here will stand in for the kill method. 20 00:01:43,410 --> 00:01:45,180 So here you have to specify a Colaba. 21 00:01:46,460 --> 00:01:49,580 Followed by the collar type in this case, it's going to be a double. 22 00:01:52,000 --> 00:01:58,330 Accordingly, I'll call this Colaba, and then whatever lambda expression you put here is going to stand 23 00:01:58,330 --> 00:01:59,440 in for the call method. 24 00:02:01,150 --> 00:02:03,640 With that being said, we do not need this anymore. 25 00:02:04,540 --> 00:02:05,680 We can say callable. 26 00:02:07,160 --> 00:02:10,850 And here I have to specify the collabos type in this case, double. 27 00:02:12,550 --> 00:02:18,880 And you should probably call this callable now and now, whatever expression you put here is going to 28 00:02:18,880 --> 00:02:20,350 stand in for the call method. 29 00:02:20,680 --> 00:02:23,950 In our case, we know our callable is going to return a double value. 30 00:02:24,490 --> 00:02:25,960 Let me just import callable. 31 00:02:28,440 --> 00:02:32,520 OK, now, if I were to say, Hey, thread, can you run this callable? 32 00:02:33,650 --> 00:02:34,550 We would get an error. 33 00:02:35,440 --> 00:02:39,130 Unfortunately, threads are not designed to return a result. 34 00:02:39,640 --> 00:02:42,070 You can only create a thread out of a runnable. 35 00:02:43,540 --> 00:02:46,810 Well, thankfully, future task implements the runnable interface. 36 00:02:47,080 --> 00:02:50,830 So by virtue of polymorphism, future task can behave as a runnable. 37 00:02:53,520 --> 00:02:59,340 So as long as we wrap our callable around a future task, then create a thread out of the future task, 38 00:02:59,340 --> 00:03:00,120 then we should be good. 39 00:03:00,540 --> 00:03:01,890 And doing that is really easy. 40 00:03:01,900 --> 00:03:04,380 All we got to say is future task. 41 00:03:06,610 --> 00:03:07,120 Double. 42 00:03:11,310 --> 00:03:15,780 We'll call that future is equal to a new future task. 43 00:03:19,200 --> 00:03:19,770 And that's all. 44 00:03:25,780 --> 00:03:30,400 And now our future task, it's going to tell me, OK, you need to wait for the task to finish and then 45 00:03:30,400 --> 00:03:31,960 I'm going to grab the final results for you. 46 00:03:32,440 --> 00:03:33,400 How is it going to do that? 47 00:03:34,000 --> 00:03:36,530 Future task has a method for future tasks. 48 00:03:36,530 --> 00:03:40,810 Get, get, wait for the task to finish and grabs the final results. 49 00:03:42,670 --> 00:03:48,360 So here we can say future dagat, and once the runtime for the main thread reaches this line, FutureGen 50 00:03:48,370 --> 00:03:50,890 is going to block the main thread until our task finishes. 51 00:03:51,280 --> 00:03:55,630 Once the task gets fully executed, it returns the final value. 52 00:03:56,020 --> 00:04:01,750 Our future gut is going to grab the final value from inside the main thread and then hear that result 53 00:04:01,750 --> 00:04:02,590 gets used up. 54 00:04:04,820 --> 00:04:08,240 So here we can see results is equal to future. 55 00:04:08,430 --> 00:04:09,110 Don't get. 56 00:04:10,930 --> 00:04:13,780 On handled exception, type, execution exception. 57 00:04:14,170 --> 00:04:19,690 And for the sake of expediency, let's just catch whatever exception gets thrown at us here. 58 00:04:20,500 --> 00:04:26,140 So what's going to happen is once the runtime for the main thread reaches this line, future dagat is 59 00:04:26,140 --> 00:04:29,020 going to block the main thread until our task finishes. 60 00:04:29,380 --> 00:04:35,050 And once the callable task gets fully executed, it retains the final value it holds on to it. 61 00:04:35,660 --> 00:04:42,280 Our featured guest line is going to grab the final value from inside the main thread and then here that 62 00:04:42,280 --> 00:04:43,720 result is going to get used up. 63 00:04:45,000 --> 00:04:50,010 The fire and the app, while this heavy code is running in the background, I'm just interacting with 64 00:04:50,010 --> 00:04:50,760 the application. 65 00:04:51,870 --> 00:04:52,920 My name is Ryan. 66 00:04:54,360 --> 00:04:55,740 Still running, which is fine. 67 00:04:57,160 --> 00:04:59,580 And the computer returned a value of point five. 68 00:05:00,920 --> 00:05:01,430 Very good. 69 00:05:05,000 --> 00:05:11,780 To recap, use callable and future task if the task returns a final result as the task executes. 70 00:05:14,760 --> 00:05:17,000 Future task gets the final results.