欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
CAS原理分析

在JDK 5之前Java語(yǔ)言是靠synchronized關(guān)鍵字保證同步的,這會(huì )導致有鎖(后面的章節還會(huì )談到鎖)。

鎖機制存在以下問(wèn)題:

(1)在多線(xiàn)程競爭下,加鎖、釋放鎖會(huì )導致比較多的上下文切換和調度延時(shí),引起性能問(wèn)題。

(2)一個(gè)線(xiàn)程持有鎖會(huì )導致其它所有需要此鎖的線(xiàn)程掛起。

(3)如果一個(gè)優(yōu)先級高的線(xiàn)程等待一個(gè)優(yōu)先級低的線(xiàn)程釋放鎖會(huì )導致優(yōu)先級倒置,引起性能風(fēng)險。

volatile是不錯的機制,但是volatile不能保證原子性。因此對于同步最終還是要回到鎖機制上來(lái)。

獨占鎖是一種悲觀(guān)鎖,synchronized就是一種獨占鎖,會(huì )導致其它所有需要鎖的線(xiàn)程掛起,等待持有鎖的線(xiàn)程釋放鎖。而另一個(gè)更加有效的鎖就是樂(lè )觀(guān)鎖。所謂樂(lè )觀(guān)鎖就是,每次不加鎖而是假設沒(méi)有沖突而去完成某項操作,如果因為沖突失敗就重試,直到成功為止。

CAS 操作

上面的樂(lè )觀(guān)鎖用到的機制就是CAS,Compare and Swap。

CAS有3個(gè)操作數,內存值V,舊的預期值A,要修改的新值B。當且僅當預期值A和內存值V相同時(shí),將內存值V修改為B,否則什么都不做。

非阻塞算法 (nonblocking algorithms)

一個(gè)線(xiàn)程的失敗或者掛起不應該影響其他線(xiàn)程的失敗或掛起的算法。

現代的CPU提供了特殊的指令,可以自動(dòng)更新共享數據,而且能夠檢測到其他線(xiàn)程的干擾,而 compareAndSet() 就用這些代替了鎖定。

拿出AtomicInteger來(lái)研究在沒(méi)有鎖的情況下是如何做到數據正確性的。

private volatile int value;

首先毫無(wú)以為,在沒(méi)有鎖的機制下可能需要借助volatile原語(yǔ),保證線(xiàn)程間的數據是可見(jiàn)的(共享的)。這樣才獲取變量的值的時(shí)候才能直接讀取。

public final int get() {
        return value;
    }

然后來(lái)看看++i是怎么做到的。

public final int incrementAndGet() {
    for (;;) {
        int current = get();
        int next = current + 1;
        if (compareAndSet(current, next))
            return next;
    }
}

在這里采用了CAS操作,每次從內存中讀取數據然后將此數據和+1后的結果進(jìn)行CAS操作,如果成功就返回結果,否則重試直到成功為止。

而compareAndSet利用JNI來(lái)完成CPU指令的操作。

public final boolean compareAndSet(int expect, int update) {   
    return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
    }

整體的過(guò)程就是這樣子的,利用CPU的CAS指令,同時(shí)借助JNI來(lái)完成Java的非阻塞算法。其它原子操作都是利用類(lèi)似的特性完成的。

而整個(gè)J.U.C都是建立在CAS之上的,因此對于synchronized阻塞算法,J.U.C在性能上有了很大的提升。

CAS看起來(lái)很爽,但是會(huì )導致“ABA問(wèn)題”。

CAS算法實(shí)現一個(gè)重要前提需要取出內存中某時(shí)刻的數據,而在下時(shí)刻比較并替換,那么在這個(gè)時(shí)間差類(lèi)會(huì )導致數據的變化。

比如說(shuō)一個(gè)線(xiàn)程one從內存位置V中取出A,這時(shí)候另一個(gè)線(xiàn)程two也從內存中取出A,并且two進(jìn)行了一些操作變成了B,然后two又將V位置的數據變成A,這時(shí)候線(xiàn)程one進(jìn)行CAS操作發(fā)現內存中仍然是A,然后one操作成功。盡管線(xiàn)程one的CAS操作成功,但是不代表這個(gè)過(guò)程就是沒(méi)有問(wèn)題的。如果鏈表的頭在變化了兩次后恢復了原值,但是不代表鏈表就沒(méi)有變化。因此前面提到的原子操作AtomicStampedReference/AtomicMarkableReference就很有用了。這允許一對變化的元素進(jìn)行原子操作。






================================================================================================

http://www.cnblogs.com/maxupeng/archive/2011/07/01/2096035.html

并發(fā)中CAS的含義及Java中AtomicXXX類(lèi)的分析

維基百科:

In computer science, the compare-and-swap CPU instruction ("CAS") (or the Compare & Exchange - CMPXCHG instruction in the x86 and Itanium architectures) is a special instruction that atomically (regarding intel x86, lock prefix should be there to make it really atomic) compares the contents of a memory location to a given value and, only if they are the same, modifies the contents of that memory location to a given new value. This guarantees that the new value is calculated based on up-to-date information; if the value had been updated by another thread in the meantime, the write would fail. The result of the operation must indicate whether it performed the substitution; this can be done either with a simple Boolean response (this variant is often called compare-and-set), or by returning the value read from the memory location (not the value written to it). Compare-and-Swap (and Compare-and-Swap-Double) has been an integral part of the IBM 370(and all successor) architectures since 1970. The operating systems which run on these architectures make extensive use of Compare-and-Swap (and Compare-and-Swap-Double) to facilitate process (i.e., system and user tasks) and processor (i.e., central processors) parallelism while eliminating, to the greatest degree possible, the "disabled spin locks" which were employed in earlier IBM operating systems. In these operating systems, new units of work may be instantiated "globally", into the Global Service Priority List, or "locally", into the Local Service Priority List, by the execution of a single Compare-and-Swap instruction. This dramatically improved the responsiveness of these operating systems.

===================================================

總結:CAS是硬件CPU提供的元語(yǔ),它的原理:我認為位置 V 應該包含值 A;如果包含該值,則將 B 放到這個(gè)位置;否則,不要更改該位置,只告訴我這個(gè)位置現在的值即可。

Java并發(fā)庫中的AtomicXXX類(lèi)均是基于這個(gè)元語(yǔ)的實(shí)現,以AtomicInteger為例:

public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}

public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}

其中,unsafe.compareAndSwapInt()是一個(gè)native方法,正是調用CAS元語(yǔ)完成該操作。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java并發(fā):CAS、ABA問(wèn)題、ABA問(wèn)題解決方案
Java多核線(xiàn)程筆記
并發(fā)策略-CAS算法
【Java并發(fā)編程實(shí)戰】-----“J.U.C”:CAS操作
CAS、原子操作類(lèi)的應用與淺析及Java8對其的優(yōu)化
java.util.concurrent系列文章--(4)非阻塞算法簡(jiǎn)介 - 線(xiàn)程及并發(fā)處...
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久