File size: 35,725 Bytes
1b558aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 | 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.
|