0

I am having theoretical knowledge on Threads but i dont have much practical knowledge, when i am going through paymentgateway code of a eCommerce website i found two things

  1. They have used volatile keyword for paymentStatus.

    if we use volatile changes made by one thread will be visible to other thread. if we apply this to paymentStatus will override the paymentStatus if two threads (of two users) use this code at same time.

  2. they have create a object for Object class and every time they modify the paymentStatus they are applying synchronization of this object

    why do we need to apply synchronization on a Object class object which we are not using anywhere?

user2727493
  • 144
  • 2
  • 4
  • 15
  • for #2, do you mean like this: [Is it useful to have a dedicated object for a lock?](https://stackoverflow.com/q/50138378/217324) – Nathan Hughes Oct 25 '19 at 16:28
  • for #1, it sounds like this is the same as [What is the volatile keyword useful for](https://stackoverflow.com/questions/106591/what-is-the-volatile-keyword-useful-for) – Nathan Hughes Oct 25 '19 at 16:31
  • @NathanHughes for #2 that is what i was looking for. but for #1 that is not what i was looking for. i am a bit confused if volatile will effect the status of the payment – user2727493 Oct 25 '19 at 17:48
  • some of the answers for the volatile keyword question seem more relevant than others, look for answers talking about memory visibility. Or [this question](https://stackoverflow.com/q/25425130/217324) is a fun example of code that doesn't take memory visibility into account and fails or succeeds by accident, take a look at it and see if it helps. – Nathan Hughes Oct 25 '19 at 18:52

0 Answers0