Thread Safety
Thread Safety is an important Concept in Java, Mainly used in Multi threading programme in case such as When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. As you will see, Java provides unique, language-level support for it.
Main Purpose in details
For example In a Cinema ticket booking website , there is only one last ticket is available means , in that case two users trying to book the last ticket, two threads are accessing a class for example bookTicket(which is about to book ticket based on availability ), In that case what would happen is same ticket is would be booked for two users(but in reality only one ticket is available in the physical world). To prevent this type of problem a concept called synchronization (That is used in thread safety)
If synchronization is used in that bookTicket() class means only one thread can access the class at a time (booking of same tickets for two users is not possible) others thread has to wait until that thread (which accessed the bookTicket() class first) which enters the class exist

No comments:
Post a Comment