怎么實現(xiàn)JavaJDK沒有提供的AtomicFloat

本篇內(nèi)容主要講解“怎么實現(xiàn)Java JDK沒有提供的AtomicFloat”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么實現(xiàn)Java JDK沒有提供的AtomicFloat”吧!

創(chuàng)新互聯(lián)服務(wù)項目包括軹城網(wǎng)站建設(shè)、軹城網(wǎng)站制作、軹城網(wǎng)頁制作以及軹城網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,軹城網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到軹城省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

我們經(jīng)常會使用AtomicInteger來做計數(shù)器,如下所示:

List<String> words = Files.readAllLines(Paths.get("src/main/resources/dic.txt"));
AtomicInteger i = new AtomicInteger();
words.parallelStream().forEach(word -> {
    //獲取word的同義詞、反義詞以及相關(guān)詞
    //......
    LOGGER.info("進度:" + total + "/" + i.incrementAndGet() + " 來自線程:" + Thread.currentThread());
});

在這段代碼中,我們需要注意兩點,一是parallelStream,二是變量i。

parallelStream的使用表示forEach中的代碼段有可能會在不同線程中并發(fā)執(zhí)行,因此變量i的incrementAndGet方法要保證是原子操作,否則計數(shù)器的數(shù)據(jù)就可能會出錯。

沒啥問題,一切都還很美好,so far so nice。

有一天,我們的需求復(fù)雜了,我們需要的計數(shù)器不僅僅只是+1,而是要支持小數(shù),如2.5,3.1等等,這有什么大不了的,我們把AtomicInteger換成AtomicFloat不就支持小數(shù)了嗎?

接著我們翻遍了JDK類庫,都沒有找到AtomicFloat,怎么回事呢?

最后終于在java.util.concurrent.atomic的package-summary.html頁面的最后部分發(fā)現(xiàn)了秘密:

Additionally, classes are provided only for those types that are commonly useful in intended applications. For example, there is no atomic class for representing byte. In those infrequent cases where you would like to do so, you can use an AtomicInteger to hold byte values, and cast appropriately. You can also hold floats using Float.floatToRawIntBits(float) andFloat.intBitsToFloat(int) conversions, and doubles using Double.doubleToRawLongBits(double) andDouble.longBitsToDouble(long) conversions.

接下來我們就可以利用AtomicInteger作為基礎(chǔ)來實現(xiàn)自己的AtomicFloat了,實現(xiàn)AtomicDouble和AtomicByte也是類似的做法,下面看看在word分詞中實現(xiàn)的AtomicFloat:

package org.apdplat.word.util;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 因為Java沒有提供AtomicFloat
 * 所以自己實現(xiàn)一個
 * @author 楊尚川
 */
public class AtomicFloat extends Number {

    private AtomicInteger bits;

    public AtomicFloat() {
        this(0f);
    }

    public AtomicFloat(float initialValue) {
        bits = new AtomicInteger(Float.floatToIntBits(initialValue));
    }

    public final float addAndGet(float delta){
        float expect;
        float update;
        do {
            expect = get();
            update = expect + delta;
        } while(!this.compareAndSet(expect, update));

        return update;
    }

    public final float getAndAdd(float delta){
        float expect;
        float update;
        do {
            expect = get();
            update = expect + delta;
        } while(!this.compareAndSet(expect, update));

        return expect;
    }

    public final float getAndDecrement(){
        return getAndAdd(-1);
    }

    public final float decrementAndGet(){
        return addAndGet(-1);
    }

    public final float getAndIncrement(){
        return getAndAdd(1);
    }

    public final float incrementAndGet(){
        return addAndGet(1);
    }

    public final float getAndSet(float newValue) {
        float expect;
        do {
            expect = get();
        } while(!this.compareAndSet(expect, newValue));

        return expect;
    }

    public final boolean compareAndSet(float expect, float update) {
        return bits.compareAndSet(Float.floatToIntBits(expect), Float.floatToIntBits(update));
    }

    public final void set(float newValue) {
        bits.set(Float.floatToIntBits(newValue));
    }

    public final float get() {
        return Float.intBitsToFloat(bits.get());
    }

    public float floatValue() {
        return get();
    }

    public double doubleValue() {
        return (double) floatValue();
    }

    public int intValue() {
        return (int) get();
    }

    public long longValue() {
        return (long) get();
    }

    public String toString() {
        return Float.toString(get());
    }
}

到此,相信大家對“怎么實現(xiàn)Java JDK沒有提供的AtomicFloat”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章題目:怎么實現(xiàn)JavaJDK沒有提供的AtomicFloat
當(dāng)前鏈接:http://muchs.cn/article2/ihdcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊外貿(mào)建站、移動網(wǎng)站建設(shè)App設(shè)計、網(wǎng)站營銷App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)