-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLecture 02 RPC and Threads.srt
5120 lines (4262 loc) · 150 KB
/
Lecture 02 RPC and Threads.srt
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
1
00:00:02,500 --> 00:00:08,990
今天我想谈谈go语言,这很有趣,特别有趣
today I'd like to talk about GO which
is interesting especially interesting
2
00:00:08,990 --> 00:00:12,709
在本课程中为我们服务,因为go是你在这个课程所有实验中
for us in this course because course GO
is the language at the labs you're all
3
00:00:12,709 --> 00:00:17,090
要使用的语言,所以我今天要特别关注一些
going to do the labs in and so I want to
focus today particularly on some of the
4
00:00:17,090 --> 00:00:22,940
在实验室中最有用的机械,尤其是对
machinery that sort of most useful in
the labs and in most particular to
5
00:00:22,940 --> 00:00:29,600
分布式编程首先,您应该知道为什么我们要使用go
distributed programming um first of all
you know it's worth asking why we use go
6
00:00:29,600 --> 00:00:33,890
实际上,在本课程中,我们可以使用许多其他系统中的任何一个
in this class in fact we could have used
any one of a number of other system
7
00:00:33,890 --> 00:00:38,210
样式语言诸如Java或C#甚至Python之类的大量语言
style languages plenty languages like
Java or C sharp or even Python that
8
00:00:38,210 --> 00:00:42,859
提供我们需要的那种设施,实际上我们曾经在这里使用C++
provide the kind of facilities we need
and indeed we used to use C++ in this
9
00:00:42,859 --> 00:00:49,010
类,效果很好,它的确会像许多其他语言一样
class and it worked out fine it'll go
indeed like many other languages
10
00:00:49,010 --> 00:00:51,649
提供了一堆特别方便的特性
provides a bunch of features which are
particularly convenient
11
00:00:51,649 --> 00:00:56,659
这是对线程以及线程之间的锁定和同步的良好支持
that's good support for threads and
locking and synchronization between
12
00:00:56,659 --> 00:01:01,789
我们经常使用的线程是一个方便的远程过程调用包
threads which we use a lot it is a
convenient remote procedure call package
13
00:01:01,789 --> 00:01:06,380
听起来并不多,但实际上却是一个重要的意义
which doesn't sound like much but it
actually turns out to be a significant
14
00:01:06,380 --> 00:01:11,210
例如C++等语言的约束,实际上很难
constraint from in languages like C++
for example it's actually a bit hard to
15
00:01:11,210 --> 00:01:14,930
找到一个方便易用的远程过程调用程序包,我们当然
find a convenient easy to use remote
procedure call package and of course we
16
00:01:14,930 --> 00:01:18,409
在本课程中始终使用它,或者在程序和不同的机器上交谈
use it all the time in this course or
programs and different machines to talk
17
00:01:18,409 --> 00:01:25,100
与C++彼此不同,go是类型安全和内存安全的,这很难
to each other unlike C++ go is type safe
and memory safe that is it's pretty hard
18
00:01:25,100 --> 00:01:29,180
编写一个程序,该程序由于错误而在某些随机部分上乱涂乱画
to write a program that due to a bug
scribbles over some random piece of
19
00:01:29,180 --> 00:01:34,789
内存,然后使程序执行神秘的操作,
memory and then causes the program to do
mysterious things and that just
20
00:01:34,789 --> 00:01:39,680
同样消除了一大堆错误,这是垃圾收集,这意味着您
eliminates a big class of bugs similarly
it's garbage collected which means you
21
00:01:39,680 --> 00:01:44,509
永远不会冒着两次重复使用同一内存或仍在使用的可用内存的危险
never in danger of priam the same memory
twice or free memory that's still in use
22
00:01:44,509 --> 00:01:48,859
或其他东西,当它们停止使用时,垃圾矢量只会释放它们
or something the garbage vector just
frees things when they stop being used
23
00:01:48,859 --> 00:01:54,829
一件事可能直到您玩完这件事才变得明显
and one thing it's maybe not obvious
until you played around with just this
24
00:01:54,829 --> 00:01:58,640
之前的那种编程,但是线程和垃圾的结合
kind of programming before but the
combination of threads and garbage
25
00:01:58,640 --> 00:02:03,200
收集尤其重要,这是非
collection is particularly important one
of the things that goes wrong in a non
26
00:02:03,200 --> 00:02:08,899
如果使用线程,则像C ++这样的垃圾收集语言总是
garbage collected language like C++ if
you use threads is that it's always a
27
00:02:08,899 --> 00:02:13,430
有点困惑,需要一堆簿记才能弄清楚何时
bit of a puzzle and requires a bunch of
bookkeeping to figure out when the last
28
00:02:13,430 --> 00:02:15,660
使用共享对象的线程有
thread
that's using a shared object has
29
00:02:15,660 --> 00:02:19,530
完成使用该对象,因为只有这样,您才能在结束时释放该对象
finished using that object because only
then can you free the object as you end
30
00:02:19,530 --> 00:02:22,620
编写大量的外套,就像手动程序员一样
up writing quite a bit of coat it's like
manually the programmer it's about a
31
00:02:22,620 --> 00:02:26,580
一堆代码,您可以手动进行引用计数或按顺序进行操作
bunch of code to manually you know do
reference counting or something in order
32
00:02:26,580 --> 00:02:30,030
弄清楚您知道最后一个线程何时停止使用对象,并且
to figure out you know when the last
thread stopped using an object and
33
00:02:30,030 --> 00:02:34,710
只是痛苦而已,如果您使用垃圾,该问题将完全消失
that's just a pain and that problem
completely goes away if you use garbage
34
00:02:34,710 --> 00:02:39,390
像我们还没有去的集合,最后语言很简单
collection like we haven't go
and finally the language is simple much
35
00:02:39,390 --> 00:02:44,640
比C ++更简单使用C ++的问题之一是
simpler than C++ one of the problems
with using C++ is that often if you made
36
00:02:44,640 --> 00:02:51,420
您知道的错误,甚至可能是拼写错误,您从中得到的错误消息
an error you know maybe even just a typo
the the error message you get back from
37
00:02:51,420 --> 00:02:56,160
编译器是如此复杂,以至于在C ++中通常不值得尝试
the compiler is so complicated that in
C++ it's usually not worth trying to
38
00:02:56,160 --> 00:02:59,520
弄清楚错误消息的含义,我发现它总是更快
figure out what the error message meant
and I find it's always just much quicker
39
00:02:59,520 --> 00:03:02,670
去查看行号并尝试猜测错误一定是什么
to go look at the line number and try to
guess what the error must have been
40
00:03:02,670 --> 00:03:04,800
因为语言太复杂了
because the language is far too
complicated
41
00:03:04,800 --> 00:03:09,710
而go是您知道可能没有很多人喜欢的特性
whereas go is you know probably doesn't
have a lot of people's favorite features
42
00:03:09,710 --> 00:03:14,940
但这是相对简单的语言,所以在这一点上
but it's relatively straightforward
language okay so at this point you're
43
00:03:14,940 --> 00:03:19,290
两者都在本教程上(如果您正在寻找),您知道该看什么
both on the tutorial if you're looking
for sort of you know what to look at
44
00:03:19,290 --> 00:03:23,190
接下来学习该语言的一个好地方就是标题为
next to learn about the language a good
place to look is the document titled
45
00:03:23,190 --> 00:03:30,390
您首先可以通过搜索网络找到的有效途径
effective go which you know you can find
by searching the web all right the first
46
00:03:30,390 --> 00:03:35,940
我想谈的是线程,我们之所以非常在意的原因
thing I want to talk about is threads
the reason why we care a lot about
47
00:03:35,940 --> 00:03:41,459
本课程中的线程是线程是我们要使用的主要工具
threads in this course is that threads
are the sort of main tool we're going to
48
00:03:41,459 --> 00:03:47,340
用于管理程序中的并发,并发是一个特殊的
be using to manage concurrency in
programs and concurrency is a particular
49
00:03:47,340 --> 00:03:52,440
对分布式编程很感兴趣,因为通常情况下
interest in distributed programming
because it's often the case that one
50
00:03:52,440 --> 00:03:55,890
程序实际上需要与一堆您知道的其他计算机对话
program actually needs to talk to a
bunch of other computers you know client
51
00:03:55,890 --> 00:04:00,300
可能会与许多服务器通信,或者一台服务器可能同时在
may talk to many servers or a server may
be serving requests at the same time on
52
00:04:00,300 --> 00:04:04,830
代表许多不同的客户,所以我们需要一种方式说哦,您知道我是我的
behalf of many different clients and so
we need a way to say oh you know I'm my
53
00:04:04,830 --> 00:04:07,410
程序确实有七种不同的情况发生,因为它正在与
program really has seven different
things going on because it's talking to
54
00:04:07,410 --> 00:04:12,450
七个不同的客户,我想要一个简单的方法来允许它执行这七个
seven different clients and I want a
simple way to allow it to do these seven
55
00:04:12,450 --> 00:04:16,858
您不需要太多复杂的编程就可以知道不同的事情
different things you know without too
much complex programming I mean sort of
56
00:04:16,858 --> 00:04:21,629
推力线就是答案,所以这些都是go文档
57
00:04:21,630 --> 00:04:26,789
调用go例程,我称之为线程,它们是go例程,实际上是一样的
calls go routines which I call threads
they're go routines are really this same
58
00:04:26,789 --> 00:04:32,560
就像其他人所说的Red一样,线程的思考方式是
as what everybody else calls
Red's so the way to think of threads is
59
00:04:32,560 --> 00:04:43,120
你有一个程序和一个地址空间的程序,我要画一个
that you have a program of one program
and one address space I'm gonna draw a
60
00:04:43,120 --> 00:04:48,250
框来表示一个地址空间,并在该地址空间内
box to sort of denote an address space
and within that address space in a
61
00:04:48,250 --> 00:04:54,550
没有线程的串行程序,只有一个执行线程正在执行
serial program without threads you just
have one thread of execution executing
62
00:04:54,550 --> 00:05:00,100
该地址空间中的代码一个程序计数器一组寄存器一组
code in that address space one program
counter one set of registers one stack
63
00:05:00,100 --> 00:05:04,180
在某种程度上描述了线程中执行的当前状态
that are sort of describing the current
state of the execution in a threaded
64
00:05:04,180 --> 00:05:09,190
像go程序这样的程序,您可能有多个线程,并且您知道我很生
program like a go program you could have
multiple threads and you know I got raw
65
00:05:09,190 --> 00:05:13,480
它是多条弯曲的线,并且当每条线代表一个
it as multiple squiggly lines and when
each line represents really is a
66
00:05:13,480 --> 00:05:17,440
分开,尤其是如果线程在同时执行
separate if the especially if the
threads are executing at the same time
67
00:05:17,440 --> 00:05:21,550
但是一个单独的程序计数器是一个单独的寄存器集和一个单独的寄存器
but a separate program counter a
separate set of registers and a separate
68
00:05:21,550 --> 00:05:26,230
每个线程的堆栈,以便它们可以拥有自己的线程
stack for each of the threads so that
they can have a sort of their own thread
69
00:05:26,230 --> 00:05:31,810
控制权并在程序的不同部分执行每个线程,以及
of control and be executing each thread
in a different part of the program and
70
00:05:31,810 --> 00:05:35,530
因此,这里隐藏的是,对于每个堆栈,现在都有一个糖浆线程,
so hidden here is that for every stack
now there's a syrupy thread there's a
71
00:05:35,530 --> 00:05:44,530
它在堆栈上执行的堆栈实际上位于一个地址空间中
stack that it's executing on the stacks
are actually in in the one address space
72
00:05:44,530 --> 00:05:47,860
程序,所以即使每个堆栈每个线程都有自己的堆栈
of the program so even though each stack
each thread has its own stack
73
00:05:47,860 --> 00:05:52,240
从技术上讲,它们都位于相同的地址空间和不同的线程中
technically the they're all in the same
address space and different threads
74
00:05:52,240 --> 00:05:55,960
如果他们知道正确的地址,则可以互相引用堆栈,尽管您
could refer to each other stacks if they
knew the right addresses although you
75
00:05:55,960 --> 00:06:01,510
通常不这样做,然后在您知道主程序时
typically don't do that and then go when
you even the main program you know when
76
00:06:01,510 --> 00:06:05,110
您首先启动程序,然后在main中运行,这只是一个步骤
you first start up the program and it
runs in main that's also it's just a go
77
00:06:05,110 --> 00:06:14,440
可以做所有常规的事情,青少年可以做的很好,就像我一样
routine and can do all the things that
go teens can do all right so as I
78
00:06:14,440 --> 00:06:21,730
提到的主要原因之一是允许程序的不同部分
mentioned one of the big reasons is to
allow different parts of the program to
79
00:06:21,730 --> 00:06:27,550
某种意义上说是在另一项活动中,所以我通常指的是
sort of be in its own point in in a
different activity so I usually refer to
80
00:06:27,550 --> 00:06:36,580
出于历史原因以及我称之为IO的原因,将其作为IO并发
that as IO concurrency for historical
reasons and the reason I call it IO
81
00:06:36,580 --> 00:06:39,580
并发是,在过去,这是第一次出现
concurrency is that in the old days
where this first came up is that oh you
82
00:06:39,580 --> 00:06:41,470
可能有一个正在等待读取的线程
might have one thread that's waiting to
read
83
00:06:41,470 --> 00:06:44,410
从磁盘上,而在等待从磁盘访问时,您希望
from the disk and while it's waiting to
reach from the disk you'd like to have a
84
00:06:44,410 --> 00:06:49,000
第二个线程可能可以计算或读取磁盘中的其他位置或发送
second thread that maybe can compute or
read somewhere else in the disk or send
85
00:06:49,000 --> 00:06:54,490
网络中的一条消息,等待回复,所以我打开货币之一
a message in the network and wait for
reply so and so I open currencies one of
86
00:06:54,490 --> 00:07:00,190
您为我们穿线的东西通常意味着我可以打开货币
the things that threads by you for us it
would usually mean I can I open currency
87
00:07:00,190 --> 00:07:04,090
我们通常是说我可以拥有一个已启动或删除程序的程序
we usually mean I can have one program
that has launched or removed procedure
88
00:07:04,090 --> 00:07:08,140
将请求呼叫到网络上的其他服务器,并且正在等待许多
calls requests to different servers on
the network and is waiting for many
89
00:07:08,140 --> 00:07:13,180
在同一时间回复,这将对我们产生影响,您知道
replies at the same time that's how
it'll come up for us and you know the
90
00:07:13,180 --> 00:07:17,110
用线程执行此操作的方式是,您将为创建一个线程
way you would do that with threads is
that you would create one thread for
91
00:07:17,110 --> 00:07:21,190
您要启动该线程的每个远程过程调用都会
each of the remote procedure calls that
you wanted to launch that thread would
92
00:07:21,190 --> 00:07:26,380
具有您知道发送远程过程调用请求消息和排序的代码
have code that you know sent the remote
procedure call request message and sort
93
00:07:26,380 --> 00:07:29,320
在线程中的这一点上等待了,然后终于在回复到来时
of waited at this point in the thread
and then finally when the reply came
94
00:07:29,320 --> 00:07:33,220
返回线程将继续执行,并且使用线程使我们能够
back the thread would continue executing
and using threads allows us to have
95
00:07:33,220 --> 00:07:36,250
多个线程将所有启动请求同时发送到网络
multiple threads that all launch
requests into the network at the same
96
00:07:36,250 --> 00:07:40,690
他们都在等待的时间,或者他们不必在同一时间这样做
time they all wait or they don't have to
do it at the same time they can you know
97
00:07:40,690 --> 00:07:43,090
只要他们愿意就执行它的不同部分
execute the different parts of this
whenever they feel like it
98
00:07:43,090 --> 00:07:49,720
所以这是I / O并发性,它是不同进度的重叠
so that's i/o concurrency sort of
overlapping of the progress of different
99
00:07:49,720 --> 00:07:57,060
活动并允许一个活动正在等待其他活动可以继续进行
activities and allowing one activity is
waiting other activities can proceed
100
00:07:57,060 --> 00:08:02,740
使用线程的另一个重要原因是多核并行性
another big reason to use threads is
multi-core parallelism which I'll just
101
00:08:02,740 --> 00:08:10,270
称为并行性,这是我们要尝试实现的目标
call parallelism and here the thing
where we'd be trying to achieve with
102
00:08:10,270 --> 00:08:13,540
线程是,如果您拥有一台多核计算机,例如,我确定你们所有人都在
threads is if you have a multi-core
machine like I'm sure all of you do in
103
00:08:13,540 --> 00:08:17,470
您的笔记本电脑,如果您需要大量的计算工作,
your laptops if you have a sort of
compute heavy job that needs a lot of
104
00:08:17,470 --> 00:08:21,060
如果您有一个可以使用的程序,CPU周期就不好了
CPU cycles wouldn't it be nice if you
could have one program that could use
105
00:08:21,060 --> 00:08:25,630
计算机的所有内核上的CPU周期,实际上如果您编写了一个
CPU cycles on all of the cores of the
machine and indeed if you write a
106
00:08:25,630 --> 00:08:30,040
多线程go,如果您启动多个go例程并执行go并且它们执行某些操作
multi-threaded go if you launch multiple
go routines and go and they do something
107
00:08:30,040 --> 00:08:33,940
像坐在那里循环那样的计算机密集型,您知道pi的计算位数
computer intensive like sit there in a
loop and you know compute digits of pi
108
00:08:33,940 --> 00:08:38,520
或达到物理计算机内核数限制的值
or something then up to the limit of the
number of cores in the physical machine
109
00:08:38,520 --> 00:08:43,690
您的线程将真正并行运行,如果启动,您将知道两个线程
your threads will run truly in parallel
and if you launch you know two threads
110
00:08:43,690 --> 00:08:48,370
而不是一个,您将可以使用两倍的CPU
instead of one you'll get twice as many
you'll be able to use twice as many CPU
111
00:08:48,370 --> 00:08:53,170
每秒循环数,所以这对于某些人来说非常重要,这并不大
cycles per second so this is very
important to some people it's not a big
112
00:08:53,170 --> 00:08:57,440
处理这门课程,因为我们很少会思考
deal on this course
be it's rare that we'll sort of think
113
00:08:57,440 --> 00:09:01,640
特别是关于现实世界中的这种并行性
specifically about this kind of
parallelism in the real world though of
114
00:09:01,640 --> 00:09:06,890
构建诸如服务器之类的东西来构成分布式系统的一部分
building things like servers to form
parts of the distributed systems it can
115
00:09:06,890 --> 00:09:11,780
有时使服务器能够运行非常重要
sometimes be extremely important to be
able to have the server be able to run
116
00:09:11,780 --> 00:09:15,140
线程并利用许多内核的CPU功能,只是因为来自
threads and harness the CPU power of a
lot of cores just because the load from
117
00:09:15,140 --> 00:09:22,850
客户通常可以很高,所以并行是第二个原因
clients can often be pretty high okay so
parallelism is a second reason why
118
00:09:22,850 --> 00:09:27,170
线程对分布式系统非常感兴趣,第三个原因
threads are quite a bit interested in
distributed systems and a third reason
119
00:09:27,170 --> 00:09:32,780
这可能不那么重要,因为有些时候
which is maybe a little bit less
important is there's some there's times
120
00:09:32,780 --> 00:09:38,630
当您真的只想能够在后台执行某项操作或
when you really just want to be able to
do something in the background or you
121
00:09:38,630 --> 00:09:42,770
知道您只需要定期做一些事情,而您不想
know there's just something you need to
do periodically and you don't want to
122
00:09:42,770 --> 00:09:47,420
必须在程序的主要部分进行某种类型的插入检查
have to sort of in the main part of your
program sort of insert checks to say
123
00:09:47,420 --> 00:09:51,080
我应该做每秒钟左右发生的事情吗?
well should I be doing this things that
should happen every second or so you
124
00:09:51,080 --> 00:09:54,380
就像能够点火,每一秒都能做的一样
just like to be able to fire something
up that every second does whatever the
125
00:09:54,380 --> 00:10:00,500
周期性的事情是有一些方便的原因和一个例子
periodic thing is so there's some
convenience reasons and an example which
126
00:10:00,500 --> 00:10:05,210
经常会出现一些您知道主服务器的情况
will come up for you is it's often the
case that some you know a master server
127
00:10:05,210 --> 00:10:09,350
可能想定期检查其工人是否还活着,因为
may want to check periodically whether
its workers are still alive because one
128
00:10:09,350 --> 00:10:12,170
他们中的人死了,你知道你想在另一台机器上启动该工作
of them is died you know you want to
launch that work on another machine like
129
00:10:12,170 --> 00:10:17,420
MapReduce可能会这样做,并且是一种安排排序的方法
MapReduce might do that and one way to
arrange sort of oh do this check every
130
00:10:17,420 --> 00:10:21,680
您知道的每一分钟都会向工作人员发送消息,您还活着吗?
second every minute you know send a
message to the worker are you alive is
131
00:10:21,680 --> 00:10:25,700
触发执行程序,该执行程序只是循环睡眠一秒钟,然后
to fire off a go routine that just sits
in a loop that sleeps for a second and
132
00:10:25,700 --> 00:10:28,700
然后做周期性的事情,然后再睡一秒钟,所以在
then does the periodic thing and then
sleeps for a second again and so in the
133
00:10:28,700 --> 00:10:36,560
实验室中,您最终会触发这类线程,是的
labs you'll end up firing off these kind
of threads quite a bit yes is the
134
00:10:36,560 --> 00:10:44,840
开销值得,是的,对于这些东西,开销真的很小
overhead worth it yes the overhead is
really pretty small for this stuff I
135
00:10:44,840 --> 00:10:50,150
意味着您知道这取决于您创建多少个他坐在其中的一百万个线程
mean you know it depends on how many you
create a million threads that he sit in
136
00:10:50,150 --> 00:10:53,960
等待毫秒的循环,然后发送网络消息
a loop waiting for a millisecond and
then send a network message that's
137
00:10:53,960 --> 00:10:59,200
可能会给您的计算机带来巨大的负担,但是如果您创建了它,就会知道十个线程
probably a huge load on your machine but
if you create you know ten threads that
138
00:10:59,200 --> 00:11:04,850
睡一秒钟然后做一点工作,这可能不是什么大问题
sleep for a second and do a little bit
of work it's probably not a big deal at
139
00:11:04,850 --> 00:11:10,019
全部,这是我向您保证程序员的时间
all and it's
I guarantee you the programmer time you
140
00:11:10,019 --> 00:11:16,199
说,不必在一起糊涂,他们是不同的不同
say by not having to sort of mush
together they're different different
141
00:11:16,199 --> 00:11:21,449
将活动集中到一行代码中,这值得少量的CPU成本
activities into one line of code it's
it's worth the small amount of CPU cost
142
00:11:21,449 --> 00:11:27,600
几乎总是你知道如果不幸的话,你会发现
almost always still you know you will if
you're unlucky you'll discover in the
143
00:11:27,600 --> 00:11:32,519
实验室,您的某个循环睡眠时间不足或被解雇了
labs that some loop of yours is not
sleeping long enough or are you fired
144
00:11:32,519 --> 00:11:37,589
掉了一堆这些,从没有让它们退出,例如
off a bunch of these and never made them
exit for example and they just
145
00:11:37,589 --> 00:11:43,680
积累,所以你可以把它推得太远,所以这些就是
accumulate so you can push it too far
okay so these are the reasons that the
146
00:11:43,680 --> 00:11:47,730
人们非常喜欢线程并且将在此使用线程的主要原因
main reasons that people like threads a
lot and that will use threads in this
147
00:11:47,730 --> 00:12:01,860
一般通过异步程序对有关线程的其他任何问题进行分类
class any other questions about threads
in general by asynchronous program you
148
00:12:01,860 --> 00:12:06,779
意味着像一个控制线程,可以保持许多不同状态
mean like a single thread of control
that keeps state about many different
149
00:12:06,779 --> 00:12:12,209
活动是的,所以这是一个很好的问题,实际上你知道吗
activities yeah so this is a good
question actually there is you know what
150
00:12:12,209 --> 00:12:15,089
如果我们没有线程,或者由于某种原因我们不想
would happen if we didn't have threads
or we'd for some reason we didn't want
151
00:12:15,089 --> 00:12:18,899
使用威胁,例如我们将如何编写一个您可能知道的程序
to use threats like how would we be able
to write a program that could you know a
152
00:12:18,899 --> 00:12:23,339
可以同时与许多不同客户端或客户端对话的服务器
server that could talk to many different
clients at the same time or a client
153
00:12:23,339 --> 00:12:26,100
可以与任何服务器交谈的权利,可以使用哪些工具,以及
that could talk to him any servers right
what what tools could be used and it
154
00:12:26,100 --> 00:12:36,420
事实证明,这是另一种主要风格的另一种形式
turns out there is sort of another line
of another kind of another major style
155
00:12:36,420 --> 00:12:38,339
您如何构造这些程序,
of how do you structure these programs
called
156
00:12:38,339 --> 00:12:42,529
您调用异步程序,我可能称其为通风口驱动程序
you call the asynchronous program I
might call it a vent driven programming
157
00:12:42,529 --> 00:12:50,850
因此,或者您可以使用通风孔防止编程,并且一般
so sort of or you could use a vent
prevent programming and the the general
158
00:12:50,850 --> 00:12:54,420
事件驱动程序的结构通常是只有一个线程,并且
structure of an event-driven program is
usually that it has a single thread and
159
00:12:54,420 --> 00:13:01,380
一个循环,然后该循环坐在那里,等待任何输入或
a single loop and what that loop does is
sits there and waits for any input or
160
00:13:01,380 --> 00:13:05,699
任何可能触发处理的事件,因此一个事件可能是
sort of any event that might trigger
processing so an event might be the
161
00:13:05,699 --> 00:13:10,889
来自客户端的请求到达或计时器关闭,或者您正在构建
arrival of a request from a client or a
timer going off or if you're building a
162
00:13:10,889 --> 00:13:14,189
Window System保护了我驾驶的笔记本电脑上的许多Windows系统
Window System protect many Windows
systems on your laptops I've driven
163
00:13:14,189 --> 00:13:17,639
编写了一种事件驱动型的样式,他们在等待什么,例如按键
written an event-driven style where what
they're waiting for is like key clicks
164
00:13:17,639 --> 00:13:20,259
或鼠标移动之类的东西,因此您可能只有一个
or Mouse move
or something so you might have a single
165
00:13:20,259 --> 00:13:23,350
在事件驱动的程序中,只有一个控制威胁就无法实现
in an event-driven program it of a
single threat of control sits an aloof
166
00:13:23,350 --> 00:13:27,160
等待输入,每当它收到输入(例如数据包)时,它就会弄清楚哦
waits for input and whenever it gets an
input like a packet it figures out oh
167
00:13:27,160 --> 00:13:31,540
您知道此数据包来自哪个客户端,然后它将有一个表
you know which client did this packet
come from and then it'll have a table of