多线程之初识并发问题:模拟抢票,使用线程锁解决方案

多线程之初识并发问题

什么是并发:我们模拟一个抢票的例子

public class TextThread3 implements Runnable {
    
    

    //票数
    private  int tickName=10;
    @Override
    public void run() {
    
    
        while (true){
    
    
            //如果票数小于10跳出循环
            if(tickName<=0){
    
    
                break;
            }
            try {
    
    
            	//延时操作,防止cpu将全部资源分配给同一个人
                Thread.sleep(200);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            //Thread.currentThread().getName():获取当前线程的名字
            System.out.println(Thread.currentThread().getName()+"拿到了第"+tickName--+"张票");
        }
    }

    public static void main(String[] args) {
    
    
        TextThread3 textThread3=new TextThread3();
        new Thread(textThread3, "小明").start();
        new Thread(textThread3, "老师").start();
        new Thread(textThread3, "黄牛党").start();
    }
}

运行代码我们会发现:在我们没有加线程锁的情况下,老师和小明同时抢到了第4张票在这里插入图片描述

解决方案:加线程锁,并使用标志位停止线程

*不知道什么的标志位的参考:https://blog.csdn.net/moerduo0/article/details/113803579

修改后的代码:我们将买票的代码封装成一个新的方法buy(),并给它加一个线程锁synchronized

public class TextThread3 implements Runnable {
    
    

    //票数
    private  int tickName=10;
    //标志位
    boolean flag =true;

    @Override
    public void run() {
    
    
        //当flag为false停止循环
        while (flag){
    
    
            try {
    
    
                //买票
                buy();
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
    }
	//线程锁
    private synchronized void buy() throws Exception{
    
    
        //判断是否有票没配票时flag为false
        if(tickName<=0){
    
    
            flag=false;
            return;
        }
        //模拟延时
        Thread.sleep(100);
        //买票
        System.out.println(Thread.currentThread().getName()+"拿到了第"+tickName--+"张票");
    }
    public static void main(String[] args) {
    
    
        TextThread3 textThread3=new TextThread3();
        new Thread(textThread3, "小明").start();
        new Thread(textThread3, "老师").start();
        new Thread(textThread3, "黄牛党").start();
    }
}

效果:现在就是正常的在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/moerduo0/article/details/113800657