【多线程】共享资源的保护

一、有锁实现
package com.learning.share_resource;

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author wangyouhui
 * @Description 加锁的共享资源
 **/
@Slf4j
public class AccountWithLock {
    private Integer account;

    public AccountWithLock(Integer account){
        this.account = account;
    }

    public void withdraw(Integer amount){
        synchronized (this){
            this.account -= amount;
        }
    }

    public Integer getAccount(){
        return this.account;
    }

    public static void main(String[] args) {
        AccountWithLock accountWithLock = new AccountWithLock(10000);
        List<Thread> list = new LinkedList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(new Thread(()->{
                accountWithLock.withdraw(10);
            }));
        }
        long start = System.currentTimeMillis();
        list.forEach(Thread::start);
        list.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.currentTimeMillis();
        log.info("余额为:{}",accountWithLock.getAccount());
        log.info("耗时为:{}", end-start);
    }
}

二、无锁实现
package com.learning.share_resource;

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author wangyouhui
 * @Description 无锁的共享资源
 **/
@Slf4j
public class AccountWithNoLock {
    private AtomicInteger account;

    public AccountWithNoLock(Integer account){
        this.account = new AtomicInteger(account);
    }

    public void withdraw(Integer amount){
        while(true){
            // 获取余额的最新值
            int previous = account.get();
            // 要修改的余额
            int next = previous - amount;
            // 真正修改
            if(account.compareAndSet(previous, next)){
                break;
            }
        }
    }

    public Integer getAccount(){
        return this.account.get();
    }

    public static void main(String[] args) {
        AccountWithNoLock accountWithLock = new AccountWithNoLock(10000);
        List<Thread> list = new LinkedList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(new Thread(()->{
                accountWithLock.withdraw(10);
            }));
        }
        long start = System.currentTimeMillis();
        list.forEach(Thread::start);
        list.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.currentTimeMillis();
        log.info("余额为:{}",accountWithLock.getAccount());
        log.info("耗时为:{}", end-start);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值