用户锁,扣费模型,足够能支持一般并发

足够能支持一般并发,高并发可在这基础上使用redis原子性

UserPrice.java

package com.andnnl.userlock;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by andnnl on 2017/7/31.
 */
public class UserPrice {
    private static UserPrice instance;
    private static Map<String, byte[]> lockMap = new ConcurrentHashMap<>();
    private static Map<String, User> userMap = new ConcurrentHashMap<>();

    private UserPrice() {
    }

    public synchronized static UserPrice getInstance(String uid) {
        if (instance == null)
            instance = new UserPrice();
        byte[] b = lockMap.get(uid);
        if (b == null) {
            lockMap.put(uid, new byte[]{});
        }
        return instance;
    }

    public boolean checkPrice(String uid, int price) {
        synchronized (lockMap.get(uid)) {
            User user = userMap.get(uid);
            if (user == null) {
                user = new User(uid, 100000000);
                userMap.put(uid, user);
            }
            if (user.price - price >= 0) {
                return true;
            }
        }
        return false;
    }

    public boolean deduct(String uid, int price) {
        synchronized (lockMap.get(uid)) {
            if (checkPrice(uid, price)) {
                User user = userMap.get(uid);
                user.price -= price;
            }
        }

        return false;
    }

    static void print() {
        for (Map.Entry<String, User> entry : userMap.entrySet()) {
            System.out.println(entry.getValue().toString());
        }

    }


    public static void main(String[] args) {
        for (int i = 0; i < 300; i++) {
            final int finalI = i % 3;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String uid = "uid" + finalI;
                    UserPrice up = UserPrice.getInstance(uid);
                    while (true) {
                        if (up.checkPrice(uid, 3)) {
                            up.deduct(uid, 3);
                        }
                    }
                }
            }).start();
        }
        while (true) {
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            print();
            System.out.println();
        }
    }
}

User.java

package com.andnnl.userlock;

/**
 * Created by andnnl on 2017/7/31.
 */
public class User {
    String uid;
    int price;

    public User() {
    }

    public User(String uid, int price) {
        this.uid = uid;
        this.price = price;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid='" + uid + '\'' +
                ", price=" + price +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值