|
4 | 4 | <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
5 | 5 | <head> |
6 | 6 | <title>资源争用模型(泛多线程编程)</title> |
7 | | -<!-- 2017-09-27 周三 19:22 --> |
| 7 | +<!-- 2017-09-27 三 22:48 --> |
8 | 8 | <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> |
9 | 9 | <meta name="generator" content="Org-mode" /> |
10 | 10 | <meta name="author" content="王月阳" /> |
@@ -203,13 +203,12 @@ <h2>Table of Contents</h2> |
203 | 203 | <ul> |
204 | 204 | <li><a href="#sec-5-4-1">5.4.1. synchronized的用法</a></li> |
205 | 205 | <li><a href="#sec-5-4-2">5.4.2. Java对象头</a></li> |
206 | | -<li><a href="#sec-5-4-3">5.4.3. LockSupport+cas+state+CHL队列构成了Java锁的基础</a></li> |
207 | | -<li><a href="#sec-5-4-4">5.4.4. AQS解读</a></li> |
208 | | -<li><a href="#sec-5-4-5">5.4.5. 读写分离</a></li> |
209 | | -<li><a href="#sec-5-4-6">5.4.6. 写写分离</a></li> |
210 | | -<li><a href="#sec-5-4-7">5.4.7. CountDownLatch</a></li> |
211 | | -<li><a href="#sec-5-4-8">5.4.8. FutureTask</a></li> |
212 | | -<li><a href="#sec-5-4-9">5.4.9. Semaphore</a></li> |
| 206 | +<li><a href="#sec-5-4-3">5.4.3. AQS解读</a></li> |
| 207 | +<li><a href="#sec-5-4-4">5.4.4. 读写分离</a></li> |
| 208 | +<li><a href="#sec-5-4-5">5.4.5. 写写分离</a></li> |
| 209 | +<li><a href="#sec-5-4-6">5.4.6. CountDownLatch</a></li> |
| 210 | +<li><a href="#sec-5-4-7">5.4.7. FutureTask</a></li> |
| 211 | +<li><a href="#sec-5-4-8">5.4.8. Semaphore</a></li> |
213 | 212 | </ul> |
214 | 213 | </li> |
215 | 214 | </ul> |
@@ -756,30 +755,68 @@ <h4 id="sec-5-4-2"><span class="section-number-4">5.4.2</span> Java对象头</h4 |
756 | 755 | </div> |
757 | 756 |
|
758 | 757 | <div id="outline-container-sec-5-4-3" class="outline-4"> |
759 | | -<h4 id="sec-5-4-3"><span class="section-number-4">5.4.3</span> LockSupport+cas+state+CHL队列构成了Java锁的基础</h4> |
760 | | -</div> |
| 758 | +<h4 id="sec-5-4-3"><span class="section-number-4">5.4.3</span> AQS解读</h4> |
| 759 | +<div class="outline-text-4" id="text-5-4-3"> |
| 760 | +<p> |
| 761 | +前面我们所讲的,都是一种感性的认知。比如我们说所有的争用操作都会中某个资源状态互斥量上排队,比如我们说sync保证了线程 |
| 762 | +安全,比如我们说ReadWriteLock实现了读写锁,这些都是感性的认知,我们会理解为这样就安全了。但这些到底是怎么实现呢?下 |
| 763 | +面我们以AQS为例子,详细说明设计一个锁到底要包含哪些东西,需要哪些支撑,实现哪些功能。 |
| 764 | +一个锁的设计要考虑的包含如下几个方面: |
| 765 | +</p> |
| 766 | +<ol class="org-ol"> |
| 767 | +<li>抽象资源状态的标志位 |
| 768 | +</li> |
| 769 | +<li>对标志位的原子复合操作 |
| 770 | +</li> |
| 771 | +<li>对争用者行为的控制,包括暂停和恢复 |
| 772 | +</li> |
| 773 | +<li>对争用者的暂存功能 |
| 774 | +</li> |
| 775 | +</ol> |
| 776 | + |
| 777 | +<p> |
| 778 | +以上四个条件,就是设计一个锁的时候所要考虑的必要条件。将这四个全部实现,我们就可以设计出一个基本的锁来,然后再通过添 |
| 779 | +加一些其他的属性或操作,进而实现复杂一点的锁。这个复杂不是说分布式锁就复杂了,而是一些锁的其他特性,比如公平非公平, |
| 780 | +排他还是共享。 |
| 781 | +</p> |
761 | 782 |
|
| 783 | +<p> |
| 784 | +AQS的全称叫AbstractQueuedSynchronizer,AQS就通过实现上面三个条件,进而提供了一个基础的锁的功能。AQS是怎么实现上面三 |
| 785 | +个条件的呢?这要从AQS的基本组成和操作讲起,首先AQS的基本组成包含三部分,一个int类型的state字段-作为抽象资源状态的标 |
| 786 | +志位;一个CHL队列,用来储存被暂停的线程-实现对争用者行为的暂存功能,方便恢复;还有一个就是系统提供的Unsafe对象,这个对象 |
| 787 | +主要提供了一种功能,就是对state字段原子复合操作,也就是我们常说的cas,比较成功并设置操作,这个操作由jvm层保证了原子 |
| 788 | +性。 |
| 789 | +</p> |
| 790 | + |
| 791 | +<p> |
| 792 | +AQS的组成满足了上面的条件1,2和4,那么条件3是怎么实现的呢?JUC使用了JVM提供的LockSupport类来实现3。顾名思义, |
| 793 | +LockSupport就提供了锁的基本行为支撑,包括暂停争用者行为和恢复争用者行为。 |
| 794 | +</p> |
| 795 | + |
| 796 | +<p> |
| 797 | +下面我们通过分析JUC中ReentrantLock是怎么实现的,来看看怎么把这四个条件组合在一起就实现一个锁的。 |
| 798 | +LockSupport+cas+state+CHL队列构成了Java锁的基础 |
| 799 | +</p> |
| 800 | +</div> |
| 801 | +</div> |
762 | 802 | <div id="outline-container-sec-5-4-4" class="outline-4"> |
763 | | -<h4 id="sec-5-4-4"><span class="section-number-4">5.4.4</span> AQS解读</h4> |
| 803 | +<h4 id="sec-5-4-4"><span class="section-number-4">5.4.4</span> 读写分离</h4> |
| 804 | +<div class="outline-text-4" id="text-5-4-4"> |
| 805 | +</div><ol class="org-ol"><li><a id="sec-5-4-4-1" name="sec-5-4-4-1"></a>ReentrantReadWriteLock<br /></li></ol> |
764 | 806 | </div> |
765 | 807 | <div id="outline-container-sec-5-4-5" class="outline-4"> |
766 | | -<h4 id="sec-5-4-5"><span class="section-number-4">5.4.5</span> 读写分离</h4> |
| 808 | +<h4 id="sec-5-4-5"><span class="section-number-4">5.4.5</span> 写写分离</h4> |
767 | 809 | <div class="outline-text-4" id="text-5-4-5"> |
768 | | -</div><ol class="org-ol"><li><a id="sec-5-4-5-1" name="sec-5-4-5-1"></a>ReentrantReadWriteLock<br /></li></ol> |
| 810 | +</div><ol class="org-ol"><li><a id="sec-5-4-5-1" name="sec-5-4-5-1"></a>ConcurrentHashMap<br /></li></ol> |
769 | 811 | </div> |
770 | 812 | <div id="outline-container-sec-5-4-6" class="outline-4"> |
771 | | -<h4 id="sec-5-4-6"><span class="section-number-4">5.4.6</span> 写写分离</h4> |
772 | | -<div class="outline-text-4" id="text-5-4-6"> |
773 | | -</div><ol class="org-ol"><li><a id="sec-5-4-6-1" name="sec-5-4-6-1"></a>ConcurrentHashMap<br /></li></ol> |
| 813 | +<h4 id="sec-5-4-6"><span class="section-number-4">5.4.6</span> CountDownLatch</h4> |
774 | 814 | </div> |
775 | 815 | <div id="outline-container-sec-5-4-7" class="outline-4"> |
776 | | -<h4 id="sec-5-4-7"><span class="section-number-4">5.4.7</span> CountDownLatch</h4> |
| 816 | +<h4 id="sec-5-4-7"><span class="section-number-4">5.4.7</span> FutureTask</h4> |
777 | 817 | </div> |
778 | 818 | <div id="outline-container-sec-5-4-8" class="outline-4"> |
779 | | -<h4 id="sec-5-4-8"><span class="section-number-4">5.4.8</span> FutureTask</h4> |
780 | | -</div> |
781 | | -<div id="outline-container-sec-5-4-9" class="outline-4"> |
782 | | -<h4 id="sec-5-4-9"><span class="section-number-4">5.4.9</span> Semaphore</h4> |
| 819 | +<h4 id="sec-5-4-8"><span class="section-number-4">5.4.8</span> Semaphore</h4> |
783 | 820 | </div> |
784 | 821 | </div> |
785 | 822 | </div> |
@@ -812,7 +849,7 @@ <h3 id="sec-7-2"><span class="section-number-3">7.2</span> Zookeeper一主多从 |
812 | 849 | </div> |
813 | 850 | <div id="postamble" class="status"> |
814 | 851 | <p class="author">Author: 王月阳</p> |
815 | | -<p class="date">Created: 2017-09-27 周三 19:22</p> |
| 852 | +<p class="date">Created: 2017-09-27 三 22:48</p> |
816 | 853 | <p class="creator"><a href="http://www.gnu.org/software/emacs/">Emacs</a> 24.5.1 (<a href="http://orgmode.org">Org</a> mode 8.2.10)</p> |
817 | 854 | <p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p> |
818 | 855 | </div> |
|
0 commit comments