最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java在高并發(fā)場(chǎng)景下實(shí)現(xiàn)點(diǎn)贊計(jì)數(shù)器

 更新時(shí)間:2023年06月28日 16:01:57   作者:SSPo  
點(diǎn)贊計(jì)數(shù)器的本質(zhì)就是對(duì)某個(gè)變量在高并發(fā)情況下的修改,這篇文章主要為大家介紹了Java實(shí)現(xiàn)點(diǎn)贊計(jì)數(shù)器的示例代碼,感興趣的小伙伴可以了解一下

點(diǎn)贊計(jì)數(shù)器的本質(zhì)就是對(duì)某個(gè)變量在高并發(fā)情況下的修改,volatile關(guān)鍵字只能保證變量的可見(jiàn)性和有序性,不能保證其原子性

使用方案有如下選擇:

1. Synchronized 關(guān)鍵字

package concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
class ClickResource{
    int number = 0;
    public synchronized void clickBySynchronized(){number++;}
}
// 100個(gè)線程, 每個(gè)線程點(diǎn)贊 100萬(wàn) 次, 求準(zhǔn)確率和效率
public class AccumulatorCompareDemo {
    public static final int _1w = 10000;
    public static final int threadNumber = 100;
    public static void main(String[] args) throws InterruptedException {
        ClickResource clickResource = new ClickResource();
        long startTime  = System.currentTimeMillis();
        CountDownLatch countDownLatch1 = new CountDownLatch(threadNumber);
        for (int i = 0; i < threadNumber; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j < 100*_1w; j++) {
                        clickResource.clickBySynchronized();
                    }
                }finally {
                    countDownLatch1.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch1.await();
        long endTime  = System.currentTimeMillis();
        System.out.println("所用時(shí)間="+(endTime-startTime)+"ms"+"    最終點(diǎn)贊次數(shù)="+clickResource.number);
    }
}
// synchronized       悲觀鎖
// 所用時(shí)間=2258ms    最終點(diǎn)贊次數(shù)=100000000

