-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1083 lines (589 loc) · 58 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 >
<head>
<!--[if lt IE 9]>
<style>body {display: none; background: none !important} </style>
<meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
<![endif]-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="Cary" />
<meta name="description" content="这辈子没法做太多的事情, 所以每一件都要做到精彩绝伦!">
<meta property="og:type" content="website">
<meta property="og:title" content="酒馆儒生">
<meta property="og:url" content="https://carygao.github.io/index.html">
<meta property="og:site_name" content="酒馆儒生">
<meta property="og:description" content="这辈子没法做太多的事情, 所以每一件都要做到精彩绝伦!">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="酒馆儒生">
<meta name="twitter:description" content="这辈子没法做太多的事情, 所以每一件都要做到精彩绝伦!">
<link rel="apple-touch-icon" href= "/apple-touch-icon.png">
<link rel="alternate" href="/atom.xml" title="酒馆儒生" type="application/atom+xml">
<link rel="shortcut icon" href="/favicon.png">
<link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
<link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">
<link rel="stylesheet" href="/css/style.css">
<style> .article { opacity: 0;} </style>
<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
<title>酒馆儒生</title>
<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
<script>
var yiliaConfig = {
fancybox: true,
animate: true,
isHome: true,
isPost: false,
isArchive: false,
isTag: false,
isCategory: false,
fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
search: undefined
}
</script>
<script> yiliaConfig.jquery_ui = [false]; </script>
<script> yiliaConfig.rootUrl = "\/";</script>
</head>
<body>
<div id="container">
<div class="left-col">
<div class="overlay"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="/img/avatar.png" class="animated zoomIn">
</a>
<hgroup>
<h1 class="header-author"><a href="/">Cary</a></h1>
</hgroup>
<p class="header-subtitle">这辈子没法做太多的事情, 所以每一件都要做到精彩绝伦!</p>
<div id="switch-btn" class="switch-btn">
<div class="icon">
<div class="icon-ctn">
<div class="icon-wrap icon-house" data-idx="0">
<div class="birdhouse"></div>
<div class="birdhouse_holes"></div>
</div>
<div class="icon-wrap icon-ribbon hide" data-idx="1">
<div class="ribbon"></div>
</div>
<div class="icon-wrap icon-link hide" data-idx="2">
<div class="loopback_l"></div>
<div class="loopback_r"></div>
</div>
<div class="icon-wrap icon-me hide" data-idx="3">
<div class="user"></div>
<div class="shoulder"></div>
</div>
</div>
</div>
<div class="tips-box hide">
<div class="tips-arrow"></div>
<ul class="tips-inner">
<li>菜单</li>
<li>标签</li>
<li>友情链接</li>
<li>关于我</li>
</ul>
</div>
</div>
<div id="switch-area" class="switch-area">
<div class="switch-wrap">
<section class="switch-part switch-part1">
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives/">所有文章</a></li>
<li><a href="/tags/">标签云</a></li>
<li><a href="/about/">关于我</a></li>
</ul>
</nav>
<nav class="header-nav">
<ul class="social">
<a class="fa Email" href="mailto:[email protected]" title="Email"></a>
<a class="fa GitHub" href="#" title="GitHub"></a>
<a class="fa RSS" href="/atom.xml" title="RSS"></a>
</ul>
</nav>
</section>
<section class="switch-part switch-part2">
<div class="widget tagcloud" id="js-tagcloud">
<ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ios/">ios</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/iphone/">iphone</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/objective-c/">objective-c</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/oracle/">oracle</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sqlplus/">sqlplus</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/weblogic/">weblogic</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/随笔/">随笔</a></li></ul>
</div>
</section>
<section class="switch-part switch-part3">
<div id="js-friends">
<a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
<a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
<a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
</div>
</section>
<section class="switch-part switch-part4">
<div id="js-aboutme">专注于前端</div>
</section>
</div>
</div>
</header>
</div>
</div>
<div class="mid-col">
<nav id="mobile-nav">
<div class="overlay">
<div class="slider-trigger"></div>
<h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">Cary</a></h1>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="/img/avatar.png" class="animated zoomIn">
</a>
<hgroup>
<h1 class="header-author"><a href="/" title="回到主页">Cary</a></h1>
</hgroup>
<p class="header-subtitle">这辈子没法做太多的事情, 所以每一件都要做到精彩绝伦!</p>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives/">所有文章</a></li>
<li><a href="/tags/">标签云</a></li>
<li><a href="/about/">关于我</a></li>
<div class="clearfix"></div>
</ul>
</nav>
<nav class="header-nav">
<ul class="social">
<a class="fa Email" target="_blank" href="mailto:[email protected]" title="Email"></a>
<a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
<a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
</ul>
</nav>
</header>
</div>
<link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
<div class="body-wrap">
<article id="post-ConcurrentHashMap-JDK1-8实现深入剖析" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2017/08/22/ConcurrentHashMap-JDK1-8实现深入剖析/" class="article-date">
<time datetime="2017-08-22T07:25:54.000Z" itemprop="datePublished">2017-08-22</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2017/08/22/ConcurrentHashMap-JDK1-8实现深入剖析/">ConcurrentHashMap JDK1.8源码深入剖析</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="ConcurrentHashMap-JDK1-8源码深入剖析-一"><a href="#ConcurrentHashMap-JDK1-8源码深入剖析-一" class="headerlink" title="ConcurrentHashMap JDK1.8源码深入剖析(一)"></a>ConcurrentHashMap JDK1.8源码深入剖析(一)</h2><h3 id="阅读前建议掌握的知识"><a href="#阅读前建议掌握的知识" class="headerlink" title="阅读前建议掌握的知识"></a>阅读前建议掌握的知识</h3><ol>
<li>CAS操作</li>
<li>hashMap的原理</li>
<li>并发相关锁的知识</li>
<li>并发中原子性,可见性,重排序的概念</li>
</ol>
<h3 id="特性"><a href="#特性" class="headerlink" title="特性"></a>特性</h3><ul>
<li>底层实现原理为数组+链表+红黑树</li>
<li>当链表大小阀值超过8时转换为红黑树</li>
<li>大量使用CAS操作来实现无锁</li>
<li>并发扩容</li>
</ul>
<h3 id="主要属性"><a href="#主要属性" class="headerlink" title="主要属性"></a>主要属性</h3><h5 id="TREEIFY-THRESHOLD-UNTREEIFY-THRESHOLD"><a href="#TREEIFY-THRESHOLD-UNTREEIFY-THRESHOLD" class="headerlink" title="TREEIFY_THRESHOLD,UNTREEIFY_THRESHOLD"></a>TREEIFY_THRESHOLD,UNTREEIFY_THRESHOLD</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">static final int TREEIFY_THRESHOLD = 8;</span><br><span class="line">static final int UNTREEIFY_THRESHOLD = 6;</span><br></pre></td></tr></table></figure>
<p> 大于8时,链表转树阀值。</p>
<p> 小于6时树转链表,仅在扩容tranfer时才可能树转链表。</p>
<h5 id="sizeCtl"><a href="#sizeCtl" class="headerlink" title="sizeCtl"></a>sizeCtl</h5><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">private transient volatile int sizeCtl;</span><br></pre></td></tr></table></figure>
<p>hash表初始化或扩容时的一个控制位标识量。负数代表正在进行初始化或扩容操作。</p>
<ul>
<li>-1代表正在初始化。</li>
<li>-N 表示有N-1个线程正在进行扩容操作。</li>
<li>正数或0代表hash表还没有被初始化,这个数值表示初始化或下一次进行扩容的大小。</li>
</ul>
<h5 id="MOVED"><a href="#MOVED" class="headerlink" title="MOVED"></a>MOVED</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></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> * Encodings for Node hash fields. See above for explanation.</span><br><span class="line"> */</span><br><span class="line"> static final int MOVED = -1; // hash for forwarding nodes</span><br></pre></td></tr></table></figure>
<p>Node的hash值,为 -1时代表该Node为<code>ForwardingNode</code></p>
<h5 id="TREEBIN"><a href="#TREEBIN" class="headerlink" title="TREEBIN"></a>TREEBIN</h5><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">static final int TREEBIN = -2;// hash for roots of trees</span><br></pre></td></tr></table></figure>
<p>树根节点的hash值</p>
<h5 id="MAX-RESIZERS"><a href="#MAX-RESIZERS" class="headerlink" title="MAX_RESIZERS"></a>MAX_RESIZERS</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">/**</span><br><span class="line"> * The maximum number of threads that can help resize.</span><br><span class="line"> * Must fit in 32 - RESIZE_STAMP_BITS bits.</span><br><span class="line"> */</span><br><span class="line">private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;</span><br></pre></td></tr></table></figure>
<p>并发扩容时最大允许的help线程总数</p>
<h3 id="Node"><a href="#Node" class="headerlink" title="Node"></a>Node</h3><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">static class Node<K,V> implements Map.Entry<K,V> {</span><br><span class="line"> final int hash;</span><br><span class="line"> final K key;</span><br><span class="line"> //让value为volatile类型,让其有可见性</span><br><span class="line"> volatile V val;</span><br><span class="line"> //</span><br><span class="line"> volatile Node<K,V> next;</span><br><span class="line"></span><br><span class="line"> Node(int hash, K key, V val, Node<K,V> next) {</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><span class="line"> //禁止直接setValue</span><br><span class="line"> public final V setValue(V value) {</span><br><span class="line"> throw new UnsupportedOperationException();</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"> Node<K,V> find(int h, Object k) {</span><br><span class="line"> Node<K,V> e = this;</span><br><span class="line"> if (k != null) {</span><br><span class="line"> do {</span><br><span class="line"> K ek;</span><br><span class="line"> if (e.hash == h &&</span><br><span class="line"> ((ek = e.key) == k || (ek != null && k.equals(ek))))</span><br><span class="line"> return e;</span><br><span class="line"> } while ((e = e.next) != null);</span><br><span class="line"> }</span><br><span class="line"> return null;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>节点是ConcurrentHashMap的核心内部类,他的设计原理是不允许call setValue来直接改变它的值,因为put需要用CAS来实现无锁线程安全操作。同时提供了find方法来通过hash值和key寻找链表中的节点,该方法被map.get()调用,被子类overriden,来实现不同类型节点的查询方法。</p>
<h3 id="关键的三个CAS方法"><a href="#关键的三个CAS方法" class="headerlink" title="关键的三个CAS方法"></a>关键的三个CAS方法</h3><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">static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {</span><br><span class="line"> return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>tabAt方法通过UNsafe的CAS操作来获得在i下标位置的Node</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></pre></td><td class="code"><pre><span class="line">static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,</span><br><span class="line"> Node<K,V> c, Node<K,V> v) {</span><br><span class="line"> return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p> 对i下标位置上的Node进行CAS的set操作,如果有其他线程正在访问则会返回false,ASHIFT是指tab[i]中第i个元素在相对于数组第一个元素的偏移量,而ABASE就算第一数组的内存素的偏移地址,((long)i << ASHIFT) + ABASE就算i最后的地址。</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></pre></td><td class="code"><pre><span class="line">static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {</span><br><span class="line"> U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>利用volatile方法set节点位置的值,注意<code>setTabAt</code>总是在被锁定的代码块中才调用。</p>
<h3 id="Put方法"><a href="#Put方法" class="headerlink" title="Put方法"></a>Put方法</h3><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><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">final V putVal(K key, V value, boolean onlyIfAbsent) {</span><br><span class="line"> //不允许key或value为null</span><br><span class="line"> if (key == null || value == null) throw new NullPointerException();</span><br><span class="line"> int hash = spread(key.hashCode());</span><br><span class="line"> int binCount = 0;</span><br><span class="line"> //经典CAS操作,不set成功死不罢休</span><br><span class="line"> for (Node<K,V>[] tab = table;;) {</span><br><span class="line"> Node<K,V> f; int n, i, fh;</span><br><span class="line"> //初始化table</span><br><span class="line"> if (tab == null || (n = tab.length) == 0)</span><br><span class="line"> tab = initTable();</span><br><span class="line"> //如果节点为null,直接用cas方法尝试set,若成功直接break跳出CAS循环。</span><br><span class="line"> else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {</span><br><span class="line"> if (casTabAt(tab, i, null,</span><br><span class="line"> new Node<K,V>(hash, key, value, null)))</span><br><span class="line"> break; // no lock when adding to empty bin</span><br><span class="line"> }</span><br><span class="line"> //如果是forwardingNode节点,代表有其他线程正在扩容,于是helpTransfer辅助扩容</span><br><span class="line"> else if ((fh = f.hash) == MOVED)</span><br><span class="line"> tab = helpTransfer(tab, f);</span><br><span class="line"> //这里意味者节点下是链表或是树,通过synchronized锁住f表头,则锁住了整个链表或树</span><br><span class="line"> else {</span><br><span class="line"> V oldVal = null;</span><br><span class="line"> synchronized (f) {</span><br><span class="line"> if (tabAt(tab, i) == f) {</span><br><span class="line"> //判断是链表还是树,fh>=0则为链表</span><br><span class="line"> if (fh >= 0) {</span><br><span class="line"> binCount = 1;</span><br><span class="line"> for (Node<K,V> e = f;; ++binCount) {</span><br><span class="line"> K ek;</span><br><span class="line"> if (e.hash == hash &&</span><br><span class="line"> ((ek = e.key) == key ||</span><br><span class="line"> (ek != null && key.equals(ek)))) {</span><br><span class="line"> oldVal = e.val;</span><br><span class="line"> if (!onlyIfAbsent)</span><br><span class="line"> e.val = value;</span><br><span class="line"> break;</span><br><span class="line"> }</span><br><span class="line"> Node<K,V> pred = e;</span><br><span class="line"> //若遍历到最后一个节点,则直接将新值插入到尾部,跳出循环</span><br><span class="line"> if ((e = e.next) == null) {</span><br><span class="line"> pred.next = new Node<K,V>(hash, key,</span><br><span class="line"> value, null);</span><br><span class="line"> break;</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"> else if (f instanceof TreeBin) {</span><br><span class="line"> Node<K,V> p;</span><br><span class="line"> binCount = 2;</span><br><span class="line"> if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,</span><br><span class="line"> value)) != null) {</span><br><span class="line"> oldVal = p.val;</span><br><span class="line"> if (!onlyIfAbsent)</span><br><span class="line"> p.val = value;</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"> if (binCount != 0) {</span><br><span class="line"> //大于8的阀值下将链表转化为树</span><br><span class="line"> if (binCount >= TREEIFY_THRESHOLD)</span><br><span class="line"> treeifyBin(tab, i);</span><br><span class="line"> if (oldVal != null)</span><br><span class="line"> return oldVal;</span><br><span class="line"> break;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> //更新map大小,+1</span><br><span class="line"> addCount(1L, binCount);</span><br><span class="line"> return null;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p>putVal的设计核心思想是</p>
<ul>
<li>不允许key或value为null。</li>
<li>用spread计算key的hash值再用类似取模的方法来确定在table数组中的位置。</li>
<li>若有需要则初始化table。</li>
<li>若该节点为null,无哈希碰撞,则使用CAS来进行set操作。</li>
<li>若该节点被标记为forward,则代表有其他线程正在进行扩容,当前线程也会辅助进行扩容操作。</li>
<li>若该节点为链表或树,代表哈希碰撞,则使用synchronized来锁住桶,然后进行set操作。注意若在该节点为链表时,会依次向后找到应该存放的位置,若大于阀值8则转化为红黑树,若本来就是树结构则按照树的方法存放。</li>
</ul>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><ul>
<li><a href="http://www.cnblogs.com/huaizuo/archive/2016/04/20/5413069.html" target="_blank" rel="external">http://www.cnblogs.com/huaizuo/archive/2016/04/20/5413069.html</a></li>
<li><a href="https://my.oschina.net/hosee/blog/675884" target="_blank" rel="external">https://my.oschina.net/hosee/blog/675884</a></li>
</ul>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-Redis实现分布式锁" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2017/08/08/Redis实现分布式锁/" class="article-date">
<time datetime="2017-08-08T09:30:42.000Z" itemprop="datePublished">2017-08-08</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2017/08/08/Redis实现分布式锁/">Redis实现分布式锁</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="Redis实现分布式锁"><a href="#Redis实现分布式锁" class="headerlink" title="Redis实现分布式锁"></a>Redis实现分布式锁</h2><h4 id="分布式锁的一些问题"><a href="#分布式锁的一些问题" class="headerlink" title="分布式锁的一些问题"></a>分布式锁的一些问题</h4><ol>
<li>并发问题,若多个客户端同时上锁,结果只允许一个客户端成功,其他失败,可以利用redis的<code>SETNX</code> 命令来实现,该命令允许若给定的 key 已经存在,则 <code>SETNX</code> 不做任何动作,设置成功,返回 1 ,设置失败,返回 0 。</li>
<li>上锁后解锁的问题,可以考虑使用redis key的ttl过期,通过<code>PEXPIRE</code>来设置key的自动过期。若不使用自动过期特性,则需要在锁定结束后使用<code>DEL</code>命令来进行解锁,这种情况有可能发生某个客户端已经crash,于是这个锁会永远锁定,导致其他客户端无法获得锁。</li>
</ol>
<h4 id="Redis2-6以后新特性"><a href="#Redis2-6以后新特性" class="headerlink" title="Redis2.6以后新特性"></a>Redis2.6以后新特性</h4><p>redis2.6.12以后 SET 命令的行为可以通过一系列参数来修改</p>
<ol>
<li>EX second :设置键的过期时间为 second 秒。 <code>SET key value EX second</code> 效果等同于 <code>SETEX key second value</code> 。</li>
<li>PX millisecond :设置键的过期时间为 millisecond 毫秒。 <code>SET key value PX millisecond</code> 效果等同于 <code>PSETEX key millisecond value</code> 。</li>
<li>NX :只在键不存在时,才对键进行设置操作。 <code>SET key value NX</code> 效果等同于 <code>SETNX key value</code> 。</li>
<li>XX :只在键已经存在时,才对键进行设置操作。</li>
</ol>
<h4 id="使用新特性来解决分布式锁"><a href="#使用新特性来解决分布式锁" class="headerlink" title="使用新特性来解决分布式锁"></a>使用新特性来解决分布式锁</h4><ol>
<li>使用命令 <code>SET key value NX EX second</code> 设置锁的同时为锁设置TTL过期时间,若当锁已经存在时返回nil。</li>
</ol>
<h4 id="Redisson解决方法"><a href="#Redisson解决方法" class="headerlink" title="Redisson解决方法"></a>Redisson解决方法</h4><p><a href="https://github.com/redisson/redisson/wiki/Redisson%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D" target="_blank" rel="external">Redisson</a>是redis的一个java客户端,实现了分布式锁和同步器,Redisson的分布式锁RLock Java对象实现了java.util.concurrent.locks.Lock接口,同时还支持自动过期解锁。<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></pre></td><td class="code"><pre><span class="line">RLock lock = redisson.getLock("anyLock");</span><br><span class="line">// 最常见的使用方法</span><br><span class="line">lock.lock();</span><br><span class="line"></span><br><span class="line">// 支持过期解锁功能</span><br><span class="line">// 10秒钟以后自动解锁</span><br><span class="line">// 无需调用unlock方法手动解锁</span><br><span class="line">lock.lock(10, TimeUnit.SECONDS);</span><br><span class="line"></span><br><span class="line">// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁</span><br><span class="line">boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);</span><br><span class="line">...</span><br><span class="line">lock.unlock();</span><br></pre></td></tr></table></figure></p>
<p>以上是redisson使用锁的方法,它的原理采用了lua脚本的方式,这样能兼容redis 2.6.12之前的版本,锁的核心源码如下RedissonLock.java<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><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></pre></td><td class="code"><pre><span class="line"><T> RFuture<T> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {</span><br><span class="line"> internalLockLeaseTime = unit.toMillis(leaseTime);</span><br><span class="line"></span><br><span class="line"> return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, command,</span><br><span class="line"> //先检查key是否存在</span><br><span class="line"> "if (redis.call('exists', KEYS[1]) == 0) then " +</span><br><span class="line"> //key不存在,创建Hash</span><br><span class="line"> "redis.call('hset', KEYS[1], ARGV[2], 1); " +</span><br><span class="line"> //设置过期时间</span><br><span class="line"> "redis.call('pexpire', KEYS[1], ARGV[1]); " +</span><br><span class="line"> "return nil; " +</span><br><span class="line"> "end; " +</span><br><span class="line"> //若锁已经存在,并且是同样获得锁的线程调用</span><br><span class="line"> "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +</span><br><span class="line"> //复用锁,为锁+1</span><br><span class="line"> "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +</span><br><span class="line"> //延长锁定时间</span><br><span class="line"> "redis.call('pexpire', KEYS[1], ARGV[1]); " +</span><br><span class="line"> "return nil; " +</span><br><span class="line"> "end; " +</span><br><span class="line"> //若无法获得锁,返回锁的剩余时间</span><br><span class="line"> "return redis.call('pttl', KEYS[1]);",</span><br><span class="line"> Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName(threadId));</span><br><span class="line"> }</span><br></pre></td></tr></table></figure></p>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-could-not-read-Username-for-https-github-com-Invalid-argument" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2017/08/04/could-not-read-Username-for-https-github-com-Invalid-argument/" class="article-date">
<time datetime="2017-08-04T04:23:47.000Z" itemprop="datePublished">2017-08-04</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2017/08/04/could-not-read-Username-for-https-github-com-Invalid-argument/">could not read Username for 'https://github.com': Invalid argument</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="could-not-read-Username-for-‘https-github-com‘-Invalid-argument"><a href="#could-not-read-Username-for-‘https-github-com‘-Invalid-argument" class="headerlink" title="could not read Username for ‘https://github.com‘: Invalid argument"></a>could not read Username for ‘<a href="https://github.com" target="_blank" rel="external">https://github.com</a>‘: Invalid argument</h2><p>好久没玩hexo blog了,今天心血来潮想恢复使用,结果发现使用<code>hexo d</code>命令时会报如下错误</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">could not read Username for 'https://github.com': Invalid argument</span><br></pre></td></tr></table></figure>
<p>网上搜索了下,这个问题有以下几个原因:</p>
<ol>
<li><p>如果安装git客户端的时候没有勾选git命令在bash和cmd命令都有效,若是在cmd命令下则因为没有将git添加到windows的path,所以会出现这个错误,一般可以尝试在blog的目录打开git bash再尝试<code>hexo d</code>。</p>
</li>
<li><p>未将username和email加入到git,可以使用如下命令试试</p>
</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name "yourname"</span><br><span class="line">git config --global user.email "youremail"</span><br></pre></td></tr></table></figure>
<ol>
<li>检查<code>_config.yml</code>的配置是否正确</li>
</ol>
<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></pre></td><td class="code"><pre><span class="line">deploy:</span><br><span class="line"> type: git</span><br><span class="line"> repo: https://github.com/yourname/yourname.github.io.git</span><br><span class="line"> branch: master</span><br></pre></td></tr></table></figure>
<ol>
<li>客户端太老,我遇到就是这个问题,即便切换了git bash也仍然报错,于是我升级了新客户端,结果能正常弹出github的用户名和密码输入框,确认后可以正常部署。</li>
</ol>
</div>
<div class="article-info article-info-index">
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-如何配置Oracle的SQLPlus" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/03/11/如何配置Oracle的SQLPlus/" class="article-date">
<time datetime="2016-03-11T08:00:25.000Z" itemprop="datePublished">2016-03-11</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/03/11/如何配置Oracle的SQLPlus/">如何配置Oracle的SQLPlus</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h3 id="SQL-Plus-Overview"><a href="#SQL-Plus-Overview" class="headerlink" title="SQL*Plus Overview"></a>SQL*Plus Overview</h3><p>SQL*Plus是一个用于与Oracle Database交互和执行查询的工具,我们要安装的是命令行工具。<br>
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/oracle/">oracle</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/oracle/">oracle</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/sqlplus/">sqlplus</a></li></ul>
</div>
<p class="article-more-link">
<a href="/2016/03/11/如何配置Oracle的SQLPlus/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-Oracle-ORA-01840-input-value-not-long-enough-for-date-format" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/03/08/Oracle-ORA-01840-input-value-not-long-enough-for-date-format/" class="article-date">
<time datetime="2016-03-08T07:20:33.000Z" itemprop="datePublished">2016-03-08</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/03/08/Oracle-ORA-01840-input-value-not-long-enough-for-date-format/">Oracle - ORA-01840: input value not long enough for date format</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="错误日志:"><a href="#错误日志:" class="headerlink" title="错误日志:"></a>错误日志:</h2><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">Oracle - ORA-01840: input value not long enough for date format</span><br></pre></td></tr></table></figure>
<p>在程式执行查询方法时,日志中出现以上错误</p>
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/oracle/">oracle</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/oracle/">oracle</a></li></ul>
</div>
<p class="article-more-link">
<a href="/2016/03/08/Oracle-ORA-01840-input-value-not-long-enough-for-date-format/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-Hexo安装教程-一" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/03/07/Hexo安装教程-一/" class="article-date">
<time datetime="2016-03-07T04:14:39.000Z" itemprop="datePublished">2016-03-07</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/03/07/Hexo安装教程-一/">Hexo安装教程(一)</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="什么是Hexo"><a href="#什么是Hexo" class="headerlink" title="什么是Hexo?"></a>什么是Hexo?</h2><p>Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 <a href="http://daringfireball.net/projects/markdown/">Markdown</a>(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。</p>
<h2 id="Hexo安装环境"><a href="#Hexo安装环境" class="headerlink" title="Hexo安装环境"></a>Hexo安装环境</h2><ul>
<li>Windows7</li>
</ul>
<h2 id="Hexo安装步骤概览"><a href="#Hexo安装步骤概览" class="headerlink" title="Hexo安装步骤概览"></a>Hexo安装步骤概览</h2><ol>
<li>安装node.js</li>
<li>安装github desktop</li>
<li>安装hexo
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/hexo/">hexo</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/hexo/">hexo</a></li></ul>
</div>
<p class="article-more-link">
<a href="/2016/03/07/Hexo安装教程-一/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-随笔" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/03/04/随笔/" class="article-date">
<time datetime="2016-03-04T08:55:33.000Z" itemprop="datePublished">2016-03-04</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/03/04/随笔/">随笔</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p><img src="/resources/164646181173681758.jpg" alt="澳门"><br><em>没有人是天才,个人的才能是靠自己挖掘的,登上山顶,望望身后已看不见的芸芸众生,回过头来,还需要继续向上。生活本不是闲情逸致,它本就是丑陋的,这个世界不会因为谁的离开就停止送给你恶意。我们唯一要学会的就是战斗,即使遇到困难,我们也要独自无谓的战斗下去。</em></p>
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/感悟/">感悟</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/随笔/">随笔</a></li></ul>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-ORA-01882-timezone-region" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2016/03/03/ORA-01882-timezone-region/" class="article-date">
<time datetime="2016-03-03T08:47:02.000Z" itemprop="datePublished">2016-03-03</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2016/03/03/ORA-01882-timezone-region/">ORA-01882: timezone region</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<h2 id="错误日志:"><a href="#错误日志:" class="headerlink" title="错误日志:"></a>错误日志:</h2><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">ORA-01882: timezone region</span><br></pre></td></tr></table></figure>
<p>用weblogic配置连接池连到oracle时报错</p>
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/java/">java</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/oracle/">oracle</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/weblogic/">weblogic</a></li></ul>
</div>
<p class="article-more-link">
<a href="/2016/03/03/ORA-01882-timezone-region/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-原-如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2010/08/18/原-如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来/" class="article-date">
<time datetime="2010-08-18T08:21:00.000Z" itemprop="datePublished">2010-08-18</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2010/08/18/原-如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来/">如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来?</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>大家都知道如果给<code>UITableViewController</code>装载一些数据和控制cell的行为(高度,样式等)都需要指定<code>UITableView</code>的delegate给自身,一般会使用IB或者在<code>viewDidLoad</code>中写上<code>self.tableView.delegate = self;</code>然后实现<code>UITableViewDelegate</code>和<code>UITableViewDataSource</code>中的方法,例如:<code>-tableView:cellForRowAtIndexPath:</code> 等等.<br>那么如何将实现<code>UITableViewDelegate</code>和<code>UITableViewDataSource</code>的方法脱离出来单独放在一个新的class中呢?<br>
</div>
<div class="article-info article-info-index">
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/objectiveC/">objectiveC</a>
</div>
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/ios/">ios</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/iphone/">iphone</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/objective-c/">objective-c</a></li></ul>
</div>
<p class="article-more-link">
<a href="/2010/08/18/原-如何将UITableViewDelegate和UITableViewDataSource从UITableViewController中分离出来/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-原-如何移动组件的位置,例如如何将UIAlertView显示在屏幕上部" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/2010/05/26/原-如何移动组件的位置,例如如何将UIAlertView显示在屏幕上部/" class="article-date">
<time datetime="2010-05-26T08:59:00.000Z" itemprop="datePublished">2010-05-26</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/2010/05/26/原-如何移动组件的位置,例如如何将UIAlertView显示在屏幕上部/">[原]如何移动组件的位置,例如如何将UIAlertView显示在屏幕上部</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>苹果自带的警告框非常好用,但是它总是显示在屏幕中间,我们如何将它的位置移动呢?从Iphone SDK3开始我们可以使用CGAffineTransformTranslate</p>
</div>
<div class="article-info article-info-index">
<p class="article-more-link">
<a href="/2010/05/26/原-如何移动组件的位置,例如如何将UIAlertView显示在屏幕上部/#more">阅读全文 >></a>
</p>
<div class="clearfix"></div>
</div>
</div>
</article>
<nav id="page-nav">
<span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/">Next »</a>
</nav>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info">
<div class="footer-left">
<i class="fa fa-copyright"></i>