-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1618 lines (644 loc) · 86 KB
/
index.html
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
<!DOCTYPE html>
<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">
<link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
version: '6.3.0',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
fastclick: false,
lazyload: false,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="xclidongbo's Blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="xclidongbo's Blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="xclidongbo's Blog">
<link rel="canonical" href="http://yoursite.com/"/>
<script type="text/javascript" id="page.configurations">
CONFIG.page = {
sidebar: "",
};
</script>
<title>xclidongbo's Blog</title>
<noscript>
<style type="text/css">
.use-motion .motion-element,
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-title { opacity: initial; }
.use-motion .logo,
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion {
.logo-line-before i { left: initial; }
.logo-line-after i { right: initial; }
}
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">xclidongbo's Blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
</div>
<div class="site-nav-toggle">
<button aria-label="Toggle navigation bar">
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home menu-item-active">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />Home</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />Tags</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-categories"></i> <br />Categories</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />Archives</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />About</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/27/iOS-多线程/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="xclidongbo's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/27/iOS-多线程/" itemprop="url">
iOS-多线程
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2018-05-27 11:41:30 / Modified: 17:19:31" itemprop="dateCreated datePublished" datetime="2018-05-27T11:41:30+08:00">2018-05-27</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">iOS</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p>多线程的分类</p>
<ul>
<li>pthread: 一套通用的多线程API, 跨平台,可移植,使用难度大.适用于Unix/Linux/Windows等系统</li>
<li>NSThread: 面向对象,需要手动管理线程的生命周期.</li>
<li>GCD: 替代NSThread等线程技术, 充分运用多核.</li>
<li>NSOperation: 基于GCD,比GCD多一些简单实用的功能, 更加的面向对象.</li>
</ul>
<h3 id="GCD"><a href="#GCD" class="headerlink" title="GCD"></a>GCD</h3><p>GCD特点:</p>
<pre><code>- GCD会自动利用更多的CPU内核.
- GCD自动管理线程的生命周期.
- 程序员只要告诉GCD想要如何执行任务
</code></pre><p>GCD概念:</p>
<pre><code>- 任务(block): 任务就是将要在线程中执行的代码.
- 同步(sync): 一个接一个执行, 后一个等待前一个执行完毕,再执行.
- 异步(async): 开启多个新线程, 任务同一时间可以一起执行.
- 队列: 装在线程任务的队列结构.在GCD中有两种,串行和并行队列.
- 并发队列: 线程可以同时一起执行.实际上是CPU在多条线程之间快速切换.
- 串行队列: 线程只能依次有序执行.
</code></pre><blockquote>
<p>注意: <em>异步</em>虽然有开启新线程的能力,但并不一定开启新线程.这和任务所指定的队列有关.</p>
</blockquote>
<h4 id="任务和队列"><a href="#任务和队列" class="headerlink" title="任务和队列"></a>任务和队列</h4><h5 id="1-异步串行"><a href="#1-异步串行" class="headerlink" title="1. 异步串行"></a>1. 异步串行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">/* </span><br><span class="line">异步一定会开辟新线程吗? 不一定, main</span><br><span class="line">同步一定会开辟新线程吗? 一定不</span><br><span class="line">开辟新线程512k, 主线程1M.</span><br><span class="line">*/</span><br><span class="line">- (void)asyncSerial{</span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>开启新线程, 因为是同步,所以按照顺序执行任务.执行结果:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">begin</span><br><span class="line">end</span><br><span class="line">任务1</span><br><span class="line">任务2</span><br><span class="line">任务3</span><br></pre></td></tr></table></figure>
<h5 id="2-同步串行"><a href="#2-同步串行" class="headerlink" title="2. 同步串行"></a>2. 同步串行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">- (void)syncSerial{</span><br><span class="line"> </span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>在主线程顺序执行,执行结果: </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">begin</span><br><span class="line">任务1</span><br><span class="line">任务2</span><br><span class="line">任务3</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
<h5 id="3-同步并行"><a href="#3-同步并行" class="headerlink" title="3. 同步并行"></a>3. 同步并行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">- (void)syncConcurrent{</span><br><span class="line"> </span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>因为同步,所以会按照顺序执行,并不会开启新线程.所以执行结果和同步串行的执行结果一致.</p>
<h5 id="4-异步并行"><a href="#4-异步并行" class="headerlink" title="4. 异步并行"></a>4. 异步并行</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">- (void)asyncConcurrent{</span><br><span class="line"> </span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>任务交替执行,开启多线程(任务先后顺序不定),结果:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">begin</span><br><span class="line">end</span><br><span class="line">任务x</span><br><span class="line">任务x</span><br><span class="line">任务x</span><br></pre></td></tr></table></figure>
<h5 id="5-主队列同步-死锁"><a href="#5-主队列同步-死锁" class="headerlink" title="5. 主队列同步(死锁)"></a>5. 主队列同步(死锁)</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">- (void)syncMain{</span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_get_main_queue();</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_sync(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>死锁造成的原因:</p>
<pre><code>1. 如果在主线程中运用主队列同步,也就是把任务加入到主线程的队列中. 主线程的特性: 主线程上的任务执行完毕,才能执行主队列的任务.
2. 同步对于任务是立即执行的, 那么当把任务1放进主队列的时候,它就立马执行.
3. 可主线程目前正在处理syncMain方法, 任务需要等待syncMain方法执行完毕后才能执行.
4. syncMain方法 执行到第一个任务的时候,又要等第一个任务执行完毕才能执行任务2,任务3.
5. 这样syncMain方法和任务1 就开始互相等待, 造成死锁.
</code></pre><h5 id="6-主队列异步"><a href="#6-主队列异步" class="headerlink" title="6. 主队列异步"></a>6. 主队列异步</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (void)asyncMain{</span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_queue_t queue = dispatch_get_main_queue();</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务1");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务2");</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> NSLog(@"任务3");</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> NSLog(@"end");</span><br></pre></td></tr></table></figure>
<ul>
<li>所有任务都是在当前线程(主线程)中执行,并没有开启新线程(虽然异步有开启线程的能力,但因为主队列,所有任务都会在主线程中执行)</li>
<li>所有任务在打印begin和end之后才执行.</li>
<li>任务是按照顺序执行, 因为主队列是串行队列.</li>
</ul>
<p>所以执行顺序为:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">begin</span><br><span class="line">end</span><br><span class="line">任务1</span><br><span class="line">任务2</span><br><span class="line">任务3</span><br></pre></td></tr></table></figure>
<h4 id="API"><a href="#API" class="headerlink" title="API"></a>API</h4><h5 id="GCD线程通信"><a href="#GCD线程通信" class="headerlink" title="GCD线程通信"></a>GCD线程通信</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">- (void)communication {</span><br><span class="line"> //获取全局并发队列</span><br><span class="line"> dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);</span><br><span class="line"> //获取主队列</span><br><span class="line"> dispatch_queue_t mainQueue = dispatch_get_main_queue();</span><br><span class="line"></span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> //添加耗时操作, 异步进行.</span><br><span class="line"> for( int i = 0, i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> dispatch_async(mainQueue, ^{</span><br><span class="line"> //刷新UI操作.</span><br><span class="line"> })</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="GCD栅栏方法-dispatch-barrier-async"><a href="#GCD栅栏方法-dispatch-barrier-async" class="headerlink" title="GCD栅栏方法: dispatch_barrier_async"></a>GCD栅栏方法: dispatch_barrier_async</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (void)barrier {</span><br><span class="line"> dispatch_queue_t queue = dispatch_queue_create("net.test", DISPATCH_QUEUE_CONCURRENT);</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> for(int i = 0; i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> NSLog(@"1 ---- %@", [NSThread currentThread]);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> for(int i = 0; i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> NSLog(@"2 ---- %@", [NSThread currentThread]);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> dispatch_barrier_async(queue, ^{</span><br><span class="line"> for(int i = 0; i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> NSLog(@"barrier ---- %@", [NSThread currentThread]);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> for(int i = 0; i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> NSLog(@"3 ---- %@", [NSThread currentThread]);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> for(int i = 0; i < 2; i++) {</span><br><span class="line"> [NSThread sleepForTimeInterval:2];</span><br><span class="line"> NSLog(@"4 ---- %@", [NSThread currentThread]);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>输出结果:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">barrier</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td></tr></table></figure>
<h5 id="GCD延时方法-dispatch-after"><a href="#GCD延时方法-dispatch-after" class="headerlink" title="GCD延时方法: dispatch_after"></a>GCD延时方法: dispatch_after</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">- (void)after {</span><br><span class="line"> dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{</span><br><span class="line"> //延时操作</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="GCD-只执行一次-dispach-once"><a href="#GCD-只执行一次-dispach-once" class="headerlink" title="GCD 只执行一次: dispach_once"></a>GCD 只执行一次: dispach_once</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)once {</span><br><span class="line"> static dispatch_once_t onceToken;</span><br><span class="line"> dispatch_once(&onceToken, ^{</span><br><span class="line"></span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="GCD-快速迭代操作-dispatch-apply"><a href="#GCD-快速迭代操作-dispatch-apply" class="headerlink" title="GCD 快速迭代操作: dispatch_apply"></a>GCD 快速迭代操作: dispatch_apply</h5><ul>
<li>通常我们使用for循环遍历, 但是GCD给我们提供了快速迭代的函数<code>dispatch_apply</code>.<code>dispatch_apply</code>按照指定的次数讲指定的任务追加到指定队列中, 并等待全部队列执行结束.</li>
</ul>
<blockquote>
<p>注意: 在串行队列中使用<code>dispatch_apply</code>, 只会一个个的执行.没有意义. 只有在并行队列中进行异步操作,才会有优势. 无论并行串行, <code>dispatch_apply</code>都会等全部任务执行完毕,这点类似同步操作.就像队列组中<code>dispatch_group_wait</code>方法.</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">- (void)apply {</span><br><span class="line"> dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);</span><br><span class="line"> NSLog(@"begin");</span><br><span class="line"> dispatch_apple(6, queue, ^(size_t index){</span><br><span class="line"> //单个逻辑</span><br><span class="line"> });</span><br><span class="line"> NSLog(@"end");</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h5 id="GCD-队列组-dispatch-group"><a href="#GCD-队列组-dispatch-group" class="headerlink" title="GCD 队列组: dispatch_group"></a>GCD 队列组: dispatch_group</h5><p>有时我们的需求: 异步执行两个耗时任务, 然后当两个耗时任务都执行完毕再回到主线程执行任务, 这时候我们就用到GCD的队列组.</p>
<ul>
<li>调用队列组的<code>dispatch_group_async</code>,先把任务放在队列中,再把队列放进队列组中. 或者使用队列组的<code>dispatch_group_enter</code>,<code>dispatch_group_leave</code>组合来实现<code>dispatch_group_async</code>;</li>
<li>调用队列组的<code>dispatch_group_notify</code>回到指定线程执行任务. 或者使用<code>dispatch_group_wait</code>回到当前线程继续向下执行.</li>
</ul>
<h6 id="1-dispatch-group-notify"><a href="#1-dispatch-group-notify" class="headerlink" title="1. dispatch_group_notify"></a>1. dispatch_group_notify</h6><ul>
<li>监听group中任务的完成状态, 当所有任务执行完毕,追加任务到group中,并执行任务.</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- (void)groupNotify {</span><br><span class="line"></span><br><span class="line"> dispatch_group_t group = dispatch_group_create();</span><br><span class="line"></span><br><span class="line"> dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{</span><br><span class="line"> //追加任务1</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{</span><br><span class="line"> //追加任务2</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> </span><br><span class="line"> dispatch_group_notify(group, dispatch_get_main_queue(), ^{</span><br><span class="line"> //追加任务3</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h6 id="2-dispach-group-wait"><a href="#2-dispach-group-wait" class="headerlink" title="2. dispach_group_wait"></a>2. dispach_group_wait</h6><ul>
<li>暂停当前线程(阻塞当前线程), 等待指定的group中的任务完成后,才往下继续执行.</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">dispatch_group_t group = dispatch_group_create();</span><br><span class="line"></span><br><span class="line">dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{</span><br><span class="line"> //追加任务1</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line">dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{</span><br><span class="line"> //追加任务2</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line">//等待上面的任务完成后,再往下继续执行.</span><br><span class="line">dispatch_group_wait(group, DISPATCH_TIME_FOREVER);</span><br></pre></td></tr></table></figure>
<h6 id="3-dispatch-group-enter-dispatch-group-leave"><a href="#3-dispatch-group-enter-dispatch-group-leave" class="headerlink" title="3. dispatch_group_enter, dispatch_group_leave"></a>3. dispatch_group_enter, dispatch_group_leave</h6><ul>
<li><code>dispatch_group_enter</code>标志着一个任务追加到group,执行一次,相当于group中未执行的任务数+1.</li>
<li><code>dispatch_group_leave</code>标志着一个任务离开了group,执行一次,相当于group中未执行完成任务数-1.</li>
<li>当group中未执行完成任务数为0的时候,才会使<code>dispatch_group_wait</code>解除阻塞,以及追加到<code>dispatch_group_notify</code>中的任务.</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">- (void)groupEnterAndLeave {</span><br><span class="line"> dispatch_group_t group = dispatch_group_create();</span><br><span class="line"> dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);</span><br><span class="line"></span><br><span class="line"> dispatch_group_enter(group);</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> //追加耗时任务</span><br><span class="line"></span><br><span class="line"> dispatch_group_leave(group);</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> dispatch_group_enter(group);</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> //追加耗时任务</span><br><span class="line"></span><br><span class="line"> dispatch_group_leave(group);</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> dispatch_group_notify(group, dispatch_get_main_queue(), ^{</span><br><span class="line"> //等待异步操作执行完毕后,回到主线程</span><br><span class="line"> for (int i = 0, i< 2; i++) {</span><br><span class="line"> //耗时操作</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h6 id="6-GCD信号量-dispatch-semaphore"><a href="#6-GCD信号量-dispatch-semaphore" class="headerlink" title="6. GCD信号量: dispatch_semaphore"></a>6. GCD信号量: dispatch_semaphore</h6><p>GCD中的信号量,是持有计数的信号.</p>
<p><strong>Dispatch Semaphore</strong> 提供三个函数:</p>
<ul>
<li><code>dispatch_semaphore_create</code>: 创建一个semaphore并初始化信号总量.</li>
<li><code>dispatch_semaphore_sigal</code>: 发送一个信号,让信号总量+1.</li>
<li><code>dispatch_semaphore_wait</code>: 可使信号量减一,信号总量未0就会一直等待(阻塞所在线程),否则可以正常执行.</li>
</ul>
<p>在实际开发中主要用于:</p>
<ul>
<li>保持线程同步, 讲异步执行任务转换为同步执行任务.</li>
<li>保证线程安全,为线程加锁.</li>
</ul>
<p>####### 线程异步改为同步<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// 线程同步</span><br><span class="line">- (void)semaphoreSync {</span><br><span class="line"></span><br><span class="line"> dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);</span><br><span class="line"> dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);</span><br><span class="line"></span><br><span class="line"> __block int number = 0;</span><br><span class="line"> dispatch_async(queue, ^{</span><br><span class="line"> [NSThread sleepForTimeInterval: 2];</span><br><span class="line"> number = 100;</span><br><span class="line"> dispatch_semaphore_signal(semaphore);</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);</span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p>####### Dispatch Semaphore 加锁 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">- (void)semaphoreLock {</span><br><span class="line"></span><br><span class="line"> dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);</span><br><span class="line"> dispatch_async(dispatch_get_global_queue(0,0), ^{</span><br><span class="line"> dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);</span><br><span class="line"> NSLog(@"1");</span><br><span class="line"> sleep(2);</span><br><span class="line"> NSLog(@"1 ok");</span><br><span class="line"> dispatch_semaphore_signal(semaphore);</span><br><span class="line"> });</span><br><span class="line"> dispatch_async(dispatch_get_global_queue(0,0), ^{</span><br><span class="line"> dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);</span><br><span class="line"> NSLog(@"2");</span><br><span class="line"> sleep(2);</span><br><span class="line"> NSLog(@"2 ok");</span><br><span class="line"> dispatch_semaphore_signal(semaphore);</span><br><span class="line"> });</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="线程安全"><a href="#线程安全" class="headerlink" title="线程安全"></a>线程安全</h4><p>多个线程共同访问一块资源时候,容易引发数据错乱和数据安全.就好比几个人同时修改一个表格,造成数据混乱.</p>
<p>解决多线程安全问题的方法:</p>
<ul>
<li>互斥锁(锁对象)</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">@synchronized(锁对象){</span><br><span class="line"> //需要加锁的代码</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>例如:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">- (void)syncchronized {</span><br><span class="line"> dispatch_async(dispatch_get_global_queue(0,0), ^{</span><br><span class="line"> @synchronized(self){</span><br><span class="line"> //耗时操作</span><br><span class="line"> };</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>判断的时候锁对象要存在, 如果代码中只有一个地方需要加锁, 大多数实用self来锁对象. 这样可以避免单独再创建一个锁对象.加了互斥锁的代码,当新线程访问时,如果发现其它线程正在执行锁定的代码,新线程就会进入休眠.</p>
<ul>
<li>自旋锁</li>
</ul>
<p>当新线程访问代码时,如果发现其它线程正在执行锁定的代码, 新线程会用死循环的方式,一直等待锁定的代码执行完成.不停的尝试执行代码,比较消耗性能.</p>
<p>属性修饰atomic本身有一把自旋锁.</p>
<blockquote>
<p>nonatomic 非原子属性, 同一时间可以由很多线程读写.<br>atomic 原子属性(线程安全),同一事件只有一个线程能够写入(同一事件多个线程可以读取)<br>atomic: 线程相对安全, 但需要消耗大量资源.<br>nonatomic: 非线程安全,不过效率更高.</p>
</blockquote>
<blockquote>
<p>自旋锁的例子,可以查看GCD的dispatch_semaphore.</p>
</blockquote>
<blockquote>
<p>注意: 为什么不用atomic?<br>因为用atomic的时候, 并不能保证线程是安全的.</p>
</blockquote>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/26/HTTP相关/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="xclidongbo's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/26/HTTP相关/" itemprop="url">
HTTP相关
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2018-05-26 21:08:59" itemprop="dateCreated datePublished" datetime="2018-05-26T21:08:59+08:00">2018-05-26</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2018-05-27 17:26:23" itemprop="dateModified" datetime="2018-05-27T17:26:23+08:00">2018-05-27</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/网络/" itemprop="url" rel="index"><span itemprop="name">网络</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="HTTP简介"><a href="#HTTP简介" class="headerlink" title="HTTP简介"></a>HTTP简介</h3>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/26/Linux-ip命令/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="xclidongbo's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/26/Linux-ip命令/" itemprop="url">
Linux-ip命令
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2018-05-26 12:27:30 / Modified: 19:06:45" itemprop="dateCreated datePublished" datetime="2018-05-26T12:27:30+08:00">2018-05-26</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Linux常用命令/" itemprop="url" rel="index"><span itemprop="name">Linux常用命令</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h3><p>目前系统是Debian 9.3系统.不知从合适<code>ifconfig</code>命令已经不在linux上默认集成.目前查询和设置ip的命令由<code>ifconfig</code>改为<code>ip</code>.查询资料得知<code>ifconfig</code>是<code>net-tools</code>中已被废弃的命令.<code>ip</code>命令是<code>iproute2</code>套件中的命令. <a href="https://linux.cn/article-3144-1.html" target="_blank" rel="noopener">参考此处</a></p>
<p><img src="https://dn-linuxcn.qbox.me/data/attachment/album/201406/04/003404uy9l1t5zayzllylm.png" alt="net-tools与iproute2对比"></p>
<h3 id="安装命令"><a href="#安装命令" class="headerlink" title="安装命令"></a>安装命令</h3><p>如果系统没有安装, 可依照下面命令安装.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/shemminger/iproute2.git</span><br></pre></td></tr></table></figure>
<h3 id="列出路由列表"><a href="#列出路由列表" class="headerlink" title="列出路由列表"></a>列出路由列表</h3><p>显示不同的网络接口, 包括WIFI,以太网,和点对点等.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ip route show</span><br></pre></td></tr></table></figure></p>
<h3 id="设置和删除ip地址"><a href="#设置和删除ip地址" class="headerlink" title="设置和删除ip地址"></a>设置和删除ip地址</h3><h4 id="设置ip"><a href="#设置ip" class="headerlink" title="设置ip"></a>设置ip</h4><p>给你的机器设置一个IP地址, 可以使用一下ip命令:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo ip addr add 192.168.0.193/24 dev wlan0</span><br></pre></td></tr></table></figure>
<p>ip地址有一个后缀<code>24</code>.这种用法用于在无类域内路由选择(CIDR)中来显示所用的子网掩码.在这个例子中,子网掩码是<code>255.255.255.0</code>.<br><a href="https://yunlongs.cn/wordpress/?p=302" target="_blank" rel="noopener">CIDR参考资料</a></p>
<h4 id="删除ip"><a href="#删除ip" class="headerlink" title="删除ip"></a>删除ip</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ sudo ip addr del 192.168.0.193/24 dev wlan0</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/18/iOS面试题/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="John Doe">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="xclidongbo's Blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/05/18/iOS面试题/" itemprop="url">
iOS面试题
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Created: 2018-05-18 19:20:18" itemprop="dateCreated datePublished" datetime="2018-05-18T19:20:18+08:00">2018-05-18</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">Edited on</span>
<time title="Modified: 2018-05-27 17:23:11" itemprop="dateModified" datetime="2018-05-27T17:23:11+08:00">2018-05-27</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">In</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/iOS/" itemprop="url" rel="index"><span itemprop="name">-iOS</span></a></span>