简体   繁体   中英

Thread Safety and code synchronization in java

I was reading about Thread Safety and code synchronization in java .. I did this code in java . I do not like its output when it shows true or false in the output .

do you have any idea to remove the true and false from the output to improve my code performance ?

does my code works according to the Thread Safety and synchronization ?

thanks

public class B {

        public int numberOfTotalTickets=2;
        public int numberT;
        public String nameP;


        public boolean check(int numberOfTickets, String name) {
            numberT=numberOfTickets;
            nameP=name;
            if(numberOfTickets <=numberOfTotalTickets) {
                actionOk(nameP);
                return true;

            } else {
                actionNotOk(nameP);
                return false;
            }

        }

        public void actionOk(String nameP) {
        numberOfTotalTickets=numberOfTotalTickets-numberT;
        System.out.println("your booking is complete"+" "+ nameP);

        }

        public void actionNotOk(String nameP) {
            System.out.println("number of tickets available"+" "+ numberOfTotalTickets);
            System.out.println("sorry"+" "+ nameP +" " +"your booking is not complete because we have only"+" "+numberOfTotalTickets+" "+"tickets");

            }

    public static void main(String[] args) {

        B b = new B();
        System.out.println(b.check(2, "jack"));
        System.out.println(b.check(2, "sam"));

    }

}

my code's output is

your booking is complete jack
true
number of tickets available 0
sorry sam your booking is not complete because we have only 0 tickets
false

This code is not thread-safe.

public void actionOk(String nameP) {
    numberOfTotalTickets=numberOfTotalTickets-numberT;
    System.out.println("your booking is complete"+" "+ nameP);
}

If two threads are accessing the instance variables at the same time, then the wrong number of tickets could be returned.

Consider this

Thread1: read number of tickets as 2
Thread1: interrupted
Thread2: read number of tickets as 2
Thread2: assigns 2 tickets
Thread1: resumes and also assigns 2 tickets.

Both threads would see that 2 tickets are available.

Regarding printing of true/false: You are printing the output of check function, because it is type of boolean and you call it inside System.out.println . Just call it directly, without System.out.println .

Regarding Thread Safety: Try to use private access modifiers when not requiring access from other classes. If I understand correctly what you want to achieve with this code (you just need to call check from outside), you can transform everything to private apart from your check method and by adding the synchronized keyword to this method, you are safe.

public synchronized boolean check(int numberOfTickets, String name) {
...

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