使用多线程模拟卖票买票问题

模拟卖票和买票,测试代码是否会出现线程安全问题。

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Slf4j
public class 买票卖票 {

    public static void main(String[] args) throws InterruptedException {

        TicketWindow window = new TicketWindow(1000);

        //模拟售票个数
        List<Integer> accountList = new Vector<>();
        //存取所有线程,用于join
        List<Thread> threadList = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            Thread thread = new Thread(() ->{
                //售票
                int account = window.sell(randomPicket());
                //计数
                accountList.add(account);
            });

            threadList.add(thread);
            thread.start();
        }

        for (Thread t : threadList) {
            t.join();
        }

        int all = 0;

        for(int i = 0;i < accountList.size();i++){
            all += accountList.get(i);
        }
        log.debug("实际卖出:{}",all);
        log.debug("剩余票数:{}",window.getCount());

    }

    public static int randomPicket(){
        return (int)(Math.random() * 10);
    }

}


class TicketWindow{
    //票的个数
    private int count;

    public TicketWindow(int count) {
        this.count = count;
    }

    //卖票
    public int sell(int num){
        if(num < count){
            count -= num;
            return num;
        }else{
            return 0;
        }
    }

    public int getCount() {
        return count;
    }
}

结果展示:

20:05:20.185 [main] DEBUG 买票卖票 - 实际卖出:1007
20:05:20.192 [main] DEBUG 买票卖票 - 剩余票数:1

分析

  • 2000个线程共享window对象
  • window对象的sell()方法对count存在读写操作,且sell()方法不具有原子性

解决问题

  • sell()方法使用synchronized关键字修饰
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值