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

Java在高并發(fā)場景下實現(xiàn)點贊計數(shù)器

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

點贊計數(shù)器的本質(zhì)就是對某個變量在高并發(fā)情況下的修改,volatile關(guā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個線程, 每個線程點贊 100萬 次, 求準確率和效率
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("所用時間="+(endTime-startTime)+"ms"+"    最終點贊次數(shù)="+clickResource.number);
    }
}
// synchronized       悲觀鎖
// 所用時間=2258ms    最終點贊次數(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個線程, 每個線程點贊 100萬 次, 求準確率和效率
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("所用時間="+(endTime-startTime)+"ms"+"    最終點贊次數(shù)="+clickResource.atomicLong);
    }
}
// CAS - 輕量級鎖 - 調(diào)用 Unsafe 類的 cas 方法 - 底層操作系統(tǒng)原子指令 
// 所用時間=1177ms    最終點贊次數(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個線程, 每個線程點贊 100萬 次, 求準確率和效率
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("所用時間="+(endTime-startTime)+"ms"+"    最終點贊次數(shù)="+clickResource.longAdder);
    }
}
//所用時間=141ms    最終點贊次數(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個線程, 每個線程點贊 100萬 次, 求準確率和效率
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("所用時間="+(endTime-startTime)+"ms"+"    最終點贊次數(shù)="+clickResource.longAccumulator);
    }
}
// 所用時間=150ms    最終點贊次數(shù)=100000000

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

需要注意的是:

LongAdder 和 LongAccumulator 并不保證最終獲取的數(shù)據(jù)是完全準確無誤的,也許會有少許偏差,但在高并發(fā)的點贊場景下犧牲少量的數(shù)據(jù)精確性來保證高性能是完全能接受的

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

相關(guān)文章

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

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

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

    Spring中@ExceptionHandler注解的使用方式

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

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

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

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

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

    Java如何配置IDEA自定義注釋

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

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

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

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

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

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

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

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

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

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

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

最新評論

上饶县| 湖州市| 巴青县| 武隆县| 田阳县| 福安市| 宣汉县| 高清| 东丽区| 南通市| 庄河市| 瑞金市| 阳曲县| 页游| 营山县| 益阳市| 普陀区| 乌鲁木齐市| 茂名市| 梁山县| 保山市| 同江市| 钟祥市| 开原市| 开远市| 酒泉市| 错那县| 社会| 黄大仙区| 晋宁县| 龙游县| 濮阳县| 博客| 栖霞市| 孟津县| 金门县| 奉新县| 顺昌县| 昆山市| 阳原县| 定结县|