Multiple threads operate on an object

Me
if so

    @Override
    public void run() {
    
    
        while (true){
    
    
            if(ticketNums <= 0){
    
    
                break;
            }
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ ticketNums-- +"张票");
          //  System.out.println(ticketNums);
        }`

The above code snippet is the correct logic, but the output result is that
Insert picture description here
there are scalpers and teachers who both got the 7th ticket at the same time. This shouldn’t be right.

I am changing the number of code tickets step by step to achieve decrement step by step. The final code is like this


//多个线程同时操作一个对象
//买过车票的例子
public class TestThread4 implements Runnable {
    
    
    //票数
    public int ticketNums = 10;

    @Override
    public void run() {
    
    
        while (ticketNums>2){
    
    
//            if(ticketNums == 0){
    
    
//                break;
//            }
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"拿到了第"+ ticketNums-- +"张票");
          //  System.out.println(ticketNums);
        }
    }
    public static void main(String[] args)  {
    
    
        TestThread4 ticket = new TestThread4();
        new Thread(ticket,"小明").start();
        try {
    
    
            Thread.sleep(10);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        new Thread(ticket,"老师").start();
        try {
    
    
            Thread.sleep(10);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        new Thread(ticket,"黄牛党").start();
        try {
    
    
            Thread.sleep(10);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }


请前辈给指点一下 , 我是怀疑现在的处理器速度太快,会不会是两个线程同时对ticketNum操作了 
才导致不能正确输出结果。

我通过改while循环的判断条件 和 增加每个线程之间开始的时间 才解决的问题  但是逻辑上没问题的呀

Finally, I consulted the classmates in the group, just add a lock and the code is as follows


package testThread;

//多个线程同时操作一个对象
//买过车票的例子
public class TestThread4 implements Runnable {
    
    


    //票数
    private static volatile int ticketNums = 10;
    private static Object obj = new Object();
    @Override
    public void run() {
    
    
        while (true){
    
    
            if(ticketNums<=0){
    
    
                break;
            }
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        synchronized (obj){
    
    
                if(ticketNums > 0){
    
    

                    System.out.println(Thread.currentThread().getName()+"拿到了第"+ ticketNums-- +"张票");
                }else {
    
    
                    System.out.println("谢谢惠顾");
                }
        }

        }
    }

    public static void main(String[] args) {
    
    
        TestThread4 ticket = new TestThread4();
        new Thread(ticket,"小明").start();

        new Thread(ticket,"老师").start();

        new Thread(ticket,"黄牛党").start();
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37412975/article/details/114154455