简体   繁体   中英

How the output of following method differs magically if I just remove a print statement and execute using threads

I know about synchronization, my question is not to synchronize thread. I want to ask why is the output so different if I just remove first print statement inside if condition . Suddenly else statement starts to get executed in every case. Why?

class Covid{
    int slot = 10;
    void bookslot(int selected){
        if(slot >= selected){
            System.out.println(selected+ " booked");
            slot = slot-selected;
            System.out.println("Left slots are "+ slot);
        }else{
            System.out.println("unavailale slots");
        }
    }
}


class People extends Thread{

    static Covid cv;
    int slect;
    public void run(){
        cv.bookslot(slect);
    }
}

public class Main{

    public static void main(String[] args) {

        People.cv = new Covid();

        People t1 = new People();
        t1.slect = 7;
        t1.start();
        
        People t2 = new People();
        t2.slect = 6;
        t2.start();
    }
}

Output: 7 booked 6 booked Left slots are 3 Left slots are -3

CASE2 when print is removed (rest code is same, only one line commented out)

class Covid{
    int slot = 10;
    void bookslot(int selected){
        if(slot >= selected){
            //System.out.println(selected+ " booked");
            slot = slot-selected;
            System.out.println("Left slots are "+ slot);
        }else{
            System.out.println("unavailale slots");
        }
    }
}

class People extends Thread{

    static Covid cv;

    int slect;

    public void run(){
        cv.bookslot(slect);
    }

}

public class Main{

    public static void main(String[] args) {

        People.cv = new Covid();

        People t1 = new People();
        t1.slect = 7;
        t1.start();

        People t2 = new People();
        t2.slect = 6;
        t2.start();
    }
}

OUTPUT unavailale slots Left slots are 3

This logic is faulty

 slot = slot-selected;

It appears to me that you're confused about whether 'slot' is supposed to be the count of slots available or the maximum slot number. As implemented, it does not really work either way.

Slot starts at 10. When we execute with slot 7 selected, slot gets reset to 3, which means the thread selecting slot 6 will fail if it executes after the slot 7 selection. Is that intended? I can't really say from the code.

With the print, the timing chances to be such that both threads get to execute the 'if' decision before either of them recomputes the value of 'slot'.

Without the print, 'slot' is recomputed in one thread before the second thread evaluates the 'if'.

tl;dr - use proper synchronization or else behaviour is erratic.

Better naming might help readers. I'd suggest 'slot' be named either 'availableSlotCount' or 'maximumAvailableSlot', as appropriate to how you want it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM