偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

喝了100杯醬香拿鐵,我開竅了

開發(fā) 后端
使用“越細粒度的鎖越好”,真的是這樣嗎?會不會產(chǎn)生一些其它問題?通過對醬香拿鐵進行排序,解決了死鎖問題,避免循環(huán)等待,效率也得到了提升。

大家好,我是哪吒。

上一篇提到了鎖粒度的問題,使用“越細粒度的鎖越好”,真的是這樣嗎?會不會產(chǎn)生一些其它問題?

先說結論,可能會產(chǎn)生死鎖問題。

下面還是以購買醬香拿鐵為例:

1、定義咖啡實體類Coffee

@Data
public class Coffee {
    // 醬香拿鐵
    private String name;

    // 庫存
    public Integer inventory;

    public ReentrantLock lock = new ReentrantLock();
}

2、初始化數(shù)據(jù)

private static List<Coffee> coffeeList = generateCoffee();

public static List<Coffee> generateCoffee(){
    List<Coffee> coffeeList = new ArrayList<>();
    coffeeList.add(new Coffee("醬香拿鐵1", 100));
    coffeeList.add(new Coffee("醬香拿鐵2", 100));
    coffeeList.add(new Coffee("醬香拿鐵3", 100));
    coffeeList.add(new Coffee("醬香拿鐵4", 100));
    coffeeList.add(new Coffee("醬香拿鐵5", 100));
    return coffeeList;
}

3、隨機獲取n杯咖啡

// 隨機獲取n杯咖啡
private static List<Coffee> getCoffees(int n) {
    if(n >= coffeeList.size()){
        return coffeeList;
    }

    List<Coffee> randomList = Stream.iterate(RandomUtils.nextInt(n), i -> RandomUtils.nextInt(coffeeList.size()))
            .distinct()// 去重
            .map(coffeeList::get)// 跟據(jù)上面取得的下標獲取咖啡
            .limit(n)// 截取前面 需要隨機獲取的咖啡
            .collect(Collectors.toList());
    return randomList;
}

4、購買咖啡

private static boolean buyCoffees(List<Coffee> coffees) {
    //存放所有獲得的鎖
    List<ReentrantLock> locks = new ArrayList<>();
    for (Coffee coffee : coffees) {
        try {
            // 獲得鎖3秒超時
            if (coffee.lock.tryLock(3, TimeUnit.SECONDS)) {
                // 拿到鎖之后,扣減咖啡庫存
                locks.add(coffee.lock);
                coffeeList = coffeeList.stream().map(x -> {
                 // 購買了哪個,就減哪個
                    if (coffee.getName().equals(x.getName())) {
                        x.inventory--;
                    }
                    return x;
                }).collect(Collectors.toList());
            } else {
                locks.forEach(ReentrantLock::unlock);
                return false;
            }
        } catch (InterruptedException e) {
        }
    }
    locks.forEach(ReentrantLock::unlock);
    return true;
}

3、通過parallel并行流,購買100次醬香拿鐵,一次買2杯,統(tǒng)計成功次數(shù)

public static void main(String[] args){
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    // 通過parallel并行流,購買100次醬香拿鐵,一次買2杯,統(tǒng)計成功次數(shù)
    long success = IntStream.rangeClosed(1, 100).parallel()
            .mapToObj(i -> {
                List<Coffee> getCoffees = getCoffees(2);
                //Collections.sort(getCoffees, Comparator.comparing(Coffee::getName));
                return buyCoffees(getCoffees);
            })
            .filter(result -> result)
            .count();

    stopWatch.stop();
    System.out.println("成功次數(shù):"+success);
    System.out.println("方法耗時:"+stopWatch.getTotalTimeSeconds()+"秒");
    for (Coffee coffee : coffeeList) {
        System.out.println(coffee.getName()+"-剩余:"+coffee.getInventory()+"杯");
    }
}

耗時有點久啊,20多秒。

數(shù)據(jù)對不對?

  • 醬香拿鐵1賣了53杯。
  • 醬香拿鐵2賣了57杯。
  • 醬香拿鐵3賣了20杯。
  • 醬香拿鐵4賣了22杯。
  • 醬香拿鐵5賣了19杯。
  • 一共賣了171杯。

數(shù)量也對不上,應該賣掉200杯才對,哪里出問題了?

4、使用visualvm測一下:

果不其然,出問題了,產(chǎn)生了死鎖。

線程 m 在等待的一個鎖被線程 n 持有,線程 n 在等待的另一把鎖被線程 m 持有。

  1. 比如美杜莎買了醬香拿鐵1和醬香拿鐵2,小醫(yī)仙買了醬香拿鐵2和醬香拿鐵1;
  2. 美杜莎先獲得了醬香拿鐵1的鎖,小醫(yī)仙獲得了醬香拿鐵2的鎖;
  3. 然后美杜莎和小醫(yī)仙接下來要分別獲取 醬香拿鐵2 和 醬香拿鐵1 的鎖;
  4. 這個時候鎖已經(jīng)被對方獲取了,只能相互等待一直到 3 秒超時。

5、如何解決呢?

讓大家都先拿一樣的醬香拿鐵不就好了。讓所有線程都先獲取醬香拿鐵1的鎖,然后再獲取醬香拿鐵2的鎖,這樣就不會出問題了。

也就是在隨機獲取n杯咖啡后,對其進行排序即可。

// 通過parallel并行流,購買100次醬香拿鐵,一次買2杯,統(tǒng)計成功次數(shù)
long success = IntStream.rangeClosed(1, 100).parallel()
        .mapToObj(i -> {
            List<Coffee> getCoffees = getCoffees(2);
            // 根據(jù)咖啡名稱進行排序
            Collections.sort(getCoffees, Comparator.comparing(Coffee::getName));
            return buyCoffees(getCoffees);
        })
        .filter(result -> result)
        .count();

6、再測試一下

  • 成功次數(shù)100。
  • 咖啡賣掉了200杯,數(shù)量也對得上。
  • 代碼執(zhí)行速度也得到了質的飛躍,因為不用沒有循環(huán)等待鎖的時間了。

看來真的不是越細粒度的鎖越好,真的會產(chǎn)生死鎖問題。通過對醬香拿鐵進行排序,解決了死鎖問題,避免循環(huán)等待,效率也得到了提升。

責任編輯:姜華 來源: 哪吒編程
相關推薦

2021-12-03 10:46:49

ELKGraylog運維

2021-12-02 06:34:34

GraylogELK日志

2023-11-09 14:12:23

獎項

2020-12-21 07:36:15

緩存數(shù)據(jù)庫緩存層

2022-12-30 17:18:04

2021-12-05 23:17:18

iOS蘋果系統(tǒng)

2016-07-04 18:18:32

歐洲杯

2025-06-25 09:31:41

2020-01-21 21:15:16

WiFi網(wǎng)絡WiFi6

2020-07-03 15:10:35

Java Rust 開發(fā)

2023-12-13 08:11:36

2021-01-11 08:03:30

阿里中臺項目

2010-11-24 10:41:32

2021-12-15 06:18:23

iOS15.2蘋果 iOS

2024-01-26 07:48:10

SpringKafka提升

2024-01-05 13:26:00

KafkaTopicSpring

2025-07-04 09:31:56

2019-08-05 15:01:10

遠程醫(yī)療醫(yī)療保健就診

2021-04-28 08:51:53

計算機網(wǎng)絡安全
點贊
收藏

51CTO技術棧公眾號