2. 使用 AtomicLong 原子類

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
class ClickResource{
    int number = 0;
    AtomicLong atomicLong = new AtomicLong(0);
    public void clickByAtomicLong(){atomicLong.getAndIncrement();}
}
// 100個(gè)線程, 每個(gè)線程點(diǎn)贊 100萬(wàn) 次, 求準(zhǔn)確率和效率
public class AccumulatorCompareDemo {
    public static final int _1w = 10000;
    public static final int threadNumber = 100;
    public static void main(String[] args) throws InterruptedException {
        ClickResource clickResource = new ClickResource();
        long startTime  = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
        for (int i = 0; i < threadNumber; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j < 100*_1w; j++) {
                        clickResource.clickByAtomicLong();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        long endTime  = System.currentTimeMillis();
        System.out.println("所用時(shí)間="+(endTime-startTime)+"ms"+"    最終點(diǎn)贊次數(shù)="+clickResource.atomicLong);
    }
}
// CAS - 輕量級(jí)鎖 - 調(diào)用 Unsafe 類的 cas 方法 - 底層操作系統(tǒng)原子指令 
// 所用時(shí)間=1177ms    最終點(diǎn)贊次數(shù)=100000000

3.使用 LongAdder 類

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
class ClickResource{
    int number = 0;
    LongAdder longAdder = new LongAdder();
    public void clickByLongAdder(){longAdder.increment();}
}
// 100個(gè)線程, 每個(gè)線程點(diǎn)贊 100萬(wàn) 次, 求準(zhǔn)確率和效率
public class AccumulatorCompareDemo {
    public static final int _1w = 10000;
    public static final int threadNumber = 100;
    public static void main(String[] args) throws InterruptedException {
        ClickResource clickResource = new ClickResource();
        long startTime  = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch2 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch3 = new CountDownLatch(threadNumber);
        CountDownLatch countDownLatch4 = new CountDownLatch(threadNumber);
        for (int i = 0; i < threadNumber; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j < 100*_1w; j++) {
                        clickResource.clickByLongAdder();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        long endTime  = System.currentTimeMillis();
        System.out.println("所用時(shí)間="+(endTime-startTime)+"ms"+"    最終點(diǎn)贊次數(shù)="+clickResource.longAdder);
    }
}
//所用時(shí)間=141ms    最終點(diǎn)贊次數(shù)=100000000

4. 使用 LongAccumulator 類

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;
class ClickResource{
    int number = 0;
    LongAccumulator longAccumulator = new LongAccumulator((x,y)->x+y,0);
    public void clickByLongAccumulator(){longAccumulator.accumulate(1);}
}
// 100個(gè)線程, 每個(gè)線程點(diǎn)贊 100萬(wàn) 次, 求準(zhǔn)確率和效率
public class AccumulatorCompareDemo {
    public static final int _1w = 10000;
    public static final int threadNumber = 100;
    public static void main(String[] args) throws InterruptedException {
        ClickResource clickResource = new ClickResource();
        long startTime  = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
        for (int i = 0; i < threadNumber; i++) {
            new Thread(()->{
                try {
                    for (int j = 0; j < 100*_1w; j++) {
                        clickResource.clickByLongAccumulator();
                    }
                }finally {
                    countDownLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        long endTime  = System.currentTimeMillis();
        System.out.println("所用時(shí)間="+(endTime-startTime)+"ms"+"    最終點(diǎn)贊次數(shù)="+clickResource.longAccumulator);
    }
}
// 所用時(shí)間=150ms    最終點(diǎn)贊次數(shù)=100000000

LongAccumulator 和 LongAdder 底層調(diào)用的方法相同,主要區(qū)別是,LongAdder只能初始為0,且只能做加法操作,LongAccumulator 能初始成任何數(shù)

需要注意的是:

LongAdder 和 LongAccumulator 并不保證最終獲取的數(shù)據(jù)是完全準(zhǔn)確無(wú)誤的,也許會(huì)有少許偏差,但在高并發(fā)的點(diǎn)贊場(chǎng)景下?tīng)奚倭康臄?shù)據(jù)精確性來(lái)保證高性能是完全能接受的

以上就是Java在高并發(fā)場(chǎng)景下實(shí)現(xiàn)點(diǎn)贊計(jì)數(shù)器的詳細(xì)內(nèi)容,更多關(guān)于Java點(diǎn)贊計(jì)數(shù)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺談Java文件執(zhí)行順序、main程序入口的理解

    淺談Java文件執(zhí)行順序、main程序入口的理解

    這篇文章主要介紹了Java文件執(zhí)行順序、main程序入口的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring中@ExceptionHandler注解的使用方式

    Spring中@ExceptionHandler注解的使用方式

    這篇文章主要介紹了Spring中@ExceptionHandler注解的使用方式,@ExceptionHandler注解我們一般是用來(lái)自定義異常的,可以認(rèn)為它是一個(gè)異常攔截器(處理器),需要的朋友可以參考下
    2024-01-01
  • Spring MVC參數(shù)傳遞中文亂碼解決方法分享

    Spring MVC參數(shù)傳遞中文亂碼解決方法分享

    這篇文章主要介紹了Spring MVC參數(shù)傳遞中文亂碼解決方法分享,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • java操作PDF文件方法之轉(zhuǎn)換、合成、切分

    java操作PDF文件方法之轉(zhuǎn)換、合成、切分

    最近需要做?個(gè)把多個(gè)pdf報(bào)告合并成?個(gè)以?便預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于java操作PDF文件方法之轉(zhuǎn)換、合成、切分的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Java如何配置IDEA自定義注釋

    Java如何配置IDEA自定義注釋

    在IDEA中設(shè)置自動(dòng)創(chuàng)建類和方法的注釋可以提高編碼效率,確保代碼的一致性和可讀性,首先,對(duì)于創(chuàng)建類的注釋,可以通過(guò)修改File→Settings→File and Code Templates→Class的模板來(lái)實(shí)現(xiàn),其次,對(duì)于方法注釋
    2024-10-10
  • Spring boot route Controller接收參數(shù)常用方法解析

    Spring boot route Controller接收參數(shù)常用方法解析

    這篇文章主要介紹了Spring boot route Controller接收參數(shù)常用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出與報(bào)表生成的完整教程

    SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入導(dǎo)出與報(bào)表生成的完整教程

    本文將帶你掌握Spring Boot數(shù)據(jù)導(dǎo)入導(dǎo)出與報(bào)表生成的核心概念與使用方法,包括數(shù)據(jù)導(dǎo)入導(dǎo)出的定義與特點(diǎn),幫你學(xué)會(huì)在實(shí)際開(kāi)發(fā)中處理數(shù)據(jù)導(dǎo)入導(dǎo)出與報(bào)表生成問(wèn)題
    2026-04-04
  • Xml中使用foreach遍歷對(duì)象實(shí)現(xiàn)代碼

    Xml中使用foreach遍歷對(duì)象實(shí)現(xiàn)代碼

    這篇文章主要介紹了Xml中使用foreach遍歷對(duì)象實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問(wèn)題

    SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問(wèn)題

    這篇文章主要介紹了SpringBoot接口數(shù)據(jù)如何實(shí)現(xiàn)優(yōu)雅的脫敏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • IDEA安裝阿里代碼規(guī)范插件的步驟圖文詳解

    IDEA安裝阿里代碼規(guī)范插件的步驟圖文詳解

    這篇文章主要介紹了IDEA安裝阿里代碼規(guī)范插件的步驟,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12

最新評(píng)論

玉屏| 客服| 桐庐县| 文昌市| 简阳市| 云浮市| 公主岭市| 封丘县| 武川县| 商南县| 通山县| 乌鲁木齐县| 彰化县| 金沙县| 板桥市| 海阳市| 六盘水市| 台州市| 湘阴县| 雷波县| 玉门市| 长宁区| 南川市| 临邑县| 连江县| 平乐县| 上犹县| 潜江市| 霞浦县| 石台县| 通州市| 瓦房店市| 安平县| 潼关县| 彭山县| 泰兴市| 屏东县| 景德镇市| 乌鲁木齐县| 漠河县| 沐川县|