Skip to content

Problem in SCThread1.java #17

@shadowxdgamer

Description

@shadowxdgamer

Issue Description:

I've noticed a small bug in the current code. Occasionally, the sequence breaks, allowing two customers to check their balance at the same time.

Proposed Fix:

To resolve this, I implemented the following fix:

public void useATM() { 
    synchronized(atm) {
        atm.checkBalance(name);
        atm.withdraw(name, amount);
    }
}

By using a synchronized block and locking on the "atm" reference in the Customer class, this ensures that each customer can only use the ATM object once the previous customer has finished. This change guarantees that the operations are performed sequentially, preventing any overlap.

Outcome:

After applying this fix, the sequence integrity is maintained, and no two customers can check their balance simultaneously.

Also thank you for the course, i have been learning a lot! 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions